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);
tokenURImethod returns a string which is the URI to the metadata of an NFT token. And the metadata is presented in a JSON file.
The metadata of an NFT token is presented in a JSON file. Here is an example:
"description": "My NFT",
"name": "Hacker's Token 1337",
nameis the name of this item(i.e, this NFT token), because each NFT token may have a different name.
imageis the link to the image of this NFT token.
descriptionis a short description of this NFT token.
attributesis 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.comis 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.
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 send --private-key=XXXXXXXXXX \
XXXXXXXXXis your private key as usual
0xd382De234f8d3B353f1DB54C85cE6498a9DbC84cis the address of our NFT contract.
- And we use cast to call the
setBaseURImethod, and change the base URI to "https://my-nft-metadata.com"
Let's verify that our base URI works as expected. We use
castto get the
tokenURIof the token of
cast call 0xd382De234f8d3B353f1DB54C85cE6498a9DbC84c \
You should see the following URI printed out: