How to Get All NFTs in a Collection

Learn how to get all NFTs that belong to a particular collection using the Alchemy NFT API.

This tutorial uses the getNFTsForCollection endpoint.

If you’re building an NFT marketplace such as OpenSea, an NFT analytics platform like rarity.tools, or a website for your PFP project, you will likely want to display all the NFTs in a collection you are interested in.

1440

The Bored Ape Yacht Club collection on OpenSea

Typically, you might achieve this by calling functions on the contract directly and parsing the data that you’re interested in. This approach requires an understanding of ERC-721 or ERC-1155 standards and involves making multiple individual requests for each NFT in the collection.

Using Alchemy’s NFT API, however, you can retrieve all of the data for an entire NFT collection with a single call. This tutorial will guide you in creating a script that retrieves all metadata of an NFT collection.

Our Example

In our example, we will write a script in Node that retrieves all metadata for each NFT in the Bored Ape Yacht Club (BAYC) collection on the Ethereum network. We will achieve this by using Alchemy and the NFT API.

Creating the NFT Collections Script

Prerequisites

Before you begin the steps in this tutorial, ensure you complete the following steps:

  • Install both Node.js (> 14) and npm on your local machine. To check your Node version, run the following command in your terminal:
bash
$node -v

Step 1: Create an Alchemy app

To create an Alchemy app:

  1. From Alchemy’s dashboard, hover over the Apps drop-down menu and choose Create App.
  2. Provide a Name and Description for your app. For Chain, select Ethereum and for Network select Mainnet.
  3. Click the Create App button.

1440

Creating an app on the Alchemy Dashboard

Once you have created your app, click on your app’s View Key button in the dashboard and save the API KEY. We will use this later.

Step 2: Create a Node project

Now, let’s create an empty repository and install all node dependencies. To make requests to the NFT API, we recommend using the Alchemy SDK. However, you can also use axios or fetch libraries. Run the following commands in your terminal:

$mkdir nft-collection && cd nft-collection
>npm init -y
>npm install --save alchemy-sdk
>touch main.js

The above commands create a repository named nft-collection that holds all the files and dependencies we need. Open this repository in your favorite text editor (e.g., VS Code). We will write the remainder of our code in the main.js file.

Step 3: Get all NFTs that belong to a collection

The SDK renames getNFTsForCollection to getNftsForContract for better consistency. They refer to the same method.

To retrieve all of the NFTs that belong to a collection, we will use the getNftsForContract method. This method accepts one required argument and two optional arguments.

  • contractAddress: The address of the NFT contract we’re interested in. This is a required argument.
  • omitMetadata: A boolean that indicates whether the method should omit the NFT metadata. By default, this is set to false.

As noted, the method returns NFT metadata by default. For more information about NFT metadata, check out the NFT API FAQ.

Add the following code to the main.js file, using your Alchemy API key:

1// Setup: npm install alchemy-sdk
2import { Alchemy, Network } from "alchemy-sdk";
3
4const config = {
5 apiKey: "<-- ALCHEMY APP API KEY -->",
6 network: Network.ETH_MAINNET,
7};
8const alchemy = new Alchemy(config);
9
10const main = async () => {
11 // Contract address
12 const address = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D";
13
14 // Flag to omit metadata
15 const omitMetadata = false;
16
17 // Get all NFTs
18 const response = await alchemy.nft.getNftsForContract(address, {
19 omitMetadata: omitMetadata,
20 });
21 console.log(JSON.stringify(response, null, 2));
22};
23
24const runMain = async () => {
25 try {
26 await main();
27 process.exit(0);
28 } catch (error) {
29 console.log(error);
30 process.exit(1);
31 }
32};

Note: The API will only return data for the first 100 NFTs. For collections that contain more than 100 NFTs, use the startToken argument for the getNFTsForCollection method and set it to the NFT number you wish to retrieve.

