November 20, 2022

NFT Collection Data – How to Get NFT Collection Data

Table of Contents

Most non-fungible tokens belong to an NFT collection. As such, it can be hugely beneficial to know how to get NFT collection data if you’re planning on building NFT-related dapps. Fortunately, you do not have to build an infrastructure that will enable you to do that from scratch. Instead, here are two code snippets that can help you get all the NFT collection data you need without breaking a sweat:

const response = await Moralis.EvmApi.nft.getNFTContractMetadata({address,});
const response = await Moralis.EvmApi.nft.getContractNFTs({address,});

The “getNFTContractMetadata” and “getContractNFTs” endpoints are a part of Moralis’ ultimate NFT API. If you’ve used Moralis before and are proficient in JavaScript, you most likely know how to utilize the above lines of code to get NFT collection data. On the other hand, this may be your first rodeo with Moralis. In that case, you should complete this step-by-step tutorial. You just need to create your free Moralis account and follow our lead! 

Unlock the Power of Blockchain with Moralis

Overview

NFTs continue to be one of the most popular blockchain use cases. They offer countless opportunities to create decentralized applications (dapps) that in some way incorporate NFTs. This can be NFT explorers, NFT trackers, NFT-gated websites, Web3 games, etc. Moreover, whenever you want to build NFT-related dapps, the ability to get NFT collection data makes things a lot simpler, and using the above two snippets of code is the best way to go.

User looking at a digital painting that says NFT Collection Data.

Moving forward, we’ll first explain what NFT collection data is and what type of data an NFT collection store. Then, we’ll look closely at the ultimate data tool for NFT collections – the Moralis Web3 API. This is where you’ll get better acquainted with the Moralis NFT API and the “getNFTContractMetadata” and “getContractNFTs” endpoints. While these are the two core tools to get NFT collection data, you can take things further with Moralis’ Web3 infrastructure solution. For instance, you can use Web3 streams, which enable you to create Ethereum webhooks to effortlessly stream on-chain events directly to your backend. Moreover, you’ll also want to equip your dapp with Web3 authentication using Moralis’ Auth API

Once you know what parameters the above two endpoints take in and how to test them, you’ll be ready to take on today’s tutorial. Of course, you can combine the two endpoints in the same script. However, for the sake of clarity, we’ll show you how to work with the “getContractNFTs” endpoint. After all, the latter provides you with more details. 

Two developers using various tools in their workspace.

What is NFT Collection Data?

NFT collection data refer to different pieces of information about a collection. However, we often refer to details regarding NFTs in a particular collection as NFT collection data. As such, NFT collection data typically also refers to NFT metadata. Moreover, it’s worth pointing out that NFTs can be assigned to collections (and often are), but this is not essential. After all, an NFT can be completely independent. However, NFT creators quite often assign a larger amount of tokens to a single collection. A great example is the CryptoPunks collection, which includes close to 10,000 NFTs. Ultimately, in order to know what NFT collection data is, you need to properly understand what NFTs and NFT metadata are. 

CryptoPunk NFT Collection Data

What are Non-Fungible Tokens?

Non-fungible tokens, or NFTs, are unique non-interchangeable crypto assets. Each NFT has its unique token ID (an on-chain signature), which makes these tokens ideal for representing unique digital or physical items. This is why digital art and digital collectibles tend to be the most common examples of NFT utility. That said, you should keep in mind that NFTs are already revolutionizing many industries (certification, ownership proofs, ticketing, gaming, etc.).  

Furthermore, you may know that devs create (mint) NFTs using NFT smart contracts. The latter are on-chain pieces of software that incorporate all the required and optional details about the NFT they focus on. Moreover, the same smart contract is behind all NFTs in one collection. Hence, to get NFTs from a collection, you need to focus on the suitable smart contract for that collection.

NFT metadata code outlined in a graph.

What is NFT Metadata?

NFT metadata provides information about the data related to an NFT. Moreover, looking at the above image should give you a solid idea of what kind of details NFT metadata includes. It’s also important to note that the NFT metadata itself is not stored on the blockchain, nor are the NFT-representing files (often JPEGs but can be any other file formats). Instead, devs use cloud storage solutions (preferably decentralized ones) to store these files and the metadata files. The latter includes the NFT-representing files’ location links (URLs). That is why the skill of getting NFT metadata also enables you to get the NFT-representing files. 

