# 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](/guides/what-are-token-extensions/), 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+
1. A package manager such as npm or yarn
1. Solana Wallet, e.g., [Phantom](https://phantom.com/), [Solfare](https://www.solflare.com/), or [Metamask](https://metamask.io/)
1. Solana Access token from [GetBlock](https://account.getblock.io/)
1. Basic knowledge of JavaScript and Typescript
1. Basic understanding of Solana (wallets, transactions, lamports)

<https://youtu.be/Db-CSMJsLpM?si=ZK1LA3rHoEr7lSfN>

### Set up your environment 

Create a new project directory:

<pre class="language-bash"><code class="lang-bash">mkdir token-2022-tutorial
<strong>cd token-2022-tutorial
</strong>npm init -y
</code></pre>

Install required dependencies:

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

Create a `.env` file and add the following:

#### .env

```js
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

```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

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

Import all the dependencies:

```js
import { 
Keypair, 
SystemProgram, 
Transaction, 
sendAndConfirmTransaction } 
from "@solana/web3.js";

import { TOKEN_2022_PROGRAM_ID, 
ExtensionType, 
createInitializeMintInstruction, 
createInitializeMetadataPointerInstruction, 
getMintLen, 
TYPE_SIZE, 
LENGTH_SIZE } 
from "@solana/spl-token";

import { createInitializeInstruction, pack } from "@solana/spl-token-metadata";
import { connection, payer } from "./config.js";
```

Generate an account for the mint address

```js
async function createTokenWithMetadata() {
  console.log("=== Creating Token with Metadata Extension ===\n");
  console.log("Payer address:", payer.publicKey.toBase58());
  const mintKeypair = Keypair.generate();
  const mint = mintKeypair.publicKey;
  console.log("\nMint address:", mint.toBase58());
```

Define the token metadata:

```js
 const metadata = {
   updateAuthority: payer.publicKey,
   mint: mint,
   name: "GetBlock Tutorial Token",
   symbol: "GBT",
   uri: "https://getblock.io/",
   additionalMetadata: [["description", "A test token created with GetBlock"]],
 };
```

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

```js
const decimals = 9;
const mintLen = getMintLen([ExtensionType.MetadataPointer]);
const metadataLen =
  TYPE_SIZE + // 2 bytes for type
  LENGTH_SIZE + // 2 bytes for length
  pack(metadata).length; // Actual metadata size
const lamports = await connection.getMinimumBalanceForRentExemp
  mintLen + metadataLen,
);
```

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.

Define the structure of the transaction:

```js
const transaction = new Transaction().add(
  SystemProgram.createAccount({
    fromPubkey: payer.publicKey,
    newAccountPubkey: mint,
    space: mintLen, // ← Just the mint size
    lamports: lamports, // ← But funded for full size
    programId: TOKEN_2022_PROGRAM_ID,
  }),
  createInitializeMetadataPointerInstruction(
    mint, // mint
    payer.publicKey, // authority
    mint, // metadata address (pointing to itself)
    TOKEN_2022_PROGRAM_ID,
  ),
  createInitializeMintInstruction(
    mint, // mint
    decimals, // decimals
    payer.publicKey, // mint authority
    null, // freeze authority (optional)
    TOKEN_2022_PROGRAM_ID,
  ),
  createInitializeInstruction({
    programId: TOKEN_2022_PROGRAM_ID,
    mint: mint,
    metadata: mint,
    name: metadata.name,
    symbol: metadata.symbol,
    uri: metadata.uri,
    mintAuthority: payer.publicKey,
    updateAuthority: payer.publicKey,
  }),
);
console.log("\nSending transaction...");
const signature = await sendAndConfirmTransaction(
  connection,
  transaction,
  [payer, mintKeypair], // Signers
  { commitment: "confirmed" },
);
console.log("\Token created successfully!");
console.log("Transaction signature:", signature);
console.log("Mint address:", mint.toBase58());
console.log("\nToken details:");
console.log("  Name:", metadata.name);
console.log("  Symbol:", metadata.symbol);
console.log("  Decimals:", decimals);
console.log("  URI:", metadata.uri);
return mint;
}
ceateTokenWithMetadata()
.then(() => process.exit(0))
.catch((error) => {
  console.error("Error:", error);
  process.exit(1);
});
```

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

#### Complete code

#### index.js

```js
import { Keypair, SystemProgram, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import { TOKEN_2022_PROGRAM_ID, ExtensionType, createInitializeMintInstruction, createInitializeMetadataPointerInstruction, getMintLen, TYPE_SIZE, LENGTH_SIZE } from "@solana/spl-token";
import { createInitializeInstruction, pack } from "@solana/spl-token-metadata";
import { connection, payer } from "./config.js";
async function createTokenWithMetadata() {
  console.log("=== Creating Token with Metadata Extension ===\n");
  console.log("Payer address:", payer.publicKey.toBase58());
  const mintKeypair = Keypair.generate();
  const mint = mintKeypair.publicKey;
  console.log("\nMint address:", mint.toBase58());
  const metadata = {
    updateAuthority: payer.publicKey,
    mint: mint,
    name: "GetBlock Tutorial Token",
    symbol: "GBT",
    uri: "https://getblock.io/",
    additionalMetadata: [["description", "A test token created with GetBlock"]],
  };
  const decimals = 9;
  const mintLen = getMintLen([ExtensionType.MetadataPointer]);
  const metadataLen =
    TYPE_SIZE + // 2 bytes for type
    LENGTH_SIZE + // 2 bytes for length
    pack(metadata).length; // Actual metadata size
  const lamports = await connection.getMinimumBalanceForRentExemption(
    mintLen + metadataLen,
  );
  const transaction = new Transaction().add(
    SystemProgram.createAccount({
      fromPubkey: payer.publicKey,
      newAccountPubkey: mint,
      space: mintLen, // ← Just the mint size
      lamports: lamports, // ← But funded for full size
      programId: TOKEN_2022_PROGRAM_ID,
    }),
    createInitializeMetadataPointerInstruction(
      mint, // mint
      payer.publicKey, // authority
      mint, // metadata address (pointing to itself)
      TOKEN_2022_PROGRAM_ID,
    ),
    createInitializeMintInstruction(
      mint, // mint
      decimals, // decimals
      payer.publicKey, // mint authority
      null, // freeze authority (optional)
      TOKEN_2022_PROGRAM_ID,
    ),
    createInitializeInstruction({
      programId: TOKEN_2022_PROGRAM_ID,
      mint: mint,
      metadata: mint,
      name: metadata.name,
      symbol: metadata.symbol,
      uri: metadata.uri,
      mintAuthority: payer.publicKey,
      updateAuthority: payer.publicKey,
    }),
  );
  console.log("\nSending transaction...");
  const signature = await sendAndConfirmTransaction(
    connection,
    transaction,
    [payer, mintKeypair], // Signers
    { commitment: "confirmed" },
  );

  console.log("\Token created successfully!");
  console.log("Transaction signature:", signature);
  console.log("Mint address:", mint.toBase58());
  console.log("\nToken details:");
  console.log("  Name:", metadata.name);
  console.log("  Symbol:", metadata.symbol);
  console.log("  Decimals:", decimals);
  console.log("  URI:", metadata.uri);

  return mint;
}
createTokenWithMetadata()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error("Error:", error);
    process.exit(1);
  });

```

Run the code using this command:

```bash
node index.js
```

Result: 

```bash
=== Creating Token with Metadata Extension ===

Payer address: 5C7dUe5ZDWBCYQ5eqtGPRyz7CQYk2aK4FhZTDYQeji7r

Mint address: BVwyGVpUNUL7eYVqbMtzgbinTiUgd3tNXsgxfxoAQaWe

Sending transaction...

Token created successfully!
Transaction signature: N9DHUYdNRUc9RC2H6LMtiSPpY2UW1aVmFZsK7b8KG3xpTUWiVDrj79EkxHTh74T5QmbMR3CbprYif3jNveAnQBJ
Mint address: BVwyGVpUNUL7eYVqbMtzgbinTiUgd3tNXsgxfxoAQaWe

Token details:
  Name: GetBlock Tutorial Token
  Symbol: GBT
  Decimals: 9
  URI: https://getblock.io/
```

Verify on the [Solscan](https://solscan.io/token/BVwyGVpUNUL7eYVqbMtzgbinTiUgd3tNXsgxfxoAQaWe?cluster=devnet): 

![Solscan verification](/docs-assets/Screenshot%202026-03-22%20at%2010.14.03%E2%80%AFPM.png)

### 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. 

> **Info:** This guide uses Solana Devnet. If you want to make use of Mainnet, go to your [account dashboard ](https://account.getblock.io/)and get a Solana mainnet access token.

#### Additional Resources

1. [Token Metadata Interface](https://github.com/solana-program/token-metadata/tree/main)
1. [Solana Token](https://solana.com/docs/tokens)
1. [What are Token Extensions](/guides/what-are-token-extensions/)
1. [Solana program](https://github.com/solana-program)
1. [GetBlock Account Dashboard](https://account.getblock.io/)
1. [Create Token With Metadata Repo](https://github.com/GetBlock-io/guides/blob/main/token-2022-tutorial/create-token-with-metadata.js)
