In today’s tutorial, we’ll show you how to get ERC-721 on-chain metadata using the Moralis NFT API. Thanks to the accessibility of this industry-leading interface, all it requires is a single call to the getNFTMetadata()
endpoint. Here’s an example of what it might look like:
const Moralis = require("moralis").default; const { EvmChain } = require("@moralisweb3/common-evm-utils"); const runApp = async () => { await Moralis.start({ apiKey: "YOUR_API_KEY", // ...and any other configuration }); const address = "0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB"; const chain = EvmChain.ETHEREUM; const tokenId = "3931"; const response = await Moralis.EvmApi.nft.getNFTMetadata({ address, chain, tokenId, }); console.log(response.toJSON()); }; runApp();
All you have to do is add your Moralis API key, customize the address
and tokenId
parameters, and run the script. In return, you’ll get a response containing the NFT metadata looking something like this:
{ "token_address": "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb", "token_id": "3931", "transfer_index": [13868997, 30, 36, 0], "owner_of": "0x1cf2b8c64aed32bff2ae80e701681316d3212afd", "block_number": "13868997", "block_number_minted": "5754322", "token_hash": "3c86855c82470edd82df190019e83f16", "amount": "1", "contract_type": null, "name": "CRYPTOPUNKS", "symbol": "Ͼ", "token_uri": "https://www.larvalabs.com/cryptopunks/details/3931", "metadata": "{\"image\":\"https://www.larvalabs.com/cryptopunks/cryptopunk3931.webp\",\"name\":\"CryptoPunk 3931\",\"attributes\":[\"Vampire Hair\",\"Goat\"],\"description\":\"Male\"}", "last_token_uri_sync": null, "last_metadata_sync": "2022-05-12T18:00:22.340Z", "minter_address": "0xc352b534e8b987e036a93539fd6897f53488e56a" }
When working with Moralis, it doesn’t have to be more complicated than that to get blockchain data. If you’d like to learn more about how the above works, please check out Moralis’ official documentation on how to get NFT metadata. Or, join us in this article as we break down the process in further detail! Before we get going, however, make sure to sign up with Moralis so you can start building your next NFT project quickly and easily!
Overview
Non-fungible tokens (NFTs) have revolutionized the web by introducing unique digital tokens perfectly fit for representing physical and digital assets. A vital part of most NFTs is their metadata, which contains information about each token’s name, description, transaction history, images, etc. Accessing this information is, as you can imagine, an essential part of developing NFT-related platforms. However, querying a blockchain network for the data without the proper tools is easier said than done. Consequently, we’ll take this article to show you how to effortlessly get ERC-721 on-chain metadata using the Moralis NFT API!
However, before we get into the tutorial, we’ll kickstart today’s article by exploring the intricacies of ERC-721 on-chain metadata. From there, we’re going to introduce the Moralis NFT API and explain why this is the best and easiest way to get ahold of the information. We’ll then jump into the tutorial and show you how to get ERC-721 on-chain metadata in three straightforward steps:
- Step 1: Set Up a Moralis Account
- Step 2: Create a Script
- Step 3: Run the Script
If you’re already familiar with the ins and outs of ERC-721 on-chain metadata, then feel free to skip straight into the ”3-Step Tutorial: Get ERC-721 On-Chain Metadata with Moralis” section!
Following the tutorial, we’ll also explore some prominent use cases in which NFT metadata is needed. Lastly, to top things off, we’re going to look at the Moralis Token API, which provides the same ease of use and most of the functionality but for ERC-20 tokens instead!
Nevertheless, with no further ado, let’s kick things off by looking at what ERC-721 metadata is!
What is ERC-721 On-Chain Metadata?
To understand what ERC-721 on-chain metadata entails, we must first explore the intricacies of ERC-721, one of the most popular token standards on Ethereum.
So, what is the ERC-721 token standard?
ERC-721 is an Ethereum-based standard for non-fungible tokens (NFTs), and it’s essentially a template or format developers agree to follow when creating NFTs. Using a standard like this makes the developer’s work significantly more accessible as writing the code becomes easier and more predictable. The ERC-721 standard also ensures that tokens are compatible with the rest of the Web3 ecosystem, including exchanges, crypto wallets, etc.
So, now that we have familiarized ourselves with the standard, let’s dive into the details of ERC-721 on-chain metadata!
NFT metadata is the dataset making up the content of a non-fungible token. This data typically describes an NFT’s properties and characteristics, including its name, description, traits, link to images, transaction history, etc. Furthermore, ERC-721 on-chain metadata is usually specified in a JavaScript Object Notation (JSON) format.
As you can imagine, it’s essential to have easy access to ERC-721 on-chain metadata when building NFT-based Web3 projects. Consequently, in the following section, we’ll introduce Moralis’ industry-leading NFT API, as this is the easiest way to get NFT metadata!
How to Get ERC-721 Metadata?
If you’re looking to build a Web3 project integrating ERC-721 on-chain metadata in any capacity, then you’ll need a quick and easy way to query the blockchain for this information. And your best option is to leverage Moralis’ industry-leading NFT API!
The NFT API from Moralis is the industry’s most powerful tool for developing NFT projects. It supports over three million NFT collections across ten-plus EVM chains, including everything from established NFT projects like CryptoPunks to tokens that dropped just a few seconds ago.
The Moralis NFT API does multiple things to make your work as a developer significantly more accessible. Below, you can find five prominent examples:
- Enriched Metadata: Get access to normalized and fully enriched metadata for both individual tokens and NFT collections.
- Real-Time NFT Transfer Data: Access the latest NFT transfer data for certain NFTs or wallets in real time with a single API call.
- Advanced Spam Detection: Protect your projects from undesirable NFTs using spam indicators.
- On-Chain Pricing Data: Access and incorporate on-chain pricing data in your Web3 projects, including the lowest and last sale prices.
- Alerts: Use the Moralis Steams API to get real-time notifications on NFT trades, mints, and marketplace orders.
To learn more about how this works in practice, join us in the next section as we demo the NFT API by showing you how to get ERC-721 on-chain metadata in three straightforward steps!
With the Moralis NFT API, you can seamlessly query ERC-721 on-chain metadata with a few lines of code and a single API call. If this sounds intriguing and you want to know more, then join us in this section as we show you how to get ERC-721 on-chain metadata in three steps:
- Step 1: Set Up a Moralis Account
- Step 2: Create a Script
- Step 3: Run the Script
However, before we dive into the initial step of this tutorial, you’ll need to deal with a few prerequisites!
Prerequisites
Before you can use the NFT API, you need to take care of a few prerequisites. So, if you haven’t already, you can start by installing and setting up the following:
You also need to set up a project and run the following command in the root folder to install the Moralis SDK:
npm install moralis @moralisweb3/common-evm-utils
That’s it; you’re now ready to start using the Moralis NFT API. So join us in the tutorial’s first step as we briefly show you how to set up a Moralis account!
Step 1: Set Up a Moralis Account
In order to make calls to the Moralis NFT API, you need a Moralis API key. For this, you first need a Moralis account, which you can create entirely for free. To do so, simply click on the ”Start for Free” button at the top right of Moralis’ homepage and follow the instructions:
Once you have an account at your disposal, you’ll find your API key by navigating to the ”Web3 APIs” tab and clicking on the ”Get your API keys” button:
This takes you to the following page, where you can copy your API key:
Now that you have an API key at your disposal, you’re ready to make a call to the NFT API!
Step 2: Create a Script
In this tutorial, we’ll be using JavaScript. As such, you can proceed by creating a new ”index.js” file in your project’s folder and adding the following code:
const Moralis = require("moralis").default; const { EvmChain } = require("@moralisweb3/common-evm-utils"); const runApp = async () => { await Moralis.start({ apiKey: "YOUR_API_KEY", // ...and any other configuration }); const address = "0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB"; const chain = EvmChain.ETHEREUM; const tokenId = "3931"; const response = await Moralis.EvmApi.nft.getNFTMetadata({ address, chain, tokenId, }); console.log(response.toJSON()); }; runApp();
From here, you initially need to replace YOUR_API_KEY
with the key you copied in the first step:
Next, add the address and ID of the NFT you want information about by configuring the address
and tokenId
parameters:
We then pass the address
, chain
, and tokenId
parameters when calling the getNFTMetadata()
endpoint:
That’s it! Thanks to the accessibility of Moralis, all we need is a single call to the NFT API to get the ERC-721 on-chain metadata of a token!
All that remains now is running the script!
Step 3: Run the Script
To execute the script, open a new terminal and run the following command in the root folder of your project:
node index.js
Once you run the command, you’ll get a response containing the ERC-721 on-chain metadata. It will look something like this:
{ "token_address": "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb", "token_id": "3931", "transfer_index": [13868997, 30, 36, 0], "owner_of": "0x1cf2b8c64aed32bff2ae80e701681316d3212afd", "block_number": "13868997", "block_number_minted": "5754322", "token_hash": "3c86855c82470edd82df190019e83f16", "amount": "1", "contract_type": null, "name": "CRYPTOPUNKS", "symbol": "Ͼ", "token_uri": "https://www.larvalabs.com/cryptopunks/details/3931", "metadata": "{\"image\":\"https://www.larvalabs.com/cryptopunks/cryptopunk3931.webp\",\"name\":\"CryptoPunk 3931\",\"attributes\":[\"Vampire Hair\",\"Goat\"],\"description\":\"Male\"}", "last_token_uri_sync": null, "last_metadata_sync": "2022-05-12T18:00:22.340Z", "minter_address": "0xc352b534e8b987e036a93539fd6897f53488e56a" }
Congratulations! You now know how to get the NFT metadata of any ERC-721 token with just a few lines of code!
Use Cases Requiring ERC-721 Metadata
At this point, you might be wondering why you need easy and fast access to ERC-721 on-chain metadata. As such, in the following sections, we’ll explore three prominent ERC-721 metadata use cases. Specifically, we’re going to look closer at the following three examples:
- NFT Marketplaces
- Web3 Wallets
- Web3 Gaming Platforms
So, without further ado, let’s jump into the first use case and look at why you need ERC-721 on-chain metadata when building an NFT marketplace!
NFT Marketplaces
An NFT marketplace is a digital platform or website for storing, displaying, selling, buying, and, sometimes, even minting NFTs. Some prominent examples of existing NFT marketplaces are OpenSea, Rarible, and SuperRare.
To build an NFT marketplace with a compelling user experience, you need a seamless way to fetch and display the metadata of NFTs. The ERC-721 on-chain metadata contains a lot of vital information, including the token’s name, description, creator, images, etc. This is data potential buyers need in order to make informed purchasing decisions.
Web3 Wallets
Web3 wallets are software programs storing digital assets like fungible and non-fungible tokens. Some prominent examples of crypto wallets include MetaMask, Coinbase Wallet, and Trust Wallet.
As you can imagine, when building a Web3 wallet, it’s essential to access the ERC-721 on-chain metadata of the tokens stored in it. This significantly improves the user experience as you can display the token’s properties and characteristics, including the name, description, images, etc., directly in the wallet’s user interface.
Web3 Gaming Platforms
Web3 gaming platforms are blockchain-based games leveraging distributed ledger technology to offer improved security, transparency, democratization, and user control over in-game assets. Some examples of prominent Web3 gaming platforms include Axie Infinity, CryptoKitties, and The Sandbox.
NFTs play essential roles in the Web3 gaming industry as they are perfect for representing in-game characters and items. The metadata of these NFTs usually contains critical information about in-game aesthetics and behavior. As you can imagine, it’s vital that NFT platforms can access this data and incorporate the information into game dynamics.
How to Get ERC-20 Metadata?
Did you know that the NFT API only scratches the surface of what’s possible with Moralis? In addition to the NFT API, Moralis also offers other industry-leading APIs, providing the same accessibility and ease of use for other token types!
For instance, with the Token API, you can effortlessly add real-time token prices, wallet balances, transfers, metadata, and more for ERC-20 tokens to your dapps and Web3 projects. With this tool, it has never been easier to build decentralized exchanges, portfolio trackers, Web3 analytics tools, and Web3 wallets.
If you want to get the metadata of an ERC-20 token, you can simply follow the same three steps from the ”Three-Step Tutorial:…” section. However, instead of calling the getNFTMetadata()
endpoint, you call getTokenMetadata()
. Here’s an example of what the code should look like:
const Moralis = require("moralis").default; const { EvmChain } = require("@moralisweb3/common-evm-utils"); const runApp = async () => { await Moralis.start({ apiKey: "YOUR_API_KEY", // ...and any other configuration }); const addresses = ["0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"]; const chain = EvmChain.ETHEREUM; const response = await Moralis.EvmApi.token.getTokenMetadata({ addresses, chain, }); console.log(response.toJSON()); }; runApp();
To learn more about fetching this, check out the official get ERC-20 metadata by contract documentation!
Summary: How to Get ERC-721 On-Chain Metadata
Today’s article demonstrated how to get ERC-721 on-chain metadata in three straightforward steps using the Moralis NFT API:
- Step 1: Set Up a Moralis Account
- Step 2: Create a Script
- Step 3: Run the Script
As such, you now know how to integrate NFT metadata into your projects!
If you’re now looking to use your skills and build more sophisticated projects, consider checking out more content here on the Web3 blog. For instance, check out our tutorial on how to build a cryptocurrency portfolio dashboard or learn how to build a decentralized cryptocurrency exchange.
You can also explore more tools offered by Moralis. For example, read about our free crypto Price API or check out the testnet faucets page where you can, among other things, find a Linea Goerli faucet, MATIC faucet, and Optimism Goerli faucet.
Also, remember to sign up with Moralis if you want access to industry-leading tools like the NFT API. You can create your account completely for free and start building your next Web3 project immediately!
Read More: moralis.io