Alchemy: Using The Alchemy SDK with NFTs Minted Through thirdweb

Learn how to use the Alchemy SDK with NFTs minted through thirdweb.

What is Alchemy SDK?

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.

Prerequisites:

1. Start by navigating to the Thirdweb dashboard and connecting your crypto wallet.

2. Select ‘Create NFTs and Tokens’ from the list of options.

3. Select ‘NFT Collection’:

4. Give your NFT collection a name, icon, and description, then confirm that the recipient address is your crypto wallet address.

For this tutorial, we’ll be using the Göerli testnet, so we are using a Rinkeby wallet.

5. Select ‘Deploy Now’ and confirm the transaction through your crypto wallet.

6. Next, we need to create an IPFS bucket on Filebase.

To do this, navigate to console.filebase.com. If you don’t have an account already, sign up, then log in.

7. Select ‘Buckets’ from the left sidebar menu, or navigate to console.filebase.com/buckets.

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";

const sdk = new ThirdwebSDK("goerli");
const contract = sdk.getNFTCollection("CONTRACT");
// Address of the wallet you want to mint the NFT to
const walletAddress = "WALLET_ADDRESS";
// Custom metadata of the NFTs you want to mint.
const metadatas = [{
	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"),
}];

const tx = await contract.mintBatchTo(walletAddress, metadatas);
const receipt = tx[0].receipt; // same transaction receipt for all minted NFTs
const firstTokenId = 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:

const alchemy = new Alchemy();

// 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.
async function main() {
  for await (const nft of alchemy.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';

const alchemy = new Alchemy();

const nftAddress = 'CONTRACT_ADDRESS';

async function main() {
  for await (const nft of alchemy.nft.getNftsForContractIterator(nftAddress, {
    // Omit the NFT metadata for smaller payloads.
    omitMetadata: true,
  })) {
    await alchemy.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”;
const alchemy = new Alchemy();

const nftAddress =CONTRACT_ADDRESS”;

const allTransfers = await alchemy.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.

If you have any questions, please join our Discord server, or send us an email at hello@filebase.com

Last updated