How to Create A Solana NFT Collection with TypeScript

How to Create A Solana NFT Collection with TypeScript

You can deploy an NFT Collection via the dashboard, but if you're curious about how you can do it with TypeScript, then you came to the right place!

Setup

Using the CLI, create a new Node.js & Javascript project with the Solana SDK preconfigured for you, using the following command:

npx thirdweb create app --node --solana 

Deploying an NFT Collection

In order to deploy a collection using the Solana SDK, we can use the deployer that can be used in creating the collection's program.

Before we go into the code, we must first identify the following:

  1. Solana network to be used (devnet, testnet, mainnet-beta, localnet)
  2. The private key to be used for signing
  3. Address of deployed program

Let's start with the code below and build it up one by one.

import { ThirdwebSDK } from '@thirdweb-dev/sdk/solana';

async function main() {
  // setup

  // deployment

  // minting logic

  // output
}

main()
  .then(() => process.exit())
  .catch(err => {
    console.error(err);
    process.exit(1);
  });

Since we're testing functionality and don't want to use up any money, it's ideal for us to use the devnet network.

Let's get an instance of the Solana SDK using fromPrivateKey with devnet as the network argument.https://portal.thirdweb.com/solana-sdk/sdk.network

import { ThirdwebSDK } from '@thirdweb-dev/sdk/solana';

async function main() {
  // setup
  const NETWORK = "devnet";
  const sdk = ThirdwebSDK.fromPrivateKey(NETWORK, "Insert your private key here");

  // deployment

  // minting logic

  // output
}

main()
  .then(() => process.exit())
  .catch(err => {
    console.error(err);
    process.exit(1);
  });

In order to get our private key first install Phantom on your browser.

After installing Phantom create a new wallet and follow the process. After wallet creation, open the extension and click the thumbnail on the upper left of the browser extension.

Click on your avatar on top left corner

Go to Developer Settings.

Go to Developer Settings

Change Network and choose devnet.

Change Network and choose devnet
Change Network and choose devnet

Once to go back to Phantom's main screen a bar at the top should show that you're now in devnet as seen below.

Now it's time to export your private key from Phantom. First click the thumbnail in the upper left corner of the extension.

Next select Security & Privacy

Choose Export Private Key

Enter your password and click Next

Now copy your private key.

Important Note: Never share your private key. This is purely for demonstration and tutorial purposes only.

Paste it in the code below:

Warning: Do not hard code your private key like this. This is purely for demonstration purposes. See How we recommend using a secret manager to store your private key.
import { ThirdwebSDK } from '@thirdweb-dev/sdk/solana';

async function main() {
  // setup
  const NETWORK = "devnet";
  const sdk = ThirdwebSDK.fromPrivateKey(NETWORK, "INSERT YOUR PRIVATE KEY HERE");

  // deployment

  // minting logic

  // output
}

main()
  .then(() => process.exit())
  .catch(err => {
    console.error(err);
    process.exit(1);
  });

Copy the deployment code below. Feel free to change the details of your collection (name, description, symbol, and many more).

import { ThirdwebSDK } from '@thirdweb-dev/sdk/solana';

async function main() {
  // setup
  const NETWORK = "devnet";
  const sdk = ThirdwebSDK.fromPrivateKey(NETWORK, "INSERT YOUR PRIVATE KEY HERE");

  // deployment
  const deployed = await sdk.deployer.createNftCollection({
    name: "My NFT Collection",
    description: "My personal NFT collection.",
    symbol: "MNFT"
  });

  // minting logic

  // output
}

main()
  .then(() => process.exit())
  .catch(err => {
    console.error(err);
    process.exit(1);
  });

Now it's time to add the minting code and the output code. Copy your public key address from your Phantom browser extension and paste it into the code.

Feel free to change the details of the NFT you're minting (name, description, symbol, and many more).

Note: The public key added here will receive the minted NFT.
import { ThirdwebSDK } from '@thirdweb-dev/sdk/solana';

async function main() {
  // setup
  const NETWORK = "devnet";
  const sdk = ThirdwebSDK.fromPrivateKey(NETWORK, "INSERT YOUR PRIVATE KEY HERE");

  // deployment
  const deployed = await sdk.deployer.createNftCollection({
    name: "My NFT Collection",
    description: "My personal NFT collection.",
    symbol: "MNFT"
  });

  // minting logic
  const nftCollection = await sdk.getNFTCollection(deployed);

  const nft = await nftCollection.mintTo("INSERT YOUR PUBLIC KEY HERE", {
    name: "My NFT Collection",
    description: "My personal NFT collection.",
    symbol: "MNFT"
  });

  // output
  console.log("Collection deployed to: ", deployed);
  console.log("Minted nft: ", nft);
}

main()
  .then(() => process.exit())
  .catch(err => {
    console.error(err);
    process.exit(1);
  });

Running the Script

Run the script with:

npx ts-node mint-nft-collection.ts

Now you should have an output like the one below and that's it!

Verifying Created Collection and Minted NFT

Go over to Solscan and change your cluster to devnet.

Copy your Minted nft address and paste it into the search box then press Enter. You should see something like the one below:

Conclusion

Congrats on deploying your NFT collection and minting your NFT!

If you got stuck along the way, jump into our Discord to speak directly with our team!