Rohit Ramesh profile image Rohit Ramesh

How to Create and Mint NFTs on Optimism

Learn how to create NFT Collections and mint NFTs on Optimism in a few mins using only simple API calls. You can also mint NFTs to an email address or a wallet address.

How to Create and Mint NFTs on Optimism

In this tutorial, our goal is straightforward: to guide you through the process of creating and minting Non-Fungible Tokens (NFTs) on the Optimism blockchain using Crossmint.

For developers and enterprises alike, Crossmint is a versatile platform that simplifies integrating blockchain technology into applications, making the process approachable even for those with minimal blockchain experience.

Meanwhile, Optimism is renowned for its high performance in decentralized applications, emphasizing the significance of understanding diverse blockchain ecosystems in today's digital landscape.

Our journey, combining these technologies with JavaScript, will empower you to launch your digital assets efficiently. Expect to complete this tutorial in approximately one hour, gaining both knowledge and hands-on experience in the dynamic world of NFTs.

Table of Contents:


Create and Mint NFTs with No-code

If you are wondering how you can create an NFT Collection and mint NFTs with no-code, please read the in-depth guide linked below that teaches you how to do this with 0 code.

How to Create an NFT Collection and Mint NFTs with 0 Code
In under 10 mins, learn how to create NFT Collections, create and mint NFTs with no-code.

However, the rest of this guide will show you how you can create NFT Collections and mint NFTs on Optimism using 2 simple API calls.

Setup

Before diving into creating and minting NFTs on Optimism, it's paramount to understand the environments you'll be working in and how to properly set up your project with Crossmint. This setup involves discerning the difference between staging and production environments and creating an API key with the necessary permissions.

Difference Between Staging and Production

In the world of development, especially when dealing with applications like Crossmint, two primary environments are used: staging and production. The staging environment is essentially a testing ground. It mirrors the production environment but is not accessible to the general public. This is where you'll test your features and fixes. You can access it via https://staging.crossmint.com. On the other hand, the production environment is live, hosting the final version of your application accessible by end users at https://www.crossmint.com.

It's crucial to test thoroughly in staging before moving to production to avoid potential issues affecting your users.

Creating a Developer Account and API Key

  1. Set Up a Developer Account on Crossmint: First, ensure you have a developer account on Crossmint. If not, register one here.
  2. Navigate to the API Keys Section: Log into your Crossmint console and locate the "API Keys" section. This is where you'll manage and generate new API keys.
  3. Create a New API Key: Opt for "Create new key". You'll need a Server-side API Key for operations like minting NFTs. These keys ensure secure communication between your backend and Crossmint.
  4. Set the Scopes for the Key: When creating your API key, specify the necessary scopes. For NFT collection creation and minting, include:These scopes allow your API key to interact with various functions needed for your project.
    • "nfts.create"
    • "nfts.read"
    • "collections.create"
    • "collections.update"
    • "collections.read"
  5. Security Practices: Securely store your generated API key. Do not expose it in publicly accessible areas like GitHub or the frontend of your application.
Create and Mint NFTs on Optimism

By setting up your environment properly and ensuring your API key has the correct scopes, you're laying a solid foundation for creating and minting NFTs on Optimism using Crossmint and JavaScript.

Create an NFT Collection on Optimism

This section guides you through creating an NFT collection on the Optimism blockchain using the Crossmint API. Before proceeding, ensure you have your Crossmint API key ready and have decided whether to work in the staging or production environment.

Creating a New Collection

To create a collection on Optimism, you need to make a POST request to the Crossmint API. This requires specifying the blockchain (Optimism in this case), and providing the collection metadata including the "name", "imageUrl", and "description" of the collection.

Here's how you can make the request using JavaScript and Node.js:

const axios = require('axios');
const apiKey = 'YOUR_CROSSMINT_API_KEY'; // Replace with your Crossmint API key.
const url = 'https://www.crossmint.com/api/2022-06-09/collections/'; // Use the production URL.

const collectionData = {
  chain: "optimism",
  metadata: {
    name: "My Optimism NFT Collection",
    imageUrl: "https://www.example.com/my-collection-image.png",
    description: "A unique NFT collection on Optimism",
    symbol: "OPNFT"
  }
};

axios.post(url, collectionData, {
  headers: {
    'X-API-KEY': apiKey
  }
})
.then(response => {
  console.log('Collection created:', response.data);
})
.catch(error => {
  console.error('Error creating collection:', error.response.data);
});

This code snippet utilizes the "axios" library to send a POST request to the Crossmint API. Ensure you replace "YOUR_CROSSMINT_API_KEY" with your actual Crossmint API key. The collection's metadata, including its name, image URL, description, and symbol, is sent in the request body. On successful creation, the API responds with details of the created collection, including its ID.

Confirming Collection Deployment with Action ID

After creating the collection, you receive an "actionId" in the response. This ID can be used to verify whether the collection has been successfully deployed on the blockchain. You make a GET request to the Crossmint API with the "actionId" to check the deployment status.

Here's the code snippet to verify the collection deployment:

const actionId = 'REPLACE_WITH_ACTION_ID_FROM_RESPONSE'; // Replace with your actual actionId

