Add Metadata

Following the previous two tutorials, we created our contract and minted an NFT with ID = 1337. However, our minted NFT has no metadata associated with it. Without metadata, an NFT token is nothing but a token with an ID.

Our smart contract implements the ERC721 meta extension, and it has the following method for getting the metadata URI of an NFT token:

    /// @notice A distinct Uniform Resource Identifier (URI) for a given asset.
    /// @dev Throws if `_tokenId` is not a valid NFT. URIs are defined in RFC
    ///  3986. The URI may point to a JSON file that conforms to the "ERC721
    ///  Metadata JSON Schema".
    function tokenURI(uint256 _tokenId) external view returns (string);

The tokenURI method returns a string which is the URI to the metadata of an NFT token. And the metadata is presented in a JSON file.

Prepare Metadata

The metadata of an NFT token is presented in a JSON file. Here is an example:

{
  "description": "My NFT", 
  "image": "https://example.com/1337.png", 
  "name": "Hacker's Token 1337",
  "attributes": [
    {
      "trait_type": "Mouth", 
      "value": "Surprised"
    }, 
    {
      "trait_type": "Level", 
      "value": 5
    }
  ]
}
  • name is the name of this item(i.e, this NFT token), because each NFT token may have a different name.

  • image is the link to the image of this NFT token.

  • description is a short description of this NFT token.

  • and attributes is a list of attributes that this NFT token has.

Apart from the commonly used fields in the above example, some other fields are also supported in the metadata JSON file. You can find more here.

Note the link to the image is also in the metadata JSON. In this example, we are using an ordinary (web2) link. However, to make it more decentralized, you can upload your images to IPFS and use the IPFS CIDs with a gateway.

You should create one metadata JSON for each of your NFT tokens. Let's assume you are hosting all the metadata JSON files on the host of my-nft-metadata.com , and the metadata JSON of each NFT token should have the following format:

https://my-nft-metadata.com/1337
  • https://my-nft-metadata.com is called the base URI.

  • 1337 is the ID of the NFT token

If you upload all your metadata JSON files to IPFS, you should add the whole directory that includes all the JSON files. Then, all the JSON files can be visited with the same base URI. For example, all the metadata JSON files of the BAYC collection share the same base URI.

Set The Base URI

After uploading all the metadata JSON files somewhere and hosting them with the same base URI, we can set the base URI of the contract. Again, we can use the cast command:

FOUNDRY_PROFILE=dcc_testnet \
        cast send  --private-key=XXXXXXXXXX  \
        --legacy \
        0xd382De234f8d3B35823f1DB54C85cE6498a9DbC84c \
        'setBaseURI(string)' "https://my-nft-metadata.com/"
  • XXXXXXXXX is your private key as usual

  • 0xd382De234f8d3B353f1DB54C85cE6498a9DbC84c is the address of our NFT contract.

  • And we use cast to call the setBaseURI method, and change the base URI to "https://my-nft-metadata.com"

Let's verify that our base URI works as expected. We use cast to get the tokenURI of the token of 1337:

FOUNDRY_PROFILE=dcc_testnet \
    cast call 0xd382De234f8d3B353f1DB54C85cE6498a9DbC84c \
    'tokenURI(uint256)(string)' 1337 

You should see the following URI printed out:

https://my-nft-metadata.com/1337

Last updated