For instance, if you want data on NFTs 101-200, set startToken to 101. For 201-300, set it to 201. Continue this process until you reach the end of the collection.

For more information, check out the NFT API docs.

Step 4: Parse the NFT API Output

As a final step, parse the API output to show the image URL of every NFT. In the case of BAYC, this is an IPFS (InterPlanetary File System) URL.

Replace the contents of main.js with the following code:

1// Setup: npm install alchemy-sdk
2import { Alchemy, Network } from "alchemy-sdk";
3
4const config = {
5 apiKey: "<-- ALCHEMY APP API KEY -->",
6 network: Network.ETH_MAINNET,
7};
8const alchemy = new Alchemy(config);
9
10const main = async () => {
11 // Contract address
12 const address = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D";
13
14 // Flag to omit metadata
15 const omitMetadata = false;
16
17 // Get all NFTs
18 const { nfts } = await alchemy.nft.getNftsForContract(address, {
19 omitMetadata: omitMetadata,
20 });
21
22 let i = 1;
23
24 for (let nft of nfts) {
25 console.log(`${i}. ${nft.rawMetadata.image}`);
26 i++;
27 }
28};
29
30const runMain = async () => {
31 try {
32 await main();
33 process.exit(0);
34 } catch (error) {
35 console.log(error);
36 process.exit(1);
37 }
38};

Run the script by calling the following command in your terminal:

bash
$node main.js

You should obtain output that looks like this:

bash
$NFT Metadata
>1. ipfs://QmRRPWG96cmgTn2qSzjwr2qvfNEuhunv6FNeMFGa9bx6mQ
>2. ipfs://QmPbxeGcXhYQQNgsC6a36dDyYUcHgMLnGKnF8pVFmGsvqi
>3. ipfs://QmcJYkCKK7QPmYWjp4FD2e3Lv5WCGFuHNUByvGKBaytif4
>4. ipfs://QmYxT4LnK8sqLupjbS6eRvu1si7Ly2wFQAqFebxhWntcf6
>5. ipfs://QmSg9bPzW9anFYc3wWU5KnvymwkxQTpmqcRSfYj7UmiBa7
>6. ipfs://QmNwbd7ctEhGpVkP8nZvBBQfiNeFKRdxftJAxxEdkUKLcQ
>7. ipfs://QmWBgfBhyVmHNhBfEQ7p1P4Mpn7pm5b8KgSab2caELnTuV
>8. ipfs://QmRsJLrg27GQ1ZWyrXZFuJFdU5bapfzsyBfm3CAX1V1bw6
>9. ipfs://QmXEqPbvM4aq1SQSXN8DSuEcSo5SseYW1izYQbsGB8yn9x
>10. ipfs://QmUQgKka8EW7exiUHnMwZ4UoXA11wV7NFjHAogVAbasSYy
>11. ipfs://QmPQdVU1riwzijhCs1Lk6CHmDo4LpmwPPLuDauY3i8gSzL
>12. ipfs://QmVvdAbabZ2awja88uUhYHFuq67iEiroFuwLGM6HyiWcc8
>13. ipfs://QmexFWUtmFNGQwVuRN8dLLwcucSFoLYjeHkv2uAyan5H8t
>14. ipfs://QmaZLhanFj41yfXY3Ux1JNv6ZAnzsWXfbJmo7qbJzZjh4s
>15. ipfs://QmbcWzteFsAvdwiaZyC1YJVNsApDaPRmqkQDyrYAvP4u48

Congratulations! You now know how to use the Alchemy NFT API to retrieve all NFTs in a collection or at an address. You can adapt this for any network (e.g., Ethereum, Polygon, etc.).

If you enjoyed this tutorial about how to get all NFTs owned by an address, tweet us at @Alchemy and give the authors @rounak_banik and @ankg404 a shoutout!

Don’t forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!

Ready to start using the Alchemy NFT API?

Create a free Alchemy account and do share your project with us!