How to get all tokenIDs in an NFT contract
The code snippet below looks for the startTokenId
of the contract (which is usually 0
) and the current largest tokenId (maxId
). You will get all the tokenIds of the given contract by getting all the bigint
from startTokenId
to maxId
Note: If you’re sure the contract’s startTokenId is always 0
, you can skip using startTokenId()
. For thirdweb’s ERC1155 contract, the tokenId starts from 0
// Example for an ERC-721 contract
const options = {
contract: theNftContract,
};
const [startTokenId_, maxSupply] = await Promise.allSettled([
startTokenId(options),
nextTokenIdToMint(options),
totalSupply(options),
]).then(([_startTokenId, _next, _total]) => {
// default to 0 if startTokenId is not available
const startTokenId__ =
_startTokenId.status === "fulfilled" ? _startTokenId.value : 0n;
let maxSupply_: bigint;
// prioritize nextTokenIdToMint
if (_next.status === "fulfilled") {
// because we always default the startTokenId to 0 we can safely just always subtract here
maxSupply_ = _next.value - startTokenId__;
}
// otherwise use totalSupply
else if (_total.status === "fulfilled") {
maxSupply_ = _total.value;
} else {
throw new Error(
"Contract requires either `nextTokenIdToMint` or `totalSupply` function available to determine the next token ID to mint",
);
}
return [startTokenId__, maxSupply_] as const;
});
const maxId = maxSupply + startTokenId_;
const allTokenIds:bigint[] = [];
for (let i = startTokenId_; i < maxId; i++) {
allTokenIds.push(i);
}
console.log(allTokenIds); // [startTokenId_, 1n, 2n, ... maxId]
Can’t get this working? If you’ve followed the above and still have issues, contact our support team for help.
Did this answer your question?
😞
😐
🤩