November 14, 2022

How to Get All NFTs from a Collection in 3 Steps

Table of Contents

Thanks to Moralis’ ultimate NFT API, any dev can get all NFTs from a collection. Install the Moralis SDK and use the “getContractNFTs NFT API endpoint; it’s as simple as that! Essentially, with the following code snippet, you can cover the core of fetching non-fungible tokens from a collection:

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

In case you are already familiar with Moralis and how it enables you to bridge the development gap between Web2 and Web3, you probably have several ideas for utilizing the above lines of code. However, if you are new to Web3 development, make sure to complete today’s tutorial. By doing so, you’ll learn how to get all the NFTs from a collection easily. Just create your free Moralis account and follow our lead. 

Unlock the Power of Blockchain - Get All NFTs from Contract by Signing Up with Moralis.

Overview

The bear market is the best time to build dapps (decentralized applications). Considering that NFTs continue to be one of the most popular blockchain use cases, now’s the right time to learn how to work with the ultimate NFT API.  

A hologram displaying a text that says how to get all NFTs from collection.

As pointed out earlier, the “getContractNFTs” API endpoint will be the focal point of today’s article. The upcoming tutorial will revolve around properly incorporating the above-presented snippet of code. Ultimately, we’ll teach you how to use JavaScript (JS) or TypeScript (TS) to get all NFTs from a collection without breaking a sweat. Fortunately, this is a three-step process that shouldn’t take you more than a couple of minutes. Especially since you’ll be able to simply copy and paste our lines of cod, and once you get NFTs from a collection, you’ll get to use your terminal to explore the results. 

In addition, we decided to help you take things to the next level. As such, you’ll also be able to follow our lead and create a simple NFT explorer dapp. That way, you’ll see how to neatly utilize the results provided by the “getContractNFTs” endpoint. 

However, before we invite you to roll up your sleeves and follow our instructions, we need to ensure you know the basics. Hence, we’ll first explain what NFTs and NFT collections are. We’ll also take a closer look at the ultimate development tool for NFT collections – the Moralis NFT API.

NFT Collections

NFTs and Collections – What are They?

NFTs (non-fungible tokens) are special kinds of crypto assets. In line with the meaning of “non-fungible”, these types of tokens are not interchangeable. This means that each NFT has its unique token ID (an on-chain signature). Hence, NFTs are designed to represent unique digital or physical items. As such, it makes sense that digital art and digital collectibles tend to be the most common utility examples of non-fungible tokens. However, NFTs are already revolutionizing many other industries that require any kind of ownership proofing or certification issuing. Nonetheless, these crypto assets are also poised to change the gaming industry. After all, we are already seeing various Web3 gaming projects that empower players to truly own their in-game assets.  

Furthermore, NFTs are often assigned to collections. This is not essential, as an NFT can be completely independent. However, more often than not, the creators of non-fungible tokens tend to assign a larger amount of tokens to a single collection. Of course, the sizes of collections also vary greatly from project to project. In digital art, collections tend to include fewer NFTs. On the other hand, when it comes to digital collectibles, a single collection can often include several thousand NFTs. For instance, the CryptoPunks collection includes nearly 10,000 NFTs. 

CryptoPunks Collection

Moreover, it’s worth pointing out that devs create (mint) NFTs using smart contracts. These on-chain pieces of software include all the details about the NFT they focus on. Plus, contracts include predefined actions that will be executed when specific predefined conditions are met. Hence, they also govern transactions related to their NFTs. Also, note that there is the same smart contract behind all the NFTs in one collection. Thus, we get NFTs from a collection by focusing on the right smart contract.   

Ultimate Development Tool for NFT Collections

Now that you know what NFTs and NFT collections are, it’s time we take a closer look at the tool that lets you get all NFTs from a collection. By this point, you know that if you want to learn how many NFTs in a collection there are, you can use the Moralis “getContractNFTs” endpoint. Of course, there are a lot more details that this excellent tool can provide you with for a specific collection.

