getNFTsForCollection
Gets all NFTs for a given NFT contract
This endpoint is supported on the following chains and networks:
  • Ethereum: Mainnet, Goerli
  • Polygon: Mainnet and Mumbai
To see chain support across all features, check out the feature matrix.

Parameters

  • contractAddress: [string] - contract address for the NFT collection
  • withMetadata: [boolean] - (optional) if set to true, returns NFT metadata; otherwise will only return tokenIds. Defaults to false.
  • startToken: [string] - (optional) an offset used for pagination. Can be a hex string, or a decimal.
  • limit: [int] - (optional) sets the total number of NFTs returned in the response. Defaults to 100.

NOTE onstartToken:

The API response will contain a maximum of 100 tokens. Because many NFT collections include more than 100 tokens, the startToken param is used to paginate results.
If nostartTokenis specified, the query will start from the lowest tokenId. Each response will return a nextTokenthat can be passed back to the API as a startToken to return the next page of results for the given NFT collection (if applicable). You can also use any tokenId from the collection as the startToken, and the results in the response will start from that offset. See the 'examples' section for an example of how to use this parameter to fetch all of the NFTS in a collection.

Returns

  • nfts: list of objects that represent NFTs stored under the queried contract address.
    Object schema:
    • id
      • tokenId: [string] Hexadecimal token ID for NFT
      • tokenMetadata
        • tokenType: "ERC721" or "ERC1155"
    • tokenUri:
      • raw: uri representing the location of the NFT's original metadata blob. This is a backup for you to parse when the metadata field is not automatically populated.
      • gateway: public gateway uri for the raw uri above.
    • metadata: relevant metadata for NFT contract. This is useful for viewing image url, traits, etc. without having to follow the metadata url in tokenUri to parse manually. For more info on NFT responses: check out the NFT API FAQ.
  • nextToken: [string] an offset used for pagination

Example

The example below is for Ethereum Mainnet. If you are using Polygon you'll need to use your polygon endpoint instead: https://polygon-mainnet.g.alchemy.com/nft/v2/your-api-key/getNFTs...

Request

Alchemy SDK
Fetch (JS)
Axios (JS)
Postman
Curl
1
// Github: https://github.com/alchemyplatform/alchemy-sdk-js
2
// Setup: npm install @alch/alchemy-sdk
3
import {
4
Network,
5
initializeAlchemy,
6
getNftsForCollection,
7
} from "@alch/alchemy-sdk";
8
9
// Optional Config object, but defaults to demo api-key and eth-mainnet.
10
const settings = {
11
apiKey: "demo", // Replace with your Alchemy API Key.
12
network: Network.ETH_MAINNET, // Replace with your network.
13
maxRetries: 10,
14
};
15
16
const alchemy = initializeAlchemy(settings);
17
18
// Print total NFT count returned in the response:
19
const nftsForCollection = await getNftsForCollection(
20
alchemy,
21
"0x61fce80d72363b731425c3a2a46a1a5fed9814b2"
22
);
23
console.log(nftsForCollection);
24
Copied!
1
import fetch from 'node-fetch';
2
3
var requestOptions = {
4
method: 'GET',
5
redirect: 'follow'
6
};
7
8
const apiKey = "demo"
9
const baseURL = `https://eth-mainnet.alchemyapi.io/nft/v2/${apiKey}/getNFTsForCollection`;
10
const contractAddr = "0x61fce80d72363b731425c3a2a46a1a5fed9814b2";
11
const tokenId = "0x1ea2";
12
const withMetadata = "false";
13
const fetchURL = `${baseURL}?contractAddress=${contractAddr}&startToken=${tokenId}&withMetadata=${withMetadata}`;
14
15
fetch(fetchURL, requestOptions)
16
.then(response => response.json())
17
.then(response => JSON.stringify(response, null, 2))
18
.then(result => console.log(result))
19
.catch(error => console.log('error', error));
Copied!
1
import axios from 'axios';
2
3
// replace with your Alchemy api key
4
const apiKey = "demo";
5
const baseURL = `https://eth-mainnet.alchemyapi.io/nft/v2/${apiKey}/getNFTsForCollection`;
6
const contractAddr = "0x61fce80d72363b731425c3a2a46a1a5fed9814b2";
7
const startToken = "0x1ea2";
8
const withMetadata = "false";
9
10
var config = {
11
method: 'get',
12
url: `${baseURL}?contractAddress=${contractAddr}&startToken=${startToken}&withMetadata=${withMetadata}`,
13
headers: { }
14
};
15
16
axios(config)
17
.then(response => console.log(JSON.stringify(response.data, null, 2)))
18
.catch(error => console.log(error));
Copied!
1
URL: https://eth-mainnet.g.alchemy.com/nft/v2/demo/getNFTsForCollection/?contractAddress=0x61fce80d72363b731425c3a2a46a1a5fed9814b2&startToken=0x1ea2&withMetadata=false
2
RequestType: GET
Copied!
1
curl 'https://eth-mainnet.g.alchemy.com/nft/v2/demo/getNFTsForCollection/?contractAddress=0x61fce80d72363b731425c3a2a46a1a5fed9814b2&startToken=0x1ea2&withMetadata=false'
Copied!
If you're having trouble running requests via Alchemy Web3.js, Fetch, or Axios, please refer to: NFT API Quickstart Guide

Response

1
{"nfts":
2
[{"id":{"tokenId":"0x1f"}},
3
{"id":{"tokenId":"0x20"}},
4
{"id":{"tokenId":"0x21"}},
5
{"id":{"tokenId":"0x22"}},
6
{"id":{"tokenId":"0x23"}},
7
{"id":{"tokenId":"0x24"}},
8
{"id":{"tokenId":"0x25"}},
9
{"id":{"tokenId":"0x26"}},
10
{"id":{"tokenId":"0x27"}},
11
{"id":{"tokenId":"0x28"}},
12
{"id":{"tokenId":"0x29"}},
13
{"id":{"tokenId":"0x2a"}},
14
{"id":{"tokenId":"0x2b"}},
15
.....
16
{"id":{"tokenId":"0x80"}},
17
{"id":{"tokenId":"0x81"}},
18
{"id":{"tokenId":"0x82"}}],
19
"nextToken":"0x83"}
Copied!

Getting the full collection

The startToken and nextToken fields are designed to allow users to fetch all of the NFTs in a collection using the following pattern:
1
import axios from 'axios';
2
3
// replace with your Alchemy api key
4
const apiKey = "demo";
5
const baseURL = `https://eth-mainnet.alchemyapi.io/nft/v2/${apiKey}/getNFTsForCollection`;
6
const contractAddr = "0x61fce80d72363b731425c3a2a46a1a5fed9814b2";
7
8
async function callGetNFTsForCollectionOnce(
9
startToken = ""
10
) {
11
const url = `${baseURL}/?contractAddress=${contractAddr}&startToken=${startToken}`;
12
const response = await axios.get(url);
13
return response.data;
14
}
15
16
let startToken = "";
17
let hasNextPage = true;
18
totalNftsFound = 0;
19
while (hasNextPage) {
20
const { nfts, nextToken } = await callGetNFTsForCollectionOnce(
21
startToken
22
);
23
if (!nextToken) {
24
// When nextToken is not present, then there are no more NFTs to fetch.
25
hasNextPage = false;
26
}
27
startToken = nextToken;
28
totalNftsFound += nfts.length;
29
}
Copied!