How to get the metadata of a specific NFT

What is NFT metadata?

NFT metadata is the set of descriptive information stored off-chain that details the attributes, properties, and additional content associated with an NFT. These details can be an image URL, its name, description, and attributes related to its uniqueness and ownership.

How to do it with Moralis?

As an extensive framework for web3 developers, Moralis provides several EVM-centric APIs, allowing you to deliver projects more quickly.

In our case, Moralis has an NFT API that you can make use of, and they even provide a code sample for the wallet history use-case:

To adapt their code sample, replace the reference to Ethereum with their ID for Chiliz Chain, 0x15b38:

import Moralis from 'moralis';

try {
  await Moralis.start({
    apiKey: "YOUR_API_KEY"
  });

  const response = await Moralis.EvmApi.nft.getNFTMetadata({
    "chain": "0x15b38", // This is for Chiliz Chain Mainnet.
                        // For Spicy Testnet, use "0x15b32".
    "format": "decimal",
    "normalizeMetadata": true,
    "mediaItems": false,
    "address": "0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB",
    "tokenId": "1"
  });

  console.log(response.raw);
} catch (e) {
  console.error(e);
}

How to do it with Tatum?

As a unified framework for building Web3 apps, Tatum provides several EVM-centric APIs, allowing you to deliver projects more quickly.

In our case, Tatum has an NFT submodule to its blockchain abstraction tool that we can make use of, and they even provide a code sample for the metadata-retrieval use-case:

To adapt their code sample, replace the reference to Ethereum with Chiliz Chain:

// yarn add @tatumio/tatum
import {TatumSDK, Network, Chiliz, ResponseDto, NftTokenDetail} from '@tatumio/tatum'

const tatum = await TatumSDK.init<Chiliz>({network: Network.CHILIZ})

const metadata: ResponseDto<NftTokenDetail|null> = await tatum.nft.getNftMetadata({
  tokenAddress: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', // replace with your collection
  tokenId: '1'
})

console.log(metadata.data)

How to do it with thirdweb

Thirdweb is a platform offering tools and infrastructure for building decentralized applications on multiple blockchain networks. It simplifies blockchain integration with SDKs, APIs, and pre-built components.

In our case, thirdweb we can take inspiration from source code hosted on their documentation site:

Here is how we could adapt that into code to retrieve metadata:

const { ThirdwebSDK } = require("@thirdweb-dev/sdk");

const sdk = new ThirdwebSDK("https://rpc.chiliz.com");
const nftContractAddress = "0xYourNFTContractAddress";
const tokenId = "YourTokenID";

async function getNFTMetadata() {
  try {
    const nftContract = await sdk.getNFTCollection(nftContractAddress);
    const nftMetadata = await nftContract.get(tokenId);
    console.log(`Metadata of NFT ${tokenId}:`, nftMetadata);
  } catch (error) {
    console.error("Error fetching NFT metadata:", error);
  }
}

getNFTMetadata();

Last updated