You can even expand your toolset by utilizing other NFT API endpoints in Moralis’ arsenal. Furthermore, when building dapps, you’ll likely want to use other Moralis’ Web3 APIs, starting with its Web3 Auth API. Moreover, when working with NFT collections, the “getWalletNFTCollections” and “getNFTContractMetadata” endpoints can be particularly useful. 

However, in this article, we want to show you how to get all the NFTs from a collection using “getContractNFTs. Thus, we need to take a closer look at this endpoint, which essentially enables you to get all NFTs from a contract. Also, “getContractNFTs” provides you with metadata for all NFTs belonging to a particular smart contract. That said, you should keep in mind that the endpoint is limited to presenting 100 results per page. As such, you need to work with the cursor parameter for collections with more than 100 NFTs. 

Get NFTs by Contract Docs

Following is a screenshot of the “Get NFTs by Contract” documentation page:

The getContractNFT endpoint documentation page.

The latter provides all the details about the endpoint. This page also enables you to test this endpoint and copy the code that matches your programming language. Furthermore, here are the parameters that this endpoint takes in:

  • address – You use this parameter to provide an NFT smart contract address. Moreover, this parameter is essential (all other parameters are optional). 
  • chain – This parameter lets you determine which programmable chain to focus on (Moralis supports all leading Web3 networks).  
  • format – You use this parameter to determine what type of token ID format you want to work with – decimal or HEX.
  • limit – This parameter lets you set the desired page size of the result.
  • totalRange – You use this parameter to determine the number of subranges to split the results into.
  • range – This parameter lets you set the desired subrange to query.
  • cursor – You use this parameter to get to the next page when you have more than 100 NFTs or want to display even fewer NFTs per page.
  • normalizeMetadata” – This parameter lets you decide whether normalized metadata should be returned (true) or not (false). If you choose “true”, the endpoint returns metadata in a standardized structure in a new object called “normalized_metadata”.     

Now that you know the theory and the capabilities of the “getContractNFTs” endpoint, you are ready to get your hands dirty. As such, make sure to follow our tutorial that covers the steps you need to complete to get NFTs from a collection.

How to Get All NFTs from a Collection

Knowing how to get all NFTs from a collection can be extremely useful. As pointed out earlier, NFTs play important roles in many dapps, and you can use these types of tokens to represent ownership of a unique item on the blockchain. Accordingly, you can build NFT portfolio trackers, NFT-gated websites, DAOs, social graphs, and many other types of NFT dapps.

Before moving on, make sure you have these three prerequisites completed:

  • Install Node v.14 or higher.
  • Prepare your favorite code editor or IDE. We tend to use Visual Studio Code (VSC).
  • Install your favorite package manager (”npm, yarn, or pnpm”).
Wizard pointing at text that says learn how to get all nfts from a collection.

Step 1: Set Up Moralis  

To complete this tutorial and get all NFTs from a collection, you must access your Moralis admin area. As such, make sure you create your free Moralis account. You can do so by using the link in the intro or by visiting the Moralis homepage:

In either case, you’ll land on the register page. Simply enter your full name and email address, create your password, and accept the terms and conditions. With these details in place, you’ll be able to click on the “Sign Up” button:

Finally, confirm your account by clicking on the confirmation link in your email inbox.

Whether you want to use “getContractNFTs” to get NFTs from a collection or any other Moralis Web3 API endpoint, you need your Web3 API key. Fortunately, now that you have your Moralis account ready and can access your admin area, you get to obtain it in two simple clicks:

You’ll see where to paste your API key in the next section. However, before moving on, install the Moralis SDK in your project. Depending on the package manager that you are using, you need to run one of the following commands:

npm install moralis
yarn add moralis
pnpm add moralis

Step 2: Use an Example Script to Get All NFTs from a Collection

With the initial setup under your belt, you are ready to focus on an example script that enables you to get all the NFTs from a collection effortlessly. Here is an “index.js” script that does the trick:

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: In case you prefer working with TypeScript, make sure to use the “documentation page” link above. There, you’ll get to choose between “index.js” and “index.ts”.

