How to Create Tokens With Metadata On Solana Using Token-2022

Create Solana SPL tokens with metadata using Token-2022 program — developer guide

Now that you have learnt about token extension, in this guide, you will be learning how to create a simple token with metadata extension

Prerequistics

Before you begin, ensure you have the following:

  1. Node installed on your PC, preferably v20+

  2. A package manager such as npm or yarn

  3. Solana Access token from GetBlockarrow-up-right

  4. Basic knowledge of JavaScript and Typescript

  5. Basic understanding of Solana (wallets, transactions, lamports)

Set up your environment

1

Create a new project directory:

mkdir token-2022-tutorial
cd token-2022-tutorial
npm init -y
2

Install required dependencies:

npm install @solana/web3.js @solana/spl-token @solana/spl-token-metadata dotenv
3

Create a .env file and add the following:

.env
RPC_URL=https://api.devnet.solana.com //or mainnet URL
PRIVATE_KEY=your-wallet-private-key

Configuration File

Create config.js file and add the following:

config.js
import { Connection, Keypair } from "@solana/web3.js";
import bs58 from "bs58";

import 'dotenv/config'

// Replace with your GetBlock endpoint
const GETBLOCK_RPC_URL = process.env.RPC_URL;

// Create connection to Solana via GetBlock
const connection = new Connection(GETBLOCK_RPC_URL, "confirmed");

// Load keypair from private key in .env 
const payer = Keypair.fromSecretKey(bs58.decode(process.env.PRIVATE_KEY));

export { connection, payer};

Create a Token with Metadata Extension

1

Create a new file index.js . This will serve as your working file

2

Import all the dependencies:

3

Generate an account for the mint address

4

Define the token metadata:

5

Calculate the metadata space and rent needed for the mint account:

This ensures our token mint account has sufficient space for the extensions you use or else the metadata initialization would fail with an insufficient lamports error.

6

Define the structure of the transaction:

This creates the mint address, initializes the mint, metadata, points to the mint itself as the metadata account, and sends the transaction.

chevron-rightComplete codehashtag
7

Run the code using this command:

8

Result:

Verify on the Solscanarrow-up-right:

Solscan verification

Conclusion

In this guide, you have learnt how to create a token with metadata extension. This guide also walks you through generating a Mint account for your token.

circle-info

This guide uses Solana Devnet. If you want to make use of Mainnet, go to your account dashboard arrow-up-rightand get a Solana mainnet access token.

Additional Resources

Last updated

Was this helpful?