Alchemy SDK is a Javascript SDK that supports the syntax and functionality of the Ethers.js AlchemyProvider and WebSocketProvider packages. It adds additional functionality including access to Alchemy’s enhanced API and NFT API, and offers features such as automated retries.
Read below to learn how to use the Alchemy SDK with NFTs minted through thirdweb.
Select ‘Create Bucket’ in the top right corner to create a new bucket for your NFTs.
8. Enter a bucket name and choose the IPFS storage network to create the bucket.
Bucket names must be unique across all Filebase users, be between 3 and 63 characters long, and can contain only lowercase characters, numbers, and dashes.
9. Now, upload your NFTs to Filebase using the web console and selecting ‘Folder’, then select the folder that contains your NFT files.
These files need to be named in sequential order, such as 0.png, 1.png, 2.png, etc.
10. You will see your folder uploaded as a single object:
11. Copy the CID of your folder:
12. Navigate to your IPFS Folder using the Filebase IPFS gateway to see your folder’s files:
https://ipfs.filebase.io/ipfs/IPFS_CID
Take note of this URL.
13. Head back to the Thirdweb dashboard, where you will see the page for your NFT collection.
Select the ‘Code’ tab. This code showcases different code snippets to use to mint your NFTs using scripts, with your contract address and crypto wallet address inputted automatically for easy copy and paste. This tutorial will showcase the JavaScript code examples, but you can use any of the snippets provided in this tab.
14. Open a command line window and install the Thirdweb SDK:
npm install @thirdweb-dev/sdk
15. Open an IDE such as VSCode and insert the following code, replacing CONTRACT with your contract address, WALLET_ADDRESS with your crypto wallet address, each IPFS_CID with your IPFS folder CID you took note of earlier, and replacing the name and description of each NFT with your desired information.
import { ThirdwebSDK } from"@thirdweb-dev/sdk";constsdk=newThirdwebSDK("goerli");constcontract=sdk.getNFTCollection("CONTRACT");// Address of the wallet you want to mint the NFT toconstwalletAddress="WALLET_ADDRESS";// Custom metadata of the NFTs you want to mint.constmetadatas= [{ name:"My NFT #1", description:"This is a cool NFT", image:fs.readFileSync("https://ipfs.filebase.io/ipfs/IPFS_CID/0.png"), }, { name:"My NFT #2", description:"This is a cool NFT", image:fs.readFileSync("https://ipfs.filebase.io/ipfs/IPFS_CID/1.png"),}];consttx=awaitcontract.mintBatchTo(walletAddress, metadatas);constreceipt= tx[0].receipt; // same transaction receipt for all minted NFTsconstfirstTokenId= tx[0].id; // token id of the first minted NFT
You can edit this script to include as many NFTs as you’d like to mint at one time.
16. Save this script as mint.js, then run this script with the command:
node mint.js
17. Refresh your Thirdweb dashboard. Your NFTs will be listed.
18. You will need the NFT collection’s contract address later, so copy the contract address as shown here:
19. Next, open a new terminal window. Download the Alchemy SDK with the command:
npm install alchemy-sdk
20. Create a new project directory called alchemy-sdk, then navigate into the directory:
mkdir alchemy-sdk
cd alchemy-sdk
21. Create a new file called get-nfts.js. Insert the following code into this file:
constalchemy=newAlchemy();// Get how many NFTs an address owns.alchemy.nft.getNftsForOwner('ADDRESS').then(nfts => {console.log(nfts.totalCount);});// Get all the image urls for all the NFTs an address owns.asyncfunctionmain() {forawait (constnftofalchemy.nft.getNftsForOwnerIterator('ADDRESS')) {console.log(nft.media); }}main();// Filter out spam NFTs.alchemy.nft.getNftsForOwner('ADDRESS', { excludeFilters: [NftExcludeFilters.SPAM]}).then(console.log);
Replace ADDRESS with your crypto wallet address.
Run this file with the command node get-nfts.js.
This piece of code returns all the NFTs and associated images that are owned by the specified crypto wallet address.
22. Create another new file called get-addresses.js. Enter the following code into this file:
import { Alchemy} from'alchemy-sdk';constalchemy=newAlchemy();constnftAddress='CONTRACT_ADDRESS';asyncfunctionmain() {forawait (constnftofalchemy.nft.getNftsForContractIterator(nftAddress, {// Omit the NFT metadata for smaller payloads. omitMetadata:true, })) {awaitalchemy.nft.getOwnersForNft(nft.contract.address,nft.tokenId).then((response) =>console.log("owners:",response.owners,"tokenId:",nft.tokenId) ); }}main();
Replace CONTRACT_ADDRESS with the contract address of your NFT collection on thirdweb.
Run this file with the command node get-addresses.js.
This piece of code returns the addresses of every wallet that owns an NFT from your collection.
23. Create a new file called get-transactions.js. Insert the following code into the file:
import { Alchemy } from “alchemy-sdk”;constalchemy=newAlchemy();constnftAddress= “CONTRACT_ADDRESS”;constallTransfers=awaitalchemy.core.getAssetTransfers({ fromBlock: “0x0, contractAddresses: [nftAddress], category: [“erc721”], order : “desc”,});
Replace CONTRACT_ADDRESS with the contract address of your NFT collection on thirdweb.
Run this file with the command node get-transactions.js.
This piece of code returns all transactions including NFTs in your collection.