🎉 ⏱ Ready to build? Get early access now!⏱ 🎉
NFT Collection Code Examples
Last updated:
January 13, 2022
Easy copy and paste snippets to use thirdweb NFT Collection module
Mint an NFT
const nftCollection = sdk.getNFTModule("<MODULE_ADDRESS>"); const mintNft = async () => { try { await nftCollection.mint({ name: "thirdweb", description: "Smart contracts you control. Tools that accelerate your workflow. Intuitive SDKs and widgets for developers.", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/1.png", properties: {}, }); } catch (err) { console.log(err); } }; mintNft();
Mint an NFT for another wallet
Replace <TARGET_ADDRESS>
with the target address.
const nftCollection = sdk.getNFTModule("<MODULE_ADDRESS>"); const mintNftTo = async (address: string) => { try { await nftCollection.mintTo(address, { name: "thirdweb", description: "Smart contracts you control. Tools that accelerate your workflow. Intuitive SDKs and widgets for developers.", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/1.png", properties: {}, }); } catch (err) { console.log(err); } }; mintNftTo("<TARGET_ADDRESS>");
Get the balance for the current account
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const balance = async () => { await nftCollection .balance() .then((data) => console.log(data.toNumber())) .catch((err) => console.log(err)); };
Get the balance for a different account
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const balance = async () => { let address = "<TARGET_ADDRESS>"; await nftCollection .balanceOf(address) .then((data) => console.log(data.toNumber())) .catch((err) => console.log(err)); };
Burn a specific token by token ID
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const burnToken = async (tokenId) => { await nftCollection .burn(tokenId) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Generate signature for a mint request
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const generateNftSignature = async () => { await nftCollection .generateSignature({ // The address of the currency used in the event that there is a price set on the token. // If this is set to the 0x0 address, then its free to mint. currencyAddress: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", // Native currencies. // The metadata of the token to generate a signature for. metadata: { id: "0", }, mintEndTimeEpochSeconds: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7, mintStartTimeEpochSeconds: Math.floor(Date.now() / 1000), price: 0, // The receiver of the NFTs being minted when the signature is claimed. // 0x0000000000000000000000000000000000000000 for no owner. to: "<TARGET_ADDRESS>", }) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Generate a batch of signatures for payloads
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const generateNftSignatureBatch = async () => { await nftCollection .generateSignatureBatch([ { currencyAddress: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", metadata: { id: "0", }, mintEndTimeEpochSeconds: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7, // 7 days from now. mintStartTimeEpochSeconds: Math.floor(Date.now() / 1000), price: 0, to: "<TARGET_ADDRESS>", }, { currencyAddress: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", metadata: { id: "1", }, mintEndTimeEpochSeconds: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 5, // 5 days from now. mintStartTimeEpochSeconds: Math.floor(Date.now() / 1000), price: 1, to: "<TARGET_ADDRESS>", }, ]) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Get a specific NFT by token ID
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const getNftById = async (tokenId) => { await nftCollection .get(tokenId) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Get all NFTs.
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const getAllNfts = async () => { await nftCollection .getAll() .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Get all NFTs with Owner
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const getAllNftsWithOwner = async () => { await nftCollection .getAllWithOwner() .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Get all NFTs owned by an Address
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const getAllNftsOwnedByAddress = async (address) => { await nftCollection .getOwned(address) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Get Royalty basis points
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const getRoyaltyBasisPoints = async () => { await nftCollection .getRoyaltyBps() .then((data) => console.log(data.toNumber())) .catch((err) => console.log(err)); };
Get Royalty recipient address
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const getRoyaltyRecipientAddress = async () => { await nftCollection .getRoyaltyRecipientAddress() .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Get NFT with owner by token ID
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const getWithOwner = async (tokenId) => { await nftCollection .getWithOwner(tokenId) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Check if a smart contract is approved to spend on your behalf
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const isApproved = async (address, contractId) => { await nftCollection .isApproved(address, contractId) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Check if NFT transfer is restricted
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const isNftTransferRestricted = async () => { await nftCollection .isTransferRestricted() .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Check if the smart contract is V1 or V2
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const isContractV1 = async () => { await nftCollection .isV1() .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Mint a NFT
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const mintNFT = async () => { await nftCollection .mint({ name: "thirdweb", description: "NFT Minted with thirdweb!", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/1.png", properties: {}, }) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Mint NFT batch
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const mintNFTBatch = async () => { await nftCollection .mintBatch([ { name: "thirdweb", description: "NFT Minted with thirdweb!", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/1.png", properties: {}, }, { name: "thirdweb #2", description: "NFT Minted with thirdweb!", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/2.png", properties: {}, }, ]) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Mint NFT to a different address
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const mintNFT = async () => { let address = "<TARGET_ADDRESS>"; await nftCollection .mintTo(address, { name: "thirdweb", description: "NFT Minted with thirdweb!", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/1.png", properties: {}, }) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Mint NFT Batch to a different address
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const mintNFTBatch = async () => { let address = "<TARGET_ADDRESS>"; await nftCollection .mintBatchTo(address, [ { name: "thirdweb", description: "NFT Minted with thirdweb!", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/1.png", properties: {}, }, { name: "thirdweb #2", description: "NFT Minted with thirdweb!", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/2.png", properties: {}, }, ]) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Mint NFT with signature
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const mintNftWithSignature = async () => { await nftCollection .mintWithSignature( // Payload obtained from the emitted JSON when signature is generated for a NFT. // Here's an example payload obtained. { // Native currency. currencyAddress: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", // Token metadata. metadata: { id: "0" }, // Defined times. mintEndTimeEpochSeconds: 1642587937, mintStartTimeEpochSeconds: 1641983137, // Price. price: 0, // Address to be sent to. to: "<TARGET_ADDRESS>", // Generated ID. id: "0x", // Image URI. uri: "ipfs://QmbCY5TnhdHMWuAsckSECb3eY31pVnndJGSRsuhGqjxjzA/0", }, // Signature obtained from the NFT signature generation. "0x" ) .then((data) => console.log(data.toNumber())) .catch((err) => console.log(err)); };
Get the owner of a token
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const getNftOwner = async (tokenId) => { await nftCollection .ownerOf(tokenId) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Set Approval for an operator.
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nft = sdk.getNFTModule(nftCollectionAddress); const setTokenApproval = async () => { // The ID for the deployed contract that will spend on your behalf. const operatorContractId = "[OPERATOR_CONTRACT_ID]"; await nft .setApproval(operatorContractId, true) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Set metadata for the module
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const setModuleMetadata = async () => { await nftCollection .setModuleMetadata({ name: "NFT module", description: "My cool NFT module!", image: "[IMAGE_BUFFER_OR_LINK]" // Replace this with image buffer, or IPFS link. }) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Set restriction on transfer
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const setTransferRestriction = async () => { await nftCollection .setRestrictedTransfer(true) // true or false .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Set Royalty basis points
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const setRoyaltyBasisPoints = async () => { await nftCollection .setRoyaltyBps(1) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Get the total supply
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const getTotalSupply = async () => { await nftCollection .totalSupply() .then((data) => console.log(data.toNumber())) .catch((err) => console.log(err)); };
Transfer a specific NFT to an address
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const transferTokenToAddress = async (tokenId, address) => { await nftCollection .transfer(address, tokenId) .then((data) => console.log(data.toNumber())) .catch((err) => console.log(err)); };
Transfer a NFT from an address to a different one
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const transferTokenFromAddress = async (fromAddress, toAddress, tokenId) => { await nftCollection .transferFrom(fromAddress, toAddress, tokenId) .then((data) => console.log(data.toNumber())) .catch((err) => console.log(err)); };
Verify mint request with a signature
// The NFT module address received after initializing the NFT module on the dashboard. const nftCollectionAddress = "<MODULE_ADDRESS>"; // Initialize the NFT drop module with the contract address. const nftCollection = sdk.getNFTModule(nftCollectionAddress); const verifyNftMintRequestWithSignature = async () => { await nftCollection .verify( // Payload obtained from the emitted JSON when signature is generated for a NFT. // Here's an example payload obtained. { // Native currency. currencyAddress: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", // Token metadata. metadata: { id: "0" }, // Defined times. mintEndTimeEpochSeconds: 1642587937, mintStartTimeEpochSeconds: 1641983137, // Price. price: 0, // Address to be sent to. to: "<TARGET_ADDRESS>", // Generated ID. id: "0x", // Image URI. uri: "ipfs://QmbCY5TnhdHMWuAsckSECb3eY31pVnndJGSRsuhGqjxjzA/0", }, // Signature obtained from the NFT signature generation. "0x" ) .then((data) => console.log(data)) .catch((err) => console.log(err)); };
Batch mint NFTs
If you want to mint a group of NFTs, it is significantly cheaper to pay gas in a single batch mint transaction than to call mint
for every token you want to mint, because you'll pay gas for every single call to mint
.
The mintBatch
method is a gas efficient way to mint any number of NFTs, and guarantees an all-or-nothing execution; as in if one token fails to mint, the whole batch will fail which helps you know that you won't double-mint an NFT.
const nftCollection = sdk.getNFTModule("<MODULE_ADDRESS>"); const mintBatchNft = async () => { try { await nftCollection.mintBatch([ { name: "thirdweb #1", description: "Smart contracts you control. Tools that accelerate your workflow. Intuitive SDKs and widgets for developers.", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/1.png", properties: {}, }, { name: "thirdweb #2", description: "Smart contracts you control. Tools that accelerate your workflow. Intuitive SDKs and widgets for developers.", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/2.png", properties: {}, }, ]); } catch (err) { console.log(err); } }; mintBatchNft();
Batch mint NFTs for another wallet
Combination of the previous example, replace <TARGET_ADDRESS>
for the target address and replace object for an array of objects.
const nftCollection = sdk.getNFTModule("<MODULE_ADDRESS>"); const mintBatchNftTo = async (address: string) => { try { await nftCollection.mintBatchTo(address, [ { name: "thirdweb #3", description: "Smart contracts you control. Tools that accelerate your workflow. Intuitive SDKs and widgets for developers.", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/1.png", properties: {}, }, { name: "thirdweb #4", description: "Smart contracts you control. Tools that accelerate your workflow. Intuitive SDKs and widgets for developers.", image: "ipfs/<YOUR_IPFS_FOLDER_CID>/2.png", properties: {}, }, ]); } catch (err) { console.log(err); } }; mintBatchNftTo("<TARGET_ADDRESS>");
Previous
Ready to build your first web3 app? Get early access & add web3 features to your project today.
Contents
Mint an NFT
Mint an NFT for another wallet
Get the balance for the current account
Get the balance for a different account
Burn a specific token by token ID
Generate signature for a mint request
Generate a batch of signatures for payloads
Get a specific NFT by token ID
Get all NFTs.
Get all NFTs with Owner
Get all NFTs owned by an Address
Get Royalty basis points
Get Royalty recipient address
Get NFT with owner by token ID
Check if a smart contract is approved to spend on your behalf
Check if NFT transfer is restricted
Check if the smart contract is V1 or V2
Mint a NFT
Mint NFT batch
Mint NFT to a different address
Mint NFT Batch to a different address
Mint NFT with signature
Get the owner of a token
Set Approval for an operator.
Set metadata for the module
Set restriction on transfer
Set Royalty basis points
Get the total supply
Transfer a specific NFT to an address
Transfer a NFT from an address to a different one
Verify mint request with a signature
Batch mint NFTs
Batch mint NFTs for another wallet