Moreover, if you look at the lines of code above, you can see “EvmChain.ETHEREUM“. The latter defines the chain the script focuses on. So, if you’d like to get all the NFTs from a collection on other leading EVM-compatible chains, you’d need to replace “ETHEREUM” accordingly. 

Nonetheless, whether you decide to use JS or TS, you need to replace the “YOUR_API_KEY” placeholder with the above-obtained Web3 API key.

Step 3: Execute the Program to Get All NFTs from a Collection

Now that you have the “index” file ready, you get to execute the program and see the details for a given address, including how many NFTs in a collection there are. Hence, enter one of the following commands in your terminal:

For JavaScript:

node index.js

For TypeScript: 

node index.ts

After running the appropriate command, check your terminal for the results. If you didn’t change the address in the code above, you should see the following results:

{
  "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"
}

So, how many NFTs in a collection are there when focusing on the “0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB” address on the Ethereum chain? If you’re thinking 9,991, you’re right. Now, try using the “index” script to get NFTs from a collection on other EMV-compatible chains.

Finalized NFT explorer dapp letting a user get all NFTs from a collection in 1 click.

Bonus: Get All NFTs from a Collection Using an Explorer Dapp 

The above tutorial taught you how to get all the NFTs from a collection. As such, you are now ready to build a proper dapp utilizing the “getContractNFTs” endpoint. If that sounds interesting, make sure to use the video below and follow our in-house expert’s lead. Instead of focusing on how many NFTs in a collection there are, you will build a neat NFT explorer dapp. The latter will enable you to enter a contract address and select the chain you want to focus on. With these two parameters in place, you’ll be able to hit the “Get NFTs” button, which will get NFTs from a collection and neatly present the first twenty NFTs: 

Finally, this is the video tutorial that will show you how to create a NodeJS backend app and ReactJS frontend app and tie it all together to create a cool NFT explorer dapp with Moralis: 

How to Get All NFTs from a Collection in 3 Steps – Summary

In today’s article, you had an opportunity to learn how to get all NFTs from a collection using the ultimate NFT API. In case you were new to Moralis, you also learned that this enterprise-grade Web3 API provider enables you to create killer dapps the easy way. We also explained what NFTs and NTF collections are. However, the core of the article was our three-step tutorial that focused on using the “getContracNFTs” endpoint. Furthermore, by completing this tutorial, you learned how to easily find out how many NFTs in a collection there are, among several other details. Last but not least, you also had a chance to follow a short video tutorial and build an example NFT explorer dapp. Hence, you now know how to get all NFTs from a collection using our example dapp. 

If you enjoyed today’s tutorial and want to continue exploring NFT development, make sure to check out our other NFT API how-to guides that await you in Moralis’ docs. On the other hand, you may be interested in exploring other blockchain development topics. In that case, you should visit the Moralis YouTube channel and our blockchain blog. Some of the latest articles there include crypto wallet integration, Web3 provider, Web3 for business, and many other interesting topics.

Last but not least, you might be interested in going full-time crypto sooner rather than later. If so, you ought to enroll in Moralis Academy. There, you’ll be able to choose among many different courses and become blockchain-certified. However, we recommend starting with blockchain and Bitcoin fundamentals

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

Holesky Faucet – How to Get Free Holesky Testnet Funds 

January 31, 2023

Notify API Alternatives – Easiest Way to Set Up Web3 Notifications

August 3, 2022

Full 2022 Guide to Different Types of DAOs

March 6, 2023

Web3 Marketplace Development – Build a Web3 Business in 3 Steps

January 7, 2023

Mint NFT from Contract – How to Mint an NFT from a Contract

February 22, 2023

Data Availability in Blockchains – Exploring the Data Availability Layer

March 21, 2023

Get Crypto Data Using a Python API for Cryptocurrency

January 22, 2023

Complete Tutorial on How to Create an ERC721 Token