Furthermore, metadata files usually come in JSON format. The links pointing to these files (once stored online) are referred to as URIs. Note that the exact details included in NFT metadata vary. However, you often know what you’re dealing with since NFT standards are set in place (ERC-721, ERC-1155, SPL, etc.). For example, in the case of ERC-721 or ERC-1155 standards, metadata usually includes a description, a name, an NFT-representing file destination, and optional attributes or traits. To learn more about attributes, check out our “How to Add Attributes to NFT Metadata” article!

Hologram showing a digital NFT.

What Data is Stored in an NFT Collection?

NFT collections are minted and governed by smart contracts. As such, the details in an NFT collection are the details that the collection’s smart contract holds. These most commonly include the total number of NFTs in a collection, the collection name, the collection symbol, the base token URI, and all the details of individual NFTs in that collection. Also, some of the most important NFT details come in the form of NFT metadata (URIs). Moreover, the latter includes NFT-representing files and other required and optional details, as explained above. However, the best way to see what type of data is stored in an NFT collection is to use the “getNFTContractMetadata” and “getContractNFTs” endpoints.

Moralis' homepage with an arrow pointing at the Start for Free button.

Ultimate Data Tool for NFT Collections

Moralis is the ultimate data tool for NFT collections, especially thanks to the “getNFTContractMetadata” and “getContractNFTs” NFT API endpoints. As such, we encourage you to take a closer look at each of the two endpoints by using the Moralis documentation. So, let’s start by looking at the “getNFTContractMetadata” endpoint and its documentation page in the next section!

Exploring the “getNFTContractMetadata” NFT API Endpoint 

Here’s the screenshot of the get collection metadata endpoint‘s documentation page:

Documentation page showing the code to get NFT collection data, which is the get collection metadata endpoint.

As you can see in the image above, this endpoint fetches the collection (contract) level metadata for a given smart contract. To do this, it requires an address of the NFT contract of interest. Moreover, this makes the “address” parameter essential. In addition, the “getNFTContractMetadata” endpoint also enables you to select which chain to query using the “chain” parameter.

You can use the top-right portion of the above page to select the programming language you want to use and try the code as illustrated in the above image.    

Exploring the “getContractNFTs” NFT API Endpoint

Unlike the “getNFTContractMetadata” endpoint that fetches the collection-level metadata, “getContractNFTs” gets significantly more details. It also fetches the metadata for all NFTs in the targeted collection and offers additional optional parameters. After finishing this article, make sure to check out one of our other articles explaining how to get all NFTs from contract. That said, let’s now make sure to explore the get NFTs by contract documentation page as well:

Showing all the information on the get NFTs by contract documentation page, including code snippets.

As explained in the previous section, you can use the documentation page for getting NFTs by contract to take the “getContractNFTs” for a test spin and copy the required lines of code for various programming languages.

Nonetheless, here are all the parameters that this endpoint takes in:

  • address – This parameter serves to provide an NFT smart contract address and is the only required parameter. 
  • chain – You can use this parameter to determine which programmable chain to target (Moralis supports all leading Web3 networks).  
  • format – This parameter lets you determine what type of token ID format you want to work with (decimal or HEX).
  • limit – You can use this parameter to set the desired page size of the result.
  • totalRange – This parameter lets you determine the number of subranges to split the results into.
  • range – You can use this parameter to set the desired subrange to query.
  • cursor – This parameter enables you to get to the next page when you have more than 100 NFTs or want to display even fewer NFTs per page.
  • normalizeMetadata” – You can use this parameter if you want to get normalized metadata. In case you select “true”, the endpoint returns metadata in a standardized structure in a new object called “normalized_metadata“.

How to Get NFT Collection Data with Moralis

Now that you know the gist of both endpoints that enable you to fetch NFT collection data, it’s time for today’s tutorial. Since “getContractNFTs” provides you with more details, we’ll focus our tutorial on this endpoint. However, the steps to use “getNFTContractMetadata” are basically the same. As such, you’ll know exactly how to utilize it after following our lead below. 

First things first, make sure to complete three prerequisites:

  • Prepare your favorite code editor or IDE. We tend to use Visual Studio Code (VSC).
  • Install Node v.14 or higher.
  • Set in place your favorite package manager (”npm, yarn, or pnpm”).
