getNftsForContractIterator -SDK

Fetches all NFTs for a given contract address and yields them in an async iterable.

This method returns the full NFTs in the contract and pages through all page keys until all NFTs have been fetched.

Don’t have an API key?

Start using this method in your app today.

Description

Fetches all NFTs for a given contract address and yields them in an async iterable.

This method returns the full NFTs in the contract and pages through all page keys until all NFTs have been fetched. To get all NFTs without their associated metadata, use the options parameter in the body of the request.

Parameters

NameTypeDescription
contractAddressstringThe contract address of the NFT contract.
optionsobjectThe optional parameters to use for the request.

options paramters

PropertyTypeDescription
pageKey?stringOptional page key from an existing OwnedBaseNftsResponse or OwnedNftsResponse to use for pagination.
pageSize?numberSets the total number of NFTs to return in the response. Defaults to 100. Maximum page size is 100.
omniMetadata?booleanOptional boolean flag to omit NFT metadata. Defaults to false.
tokenUriTimeoutInMsnumberNo set timeout by default - When metadata is requested, this parameter is the timeout (in milliseconds) for the website hosting the metadata to respond. If you want to only access the cache and not live fetch any metadata for cache misses then set this value to 0.

Response

PropertyTypeDescription
AsyncIterable<Nft>objectThe NFTs for the contract

AsyncIterable<Nft response parameters

PropertyTypeDescriptioin
contractstringThe NFT's underlying contract and relevant contract metadata.
titlestringThe NFT title.
descriptionstringThe NFT description.
timeLastUpdatedstringWhen the NFT was last updated in the blockchain. Represented in ISO-8601 format.
metadataErrorstring | undefinedHolds an error message if there was an issue fetching metadata.
rawMetadataobject | undefinedThe raw metadata fetched from the metadata URL specified by the NFT. The field is undefined if Alchemy was unable to fetch metadata.
tokenUriobject | undefinedURIs for accessing the NFT's metadata blob
mediaarray of stringsURIs for accessing the NFT's media assets
spamInfoobjectDetailed information on why an NFT was classified as spam.

Example Request and Response

Prerequisite: You will need to install the Alchemy SDK before making requests with it.

The commands for installing it using npm or yarn are given below:

npm install alchemy-sdk
yarn add alchemy-sdk

Request

// Imports the Alchemy SDK
const { Alchemy, Network } = require("alchemy-sdk");

// Configures the Alchemy SDK
const config = {
    apiKey: "alchemy-replit", // Replace with your API key
    network: Network.ETH_MAINNET, // Replace with your network
};

// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);

const main = async () => {
    // define the contract address whose NFTs you want to fetch
    const contractAddress = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D";

    // create an async generator function that uses the getNftsForContractIterator method
    async function getNftsForContract() {
        try {
            let nfts = [];
            // Get the async iterable for the contract's NFTs.
            const nftsIterable = alchemy.nft.getNftsForContractIterator(contractAddress);

            // Iterate over the NFTs and add them to the nfts array.
            for await (const nft of nftsIterable) {
                nfts.push(nft);
            }

            // Log the NFTs.
            console.log(nfts);
        } catch (error) {
            console.log(error);
        }
    }

    // call the async generator function
    getNftsForContract();

};

main();

Response

[
  {
    contract: {
      address: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d',
      name: 'BoredApeYachtClub',
      symbol: 'BAYC',
      totalSupply: '10000',
      tokenType: 'ERC721',
      openSea: [Object],
      contractDeployer: '0xaba7161a7fb69c88e16ed9f455ce62b791ee4d03',
      deployedBlockNumber: 12287507
    },
    tokenId: '98',
    tokenType: 'ERC721',
    title: '',
    description: '',
    timeLastUpdated: '2023-03-02T17:33:56.703Z',
    metadataError: undefined,
    rawMetadata: {
      image: 'ipfs://Qma1kbYN4LzAK9XvQUNJ6nf79iAd3dM6K87ivm8oLrisVV',
      attributes: [Array]
    },
    tokenUri: {
      gateway: 'https://alchemy.mypinata.cloud/ipfs/QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/98',
      raw: 'ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/98'
    },
    media: [ [Object] ],
    spamInfo: undefined
  },
  {
    contract: {
      address: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d',
      name: 'BoredApeYachtClub',
      symbol: 'BAYC',
      totalSupply: '10000',
      tokenType: 'ERC721',
      openSea: [Object],
      contractDeployer: '0xaba7161a7fb69c88e16ed9f455ce62b791ee4d03',
      deployedBlockNumber: 12287507
    },
    tokenId: '99',
    tokenType: 'ERC721',
    title: '',
    description: '',
    timeLastUpdated: '2023-03-02T17:33:56.457Z',
    metadataError: undefined,
    rawMetadata: {
      image: 'ipfs://QmWiXSKwbwHkobDdZbDc6t66kf192P33Ru5UJYhL5mPJTk',
      attributes: [Array]
    },
    tokenUri: {
      gateway: 'https://alchemy.mypinata.cloud/ipfs/QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/99',
      raw: 'ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/99'
    },
    media: [ [Object] ],
    spamInfo: undefined
  },
  ... 9900 more items
]

Code Sandbox

You can test out the getNftsForContractIterator method using the code sandbox below:

Use Cases

Here are some potential use cases for the getNftsForContractIterator method:

  • Marketplace platforms: Marketplace platforms that enable buying and selling of NFTs often require the ability to search for specific NFTs based on contract addresses. The getNftsForContractIterator method can be used to retrieve all NFTs associated with a given contract, allowing users to browse and search for NFTs from a particular collection.

  • Wallet applications: Wallet applications that support NFTs may need to display all the NFTs owned by a user for a particular contract. The getNftsForContractIterator method can be used to retrieve all NFTs associated with a specific contract that is owned by a given address.

  • NFT explorer tools: NFT explorer tools allow users to browse and search for NFTs on the blockchain. These tools often require the ability to query the blockchain for NFTs associated with a specific contract. The getNftsForContractIterator method can be used to retrieve all NFTs associated with a given contract, which can then be displayed and searched through an explorer tool.

Overall, getNftsForContractIterator is a useful method for any application that involves querying the blockchain for NFTs associated with a particular contract.

Related Methods

ReadMe