How to Get NFT Metadata

How to Get NFT Metadata

Kamau Joseph

Kamau Joseph

Chainbase Intern

Outline

  1. Introduction
  2. Tools You Need to Work with Chainbase
  3. Set up a Free Account at Chainbase
  4. Write a Script using Chainbase API
  5. Print NFT Metadata
  6. Conclusion
  7. FAQs

1. Introduction

In the world of non-fungible tokens (NFTs), metadata plays a crucial role by providing additional information about an NFT, including its name, description, image, and other attributes. This metadata is essential for marketplaces and collectors who use it to display, search, and verify the authenticity and ownership of NFTs. In this article, we will explore the process of obtaining NFT metadata using the Chainbase platform and its API.

2. Tools You Need to Work with Chainbase

Before we dive into the process of obtaining NFT metadata, let's ensure we have the necessary tools in place:

  1. A free account at Chainbase with an API key: Register for a free account at Chainbase's website and obtain your API key. This key will grant you access to the Chainbase API and its various features.
  2. An IDE: To write code examples and interact with the Chainbase API, we recommend using an Integrated Development Environment (IDE) such as Visual Studio Code (VS Code). It provides a user-friendly interface for coding and executing JavaScript examples.
  3. An NFT contract address: To retrieve the metadata of a specific NFT, you'll need its contract address. This address uniquely identifies the NFT and allows Chainbase to fetch its metadata.

3. Set up a Free Account at Chainbase

To maximize the benefits of Chainbase's capabilities, it's advisable to create a free account on their platform. Here's how you can do it:

  1. Visit the Chainbase website and sign up for a new account.
  2. After logging in, navigate to the dashboard to get an overview of your account.
  3. Create a new project in the console, which will generate an API key for you to use.
  4. Make sure to securely store your API key as it will be needed to make API requests in the following steps.

4. Write a Script using Chainbase API

Now that you have your Chainbase account set up and the required tools in place, let's write a script to fetch the NFT metadata using the Chainbase API. We'll provide examples using both the fetch and axios libraries in JavaScript.

Using fetch in JavaScript:

network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
contract_addr = '0xed5af388653567af2f388e6224dc7c4b3241c544'; // Take the contract address of Azuki as an example.
token_id = '1'; // Token id should be in hex or num string. Here we take 1 as example.

fetch(`https://api.chainbase.online/v1/nft/metadata?chain_id=${network_id}&contract_address=${contract_addr}&token_id=${token_id}`, {
    method: 'GET',
    headers: {
        'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
        'accept': 'application/json'
    }
}).then(response => response.json())
    .then(data => console.log(data.data))
    .catch(error => console.error(error));

Using axios in JavaScript, you need to installĀ axiosĀ usingĀ npm install axios --saveĀ in the terminal first.

network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
contract_addr = '0xed5af388653567af2f388e6224dc7c4b3241c544'; // Take the contract address of Azuki as an example.
token_id = '1'; // Token id should be in hex or num string. Here we take 1 as example.

const axios = require('axios');
const options = {
    url: `https://api.chainbase.online/v1/nft/metadata?chain_id=${network_id}&contract_address=${contract_addr}&token_id=${token_id}`,
    method: 'GET',
    headers: {
        'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
        'accept': 'application/json'
    }
};
axios(options)
    .then(response => console.log(response.data.data))
    .catch(error => console.log(error));

Make sure to replace CHAINBASE_API_KEY with your actual Chainbase API key obtained from your account.

5. Print NFT Metadata