Wizard pointing at a billboard that states How to Get NFT Collection Data.

Step #1 – Setting Up Moralis

You need a Moralis account to access the power of the ultimate Web3 API. So, in case you haven’t done so yet, create your free Moralis account. Then, you’ll be able to access your Moralis admin area and obtain your Web3 API key:

Step 1, click on the Web3 APIs button. Step 2, copy the API key.

For now, hold on to your API key, and focus on installing the Moralis SDK in your project. A single command will do the trick; just make sure to use the command that matches your package manager:

npm install moralis
yarn add moralis
pnpm add moralis

Step #2: Using an Example “Index.js” Script

Now that you have access to Moralis, it’s time to create an example script that adequately incorporates the code lines presented at the outset of this article. Of course, you can simply copy-paste the lines of code below from our example “index.js” script:

const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/evm-utils");

const runApp = async () => {
  await Moralis.start({
    apiKey: "YOUR_API_KEY",
    // ...and any other configuration
  });
  
  const address = "0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB";

    const chain = EvmChain.ETHEREUM;
  
  const response = await Moralis.EvmApi.nft.getContractNFTs({
    address,
    chain,
  });
  
  console.log(response);
}

runApp();

Note: If you were to focus on working with the “getNFTContractMetadata” endpoint, you could use the exact same script and simply replace “getContractNFTs” with “getNFTContractMetadata”.

Looking at the above lines of code, you can see “EvmChain.ETHEREUM“. We use the latter to define the chain that we want to query. Of course, we could replace “ETHEREUM” with other supported chains or add additional chains to query multiple chains. Nonetheless, you must not forget to replace the “YOUR_API_KEY” placeholder with your Web3 API key obtained in step one.

Note: Visit the documentation page to get all the NFTs from a collection to copy the TypeScript version of the above “index” script.

Step #3: Execute the Program

At this point, you should have the above lines of code in place, including your actual Web3 API key. Hence, you are ready to execute the program and explore the results – all the NFT collection data. Thus, enter the following command in your terminal:

node index.js

Note: In case you are using the “index.ts” script, you will need to use the “node index.ts” command instead of the above one.

Finally, you can explore the results in your terminal. If you used the “0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB” address (as given above), you should be looking at this response:

{
  "total": 9991,
  "page": 0,
  "page_size": 100,
  "cursor": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ3aGVyZSI6eyJ0b2tlbl9hZGRyZXNzIjoiMHhiNDdlM2NkODM3ZGRmOGU0YzU3ZjA1ZDcwYWI4NjVkZTZlMTkzYmJiIn0sInRva2VuX2FkZHJlc3MiOiIweGI0N2UzY2Q4MzdkZGY4ZTRjNTdmMDVkNzBhYjg2NWRlNmUxOTNiYmIiLCJsaW1pdCI6MTAwLCJvZmZzZXQiOjAsIm9yZGVyIjpbXSwicGFnZSI6MSwia2V5IjoiZmQ5MmE0ODg3MmE4NjIwZTFlNmU0NTk3ODZkMTExYWYiLCJ0b3RhbCI6OTk5MSwiaWF0IjoxNjY3ODA4NzcxfQ.9qXwHyyUKJkzrub-ze-q2gm8dC0dy-jvgF0CJrm5piY",
  "result": [
    {
      "token_address": "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb",
      "token_id": "9082",
      "amount": "1",
      "token_hash": "fffdaced3ddfb220d9124289a518bb97",
      "block_number_minted": "12021693",
      "updated_at": null,
      "contract_type": null,
      "name": "CRYPTOPUNKS",
      "symbol": "Ͼ",
      "token_uri": "https://www.larvalabs.com/cryptopunks/details/9082",
      "metadata": "{\"image\":\"https://www.larvalabs.com/cryptopunks/cryptopunk9082.png\",\"name\":\"CryptoPunk 9082\",\"attributes\":[\"Bandana\",\"Big Shades\",\"Shadow Beard\"],\"description\":\"Male\"}",
      "last_token_uri_sync": null,
      "last_metadata_sync": "2022-10-05T17:55:52.262Z",
      "minter_address": "0xc352b534e8b987e036a93539fd6897f53488e56a"
    },
    {
      "token_address": "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb",
      "token_id": "4799",
      "amount": "1",
      "token_hash": "fff8ff007820e85a75c545389d5a88d4",
      "block_number_minted": "11637283",
      "updated_at": null,
      "contract_type": null,
      "name": "CRYPTOPUNKS",
      "symbol": "Ͼ",
      "token_uri": "https://www.larvalabs.com/cryptopunks/details/4799",
      "metadata": "{\"image\":\"https://www.larvalabs.com/cryptopunks/cryptopunk4799.png\",\"name\":\"CryptoPunk 4799\",\"attributes\":[\"Blue Eye Shadow\",\"Black Lipstick\",\"Straight Hair Blonde\",\"Earring\"],\"description\":\"Female\"}",
      "last_token_uri_sync": null,
      "last_metadata_sync": "2022-07-17T17:25:24.644Z",
      "minter_address": "0xc352b534e8b987e036a93539fd6897f53488e56a"
    },
    {
      "token_address": "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb",
      "token_id": "47",
      "amount": "1",
      "token_hash": "fff3657368693f145d0ad29453f6cd4d",
      "block_number_minted": "3918216",
      "updated_at": null,
      "contract_type": null,
      "name": "CRYPTOPUNKS",
      "symbol": "Ͼ",
      "token_uri": "https://www.larvalabs.com/cryptopunks/details/47",
      "metadata": "{\"image\":\"https://www.larvalabs.com/cryptopunks/cryptopunk047.png\",\"name\":\"CryptoPunk 047\",\"attributes\":[\"Knitted Cap\"],\"description\":\"Male\"}",
      "last_token_uri_sync": null,
      "last_metadata_sync": "2022-07-24T12:12:11.930Z",
      "minter_address": "0xc352b534e8b987e036a93539fd6897f53488e56a"
    }
  ],
  "status": "SYNCED"
}

