Guide 5: How to get all NFTs in a collection

Use the Alchemy SDK to get all of a contract's NFTs in an instant.

The SDK Developer Challenge: What are we here for?

The SDK Developer Challenge is an opportunity to build and learn together, using daily guides to inspire and motivate you to supercharge your app with the SDK.

The SDK will be your secret weapon to help simplify your code so you can build faster.

As you build, submit your project to win prizes.

What could you win? Epic never-before seen Alchemy swag, and $500 in monthly Alchemy credit.

For all of the challenges, you will need to set up a free Alchemy account. This will give you access to the SDK, as well as a HUGE selection of web3 development tools.

Ok, let's go!

Guide 5: How to get all NFTS in a collection

In this guide, we will assume that you already know how to:

  • Set up an Alchemy SDK instance in your local project and initialize it with your Alchemy API Key
  • Perform basic navigation of the command line
  • Make an API call to Alchemy
  • Show the results in your terminal

If you don't, head on back to Guide 2, How to get started on the Alchemy SDK.

Intro

If you are building a dApp, you may want to integrate NFT-specific data into your user experience.

That may mean integrating NFT collection data, like:

  • What is the collection name?
  • What is the collection description?
  • What is the collection floor price?
  • What is the collection's total NFT supply?
  • What is the collection's website, Discord and Twitter URLs?

And even further, that may mean questions specific to an NFT, like:

  • What is the NFT's title?
  • What is the NFT's description?
  • Where can I find the NFT's image location?
  • What is the NFT's metadata?

Well - look no further, dApp developers! Thanks to the Alchemy SDK, you can instantly retrieve ALL of the NFTs in a collection, including important metadata, that answers all of the questions listed above and more.

Step 1: Import Code Snippet into Local Project

Assuming you have a local environment set up and an Alchemy API key ready, copy-paste the code snippet below:

require('dotenv').config();
const { Alchemy, Network, Utils } = require('alchemy-sdk');

// Optional Config object, but defaults to demo api-key and eth-mainnet.
const settings = {
  apiKey: process.env.ALCHEMY_API_KEY,
  network: Network.ETH_MAINNET,
};

const alchemy = new Alchemy(settings);

async function getAllContractNFTs() {
  // NFT contract address
  const address = '0xBd3531dA5CF5857e7CfAA92426877b022e612cf8';

  // Keep this as false so you get all the needed metadata!
  const omitMetadata = false;

  // Get all NFTs and store in array
  const { nfts } = await alchemy.nft.getNftsForContract(address, {
    omitMetadata: omitMetadata,
  });

  let i = 0;\n\n  for (let nft of nfts) {
    // for now, let's log just the NFT image location!
    console.log(`${i}. ${nft.rawMetadata.image}`);
    i++;
  }
}

getAllContractNFTs();

This is the first time in the SDK Developer Challenge that we use the nft namespace available on the Alchemy SDK instance! This namespace gives you direct access to the Alchemy NFT Enhanced API.

Step 2: Run Your Script!

You'll notice the code will only console.log() the image location of all the NFTs in a collection, but that's just because we set it up this way. You can also see all of the available data that is returned from this API call by doing something like:

// do this to see all the data you get back on just ONE ;\n\nlet i = 0NFT
console.log(nft[0]);

// do this if you want to get all the data on ALL the NFTs
for (let nft of nfts) {
  // for now, let's log just the NFT image location!
  console.log(`${i}. ${nft[i]}`);
  i++;
}

If you show all of the data on the nft, it will look a little something like this in your console:

2862

That's a ton of data fetched in an instant!

Once you have everything set up, simply run node getAllContractNFTs.js and you will see your terminal instantly populate with all the description, total supply, image locations and metadata of a very cool NFT collection! Can you figure out which NFT collection we used? 👀

Why Is This Useful? 🤔

Without Alchemy's SDK and NFT API, if you needed up-to-date info about an NFT collection, you'd have to brute-force search the entire blockchain. The Alchemy SDK helps you avoid that! One code line and you instantly have an immense amount of data on any NFT, 🤯 to easily build an NFT gallery like Behance or an NFT indexer like Dune Analytics!

Step 3: Customize Your Script!

The script you just set up via the Alchemy SDK is extremely powerful! The contract address we used (shoutout PP!) is only an example... try replacing the value of address with your favorite NFT collection's address and see what comes back!