date
Jul 9, 2024
slug
create-solana-nft-with-metaplex
status
Published
tags
Solana
summary
Non-Fungible Tokens (NFTs) are represented on Solana as SPL Tokens with an associated metadata account, 0 decimals, and a maximum supply of 1 • Metaplex offers a collection of tools that simplify the creation and distribution of NFTs on the Solana blockchain • The Token Metadata program standardizes the process of attaching metadata to SPL Tokens • The Metaplex SDK is a tool that offers user-friendly APIs to assist developers in utilizing the onchain tools provided by Metaplex • The Candy Machine program is an NFT distribution tool used to create and mint NFTs from a collection • Sugar CLI is a tool that simplifies the process of uploading media/metadata files and creating a Candy Machine for a collection
type
Post

NFTs on Solana

A Solana NFT is a non-divisible token with associated metadata. Further, the token's mint has a maximum supply of 1.
In other words, an NFT is a standard token from the Token Program but differs from what you might think of as "standard tokens" in that it:
  1. Has 0 decimals so it cannot be divided into parts
  1. Comes from a token mint with the supply of 1 so only 1 of these tokens exists
  1. Comes from a token mint whose authority is set to null (to ensure that the supply never changes)
  1. Has an associated account that stores metadata
While the first three points are features that can be achieved with the SPL Token Program, the associated metadata requires some additional functionality.
Typically, an NFT’s metadata has both an onchain and off-chain component. See the diagram below:

Metaplex

Metaplex is an organization that provides a suite of tools, like the Metaplex SDK, that simplify the creation and distribution of NFTs on the Solana blockchain. These tools cater to a wide range of use cases and allow you to easily manage the entire NFT process of creating and minting an NFT collection.
More specifically, the Metaplex SDK is designed to assist developers in utilizing Metaplex's onchain tools. It offers a user-friendly API that focuses on popular use cases and allows for easy integration with third-party plugins. To learn more about the capabilities of the Metaplex SDK, refer to the README.
One of Metaplex's essential programs is the Token Metadata program. This program standardizes the process of attaching metadata to SPL Tokens. When creating an NFT with Metaplex, the Token Metadata program creates a metadata account using a Program Derived Address (PDA) with the token mint as a seed. This allows the metadata account for any NFT to be located deterministically using the address of the token mint. To learn more about the Token Metadata program, refer to the Metaplex documentation.
The following sections cover the basics of using the Metaplex SDK to prepare assets, create NFTs, update NFTs, and associate an NFT with a broader collection.

Metaplex instance

A Metaplex instance serves as the entry point for accessing the Metaplex SDK APIs. This instance accepts a connection used to communicate with the cluster. Additionally, developers can customize the SDK's interactions by specifying an "Identity Driver" and a "Storage Driver".
The Identity Driver is effectively a keypair that can be used to sign transactions, a requirement when creating an NFT. The Storage Driver is used to specify the storage service you want to use for uploading assets. The bundlrStorage driver is the default option and it uploads assets to Arweave, a permanent and decentralized storage service.
Below is an example of setting up the Metaplex instance for devnet.
 
Upload assets

Upload metadata

After uploading an image, it's time to upload the off-chain JSON metadata using the nfts().uploadMetadata function. This will return a URI where the JSON metadata is stored.
Remember, the off-chain portion of the metadata includes things like the image URI as well as additional information like the name and description of the NFT. While you can technically include anything you'd like in this JSON object, in most cases, you should follow the NFT standard to ensure compatibility with wallets, programs, and applications.
To create the metadata, use the SDK's uploadMetadata method. This method accepts a metadata object and returns a URI that points to the uploaded metadata.

Create NFT

After uploading the NFT's metadata, you can finally create the NFT on the network. The Metaplex SDK's create method allows you to create a new NFT with minimal configuration. This method will handle the creation of the mint account, token account, metadata account, and master edition account for you. The data provided to this method will represent the onchain portion of the NFT metadata. You can explore the SDK to see all the other input that can be optionally provided to this method.

Update NFT

If you've left isMutable as true, you may end up having a reason to update your NFT's metadata. The SDK's update method allows you to update both the onchain and off-chain portions of the NFT's metadata. To update the off-chain metadata, you'll need to repeat the steps of uploading a new image and metadata URI as outlined in the previous steps, then provide the new metadata URI to this method. This will change the URI that the onchain metadata points to, effectively updating the off-chain metadata as well.

Add NFT to Collection

Certified Collection is an NFT that individual NFTs can belong to. Think of a large NFT collection like Solana Monkey Business. If you look at an individual NFT's Metadata you will see a collection field with a key that points to the Certified Collection NFT. Simply put, NFTs that are part of a collection are associated with another NFT representing the collection itself.
To add an NFT to a collection, first, the Collection NFT has to be created. The process is the same as before, except you'll include one additional field on our NFT Metadata: isCollection. This field tells the token program that this NFT is a Collection NFT.

“Success is not final, failure is not fatal: it is the courage to continue that counts.“

Winston Churchill


© Hiếu Trần 2020 - 2025

Tip: Use console.table() for better array logging!
Anime Character