After exploring the results, you can see firsthand all the NFT collection data that the “getContractNFTs” endpoint provides you with. For instance, it even returns the total number of NFTs in the collection, which is “9991” for the above example.

NFT Collection Data – How to Get NFT Collection Data – Summary

In today’s article, you learned how to get NFT collection data the easy way. Along the way, you also learned what NFT collection data is. That said, to complete this tutorial, you only needed a free Moralis account to get your Web3 API key, which is the gateway to using all of Moralis’ Web3 API endpoints. When it comes to fetching NFT collection data, the “getNFTContractMetadata” and “getContractNFTs” endpoints are the ultimate tools. Furthermore, you had an opportunity to get acquainted with these tools and even put them to use in this article.

Knowing how to get NFT collection data can be beneficial. After all, NFTs play an important role in many dapps. With the knowledge obtained herein, you can build NFT portfolio trackers, NFT-gated websites, DAOs, social graphs, and many other types of NFT dapps. Hence, we urge you to use the Moralis documentation to create your first dapp. The best thing is that you can use your favorite legacy dev tools to do so. 

On the other hand, you may be interested in exploring other blockchain development topics. In that case, make sure to visit the Moralis YouTube channel and the Moralis blog. For instance, some of the latest topics teach you how to get wallet balance, how to get NFT metadata, integrate the WalletConnect modal and QR code, and much more.

Of course, you can also take a more professional approach to your crypto education and become blockchain-certified. In that case, you should enroll in Moralis Academy and take the first step towards becoming a Web3 expert. The “Blockchain & Bitcoin 101” course is a great place to start.   

NFT API
Unlock the full potential of your NFT projects with this industry-leading NFT API! Fast, easy, and free.
NFT API
Related Articles
January 26, 2023

Web3 Libraries – List of Web3 Libraries for Devs in 2023

November 14, 2022

How to Get All NFTs from a Collection in 3 Steps

January 24, 2024

Understand Why Crypto is Up Or Down with a Crypto Price Movement API

December 14, 2023

Holesky Faucet – How to Get Free Holesky Testnet Funds 

March 3, 2023

Optimism Goerli Faucet – How to Get Free Optimism Testnet Funds

March 1, 2023

How to Build a Block Explorer

December 11, 2022

Web3.js vs Ethers.js – The Full 2023 Guide to ETH JavaScript Libraries

March 10, 2023

How to Get Real-Time Crypto Wallet Balance Updates