axios.get(`https://www.crossmint.com/api/2022-06-09/actions/${actionId}`, {
  headers: {
    'X-API-KEY': apiKey
  }
})
.then(response => {
  console.log('Action status:', response.data);
})
.catch(error => {
  console.error('Error fetching action status:', error.response.data);
});

Replace "REPLACE_WITH_ACTION_ID_FROM_RESPONSE" with the actual "actionId" you received from the collection creation response. This request fetches the status of the collection deployment. The Collection is successfully deployed if the status returns as "completed" or a similar success indicator.

These steps conclude the process of creating and verifying a new NFT collection on Optimism using Crossmint. Proceed with minting NFTs into your newly created collection following the steps outlined in the next section.

Create and Mint NFTs on Optimism

In this section, we'll walk through the process of creating an NFT on a previously established collection on Optimism using the Crossmint API. We'll also ensure our NFT minting was successful by verifying the action ID. This tutorial assumes you have already set up your Crossmint API key and created an NFT collection on Optimism as covered in earlier sections.

Mint an NFT on the Created Collection

To mint an NFT, you'll need to make a "POST" request to the "/collections/{collectionId}/nfts" endpoint with the "collectionId", recipient details, and NFT metadata. This example uses JavaScript with the "fetch" API for making HTTP requests.

const collectionId = "your-collection-id"; // Replace with your collection ID
const apiKey = "your-crossmint-api-key"; // Replace with your Crossmint API Key

const mintNFT = async (collectionId, recipientEmail, chain) => {
  try {
    const response = await fetch(`https://www.crossmint.com/api/2022-06-09/collections/${collectionId}/nfts`, {
      method: "POST",
      headers: {
        "X-API-KEY": apiKey,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        recipient: `email:${recipientEmail}:${chain}`,
        metadata: {
          name: "Crossmint Example NFT",
          image: "https://www.crossmint.com/assets/crossmint/logo.png",
          description: "My NFT created via the mint API!",
        },
      }),
    });

    const data = await response.json();
    if(response.ok) {
      return data.actionId; // we'll use this to verify the minting
    } else {
      console.error('Error minting NFT:', data);
    }
  } catch (error) {
    console.error("Failed to mint NFT:", error);
  }
};

// Example usage
// Replace 'testy@crossmint.io' with the recipient's email and 'optimism' with the intended blockchain
mintNFT(collectionId, 'testy@crossmint.io', 'optimism').then(actionId => {
  console.log("Mint action ID:", actionId);
});

This code snippet defines a function "mintNFT" that makes a "POST" request to the Crossmint API to mint an NFT. It makes use of the collection ID obtained from creating your NFT collection. Upon success, it returns the action ID needed to verify the minting process.

Verify the NFT was Minted Using the Action ID

After minting the NFT, it's important to verify whether the minting process was successful or not. You can check the status of the minting action by using the action ID returned by the previous request.

const verifyMint = async (actionId) => {
  try {
    const response = await fetch(`https://www.crossmint.com/api/2022-06-09/actions/${actionId}`, {
      method: "GET",
      headers: {
        "X-API-KEY": apiKey,
      },
    });

    const data = await response.json();
    if(response.ok) {
      console.log("Mint action status:", data.status);
    } else {
      console.error('Error fetching mint action status:', data);
    }
  } catch (error) {
    console.error("Failed to verify mint action:", error);
  }
};

// Example usage
// Replace 'your-action-id' with the action ID returned from the mintNFT function
verifyMint('your-action-id').then(() => {
  console.log("Verification complete");
});

This code snippet defines a function "verifyMint" that makes a "GET" request to the Crossmint API to check the status of an action using its ID. It's a good practice to verify the minting to ensure the NFT was correctly created on the blockchain.

By following these steps, you can mint an NFT on Optimism using the Crossmint API and ensure its successful creation by verifying the action ID.

Conclusion

In this guide, we've taken you through the steps necessary to create and mint NFTs on the Optimism blockchain, leveraging the capabilities of Crossmint. You learned how to navigate between staging and production environments, generate an API key with the required scopes, and create your own NFT collection. Following that, we guided you through the process of minting an NFT within your collection and verifying its deployment using the action id. This guide aimed to equip you with the knowledge to seamlessly integrate NFT creation and minting into your projects on Optimism, completing the process in a straightforward and efficient manner.

What's Next?

If you are wondering how Crossmint has helped enterprises and brands by powering their NFT Drops, you can click on the link below to read all our Case Studies.

Case Studies
Access 25+ case studies, across industries, from the 30,000+ companies that have used Crossmint’s infrastructure.

If you want to learn how to use the Crossmint to create Web 3 NFT Wallets for your users using their Email Address, please watch the YouTube video below.

Need help?

For support, please join the official Crossmint Discord Server. You can also use Crossmint Help Page for the same.

Sign up developer console
Enabling NFT Cross Chain Payment on Ethereum Layer 2
This guide will show you how to enable cross chain payment on Ethereum Layer 2s such as Base or Optimism with Solana and Ethereum on mainnet.
Create an NFT Storefront with 0 Code | Crossmint
With Crossmint’s No-Code Storefronts, you can start selling NFTs right now, with cross-chain and credit card payments automatically activated
Rohit Ramesh profile image Rohit Ramesh
DevRel Engineer at Crossmint