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.
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:
Step 1: Create an Alchemy app
To create an Alchemy app:
- From Alchemy’s dashboard, hover over the Apps drop-down menu and choose Create App.
- Provide a Name and Description for your app. For Chain, select Ethereum and for Network select Mainnet.
- Click the Create App button.
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:
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
: Aboolean
that indicates whether the method should omit the NFT metadata. By default, this is set tofalse
.
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:
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:
Run the script by calling the following command in your terminal:
You should obtain output that looks like this:
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!