In this step, we'll retrieve and print the NFT metadata using the Chainbase API. Follow these instructions:

  1. Save the script you wrote in Step 2 to a file (e.g., getNFTMetadata.js).
  2. Open the terminal and navigate to the directory where you saved the script.
  3. Run command Ā node <filename>.js to execute the script and obtain the NFT metadata:
{
  "contract_address": "0xed5af388653567af2f388e6224dc7c4b3241c544",
  "name": "Azuki",
  "symbol": "AZUKI",
  "owner": "0xC8967D1537F7B995607A1DEa2B0C06E18A9756a2",
  "token_id": "0x1",
  "erc_type": "ERC721",
  "image_uri": "https://ikzttp.mypinata.cloud/ipfs/QmYDvPAXtiJg7s8JdRBSLWdgSphQdac8j1YuQNNxcGE1hg/1.png",
  "mint_time": "2022-01-12T04:17:28Z",
  "mint_transaction_hash": "0xc208fdb2f133bda64522fececd6518a565aaa6e8801b0a776f2f93c922fe9420",
  "token_uri": "https://ikzttp.mypinata.cloud/ipfs/QmQFkLSQysj94s5GvTHPyzTxrawwtjgiiYS2TBLgrvw8CW/1",
  "metadata": {
    "name": "Azuki #1",
    "image": "https://ikzttp.mypinata.cloud/ipfs/QmYDvPAXtiJg7s8JdRBSLWdgSphQdac8j1YuQNNxcGE1hg/1.png",
    "attributes": [
      {
        "trait_type": "Type",
        "value": "Human"
      },
      {
        "trait_type": "Hair",
        "value": "Pink Hairband"
      },
      {
        "trait_type": "Clothing",
        "value": "White Qipao with Fur"
      },
      {
        "trait_type": "Eyes",
        "value": "Daydreaming"
      },
      {
        "trait_type": "Mouth",
        "value": "Lipstick"
      },
      {
        "trait_type": "Offhand",
        "value": "Gloves"
      },
      {
        "trait_type": "Background",
        "value": "Off White D"
      }
    ]
  },
  "traits": [
    {
      "trait_type": "Type",
      "value": "Human"
    },
    {
      "trait_type": "Hair",
      "value": "Pink Hairband"
    },
    {
      "trait_type": "Clothing",
      "value": "White Qipao with Fur"
    },
    {
      "trait_type": "Eyes",
      "value": "Daydreaming"
    },
    {
      "trait_type": "Mouth",
      "value": "Lipstick"
    },
    {
      "trait_type": "Offhand",
      "value": "Gloves"
    },
    {
      "trait_type": "Background",
      "value": "Off White D"
    }
  ]
}

The script will send a request to the Chainbase API and retrieve the metadata for the specified NFT. The response will contain detailed information, including the NFT's name, symbol, owner, image URI, minting details, and additional attributes. Review the printed metadata to gain insights into the NFT's characteristics.

6. Conclusion

Obtaining NFT metadata is crucial for understanding an NFT's attributes and ensuring its authenticity and ownership. By leveraging the Chainbase platform and its API, you can retrieve detailed metadata for specific NFTs, empowering marketplaces, collectors, and enthusiasts to explore and engage with NFTs effectively.

Get started with Chainbase today and unlock the potential of NFT metadata!


7. FAQs

  1. What is NFT metadata? NFT metadata refers to additional information associated with an NFT, such as its name, description, image, and attributes. It provides crucial details that enhance the understanding and value of an NFT.
  2. How does metadata verify NFT authenticity? Metadata contains information unique to an NFT, such as the contract address, token ID, and minting transaction hash. These details can be cross-referenced to validate the authenticity and ownership of an NFT.
  3. Can NFT metadata be modified? In some cases, NFT metadata can be updated by the NFT creator or the platform hosting the NFT. However, altering metadata after minting may affect its authenticity and perceived value.
  4. Are there standardized formats for NFT metadata? Yes, there are various metadata standards for NFTs, such as ERC721 and ERC1155. These standards define the structure and properties of NFT metadata, ensuring compatibility and interoperability across platforms.
  5. What role does NFT metadata play in marketplaces? NFT metadata plays a crucial role in marketplaces by providing detailed information about the NFT, enabling users to search, discover, and evaluate NFTs based on their attributes. It enhances the overall user experience and facilitates informed buying decisions.

Now that you understand how to obtain NFT metadata using Chainbase, you can leverage this knowledge to explore and interact with the exciting world of NFTs. Dive into the vast ecosystem, discover unique digital assets, and experience the creativity and innovation that NFTs have to offer.

Happy exploring!


About Chainbase

Chainbase is an all-in-one data infrastructure for Web3 that allows you to index, transform, and use on-chain data at scale. By leveraging enriched on-chain data and streaming computing technologies across one data infrastructure, Chainbase automates the indexing and querying of blockchain data, enabling developers to accomplish more with less effort.

Want to learn more about Chainbase?

Visit our websiteĀ chainbase.comĀ Sign up for aĀ free account, and Check out ourĀ documentation.

Website|Blog|Twitter|Discord|Link3