I’m working with the Solana web3 SDK and need help adding metadata to my NFT.
I can successfully create tokens using splToken.Token.createMint and the transaction shows up in the explorer with a valid signature. However, I can’t figure out how to attach the basic NFT properties during the minting process.
What I need to add:
- Token name
- Symbol
- Image URI
- Decimal places
What I’ve tried:
I looked into Metaplex and the token-metadata standard but found limited documentation and examples for Solana development.
My current code:
const solanaWeb3 = require('@solana/web3.js');
const tokenProgram = require('@solana/spl-token');
(async () => {
// Establish connection to devnet
const rpcConnection = new solanaWeb3.Connection(
solanaWeb3.clusterApiUrl('devnet'),
'confirmed',
);
// Create wallet and request airdrop
var creatorWallet = solanaWeb3.Keypair.generate();
console.log("generated creator wallet ", creatorWallet);
var airdropTx = await rpcConnection.requestAirdrop(
creatorWallet.publicKey,
solanaWeb3.LAMPORTS_PER_SOL,
);
await rpcConnection.confirmTransaction(airdropTx);
// Create recipient wallet
const recipientWallet = solanaWeb3.Keypair.generate();
// Initialize new token mint
const tokenMint = await tokenProgram.Token.createMint(
rpcConnection,
creatorWallet,
creatorWallet.publicKey,
null,
9,
tokenProgram.TOKEN_PROGRAM_ID,
);
// Setup token accounts
const creatorTokenAccount = await tokenMint.getOrCreateAssociatedAccountInfo(
creatorWallet.publicKey,
);
const recipientTokenAccount = await tokenMint.getOrCreateAssociatedAccountInfo(
recipientWallet.publicKey,
);
// Mint tokens
await tokenMint.mintTo(
creatorTokenAccount.address,
creatorWallet.publicKey,
[],
1000000000,
);
// Transfer tokens
const transferTx = new solanaWeb3.Transaction().add(
tokenProgram.Token.createTransferInstruction(
tokenProgram.TOKEN_PROGRAM_ID,
creatorTokenAccount.address,
recipientTokenAccount.address,
creatorWallet.publicKey,
[],
1,
),
);
const txSignature = await solanaWeb3.sendAndConfirmTransaction(
rpcConnection,
transferTx,
[creatorWallet],
{commitment: 'confirmed'},
);
console.log('Transaction signature:', txSignature);
})();
Where exactly should I add the metadata creation step? Any working examples would be really helpful.