Libraries
Install @solana/web3.js (v1) for transaction handling and @solana/spl-token for SPL token operations:
npm install @solana/web3.js@1 @solana/spl-token bs58
This documentation uses @solana/web3.js v1. Version 2 has a different API for transaction handling.
RPC Connection
import { Connection } from '@solana/web3.js';
const connection = new Connection('https://api.mainnet-beta.solana.com');
Development Wallet
Never hardcode private keys in source code. Use environment variables or the Solana CLI keyfile.
import { Keypair } from '@solana/web3.js';
import bs58 from 'bs58';
import 'dotenv/config';
const wallet = Keypair.fromSecretKey(bs58.decode(process.env.PRIVATE_KEY));
import { Keypair } from '@solana/web3.js';
import fs from 'fs';
const privateKeyArray = JSON.parse(
fs.readFileSync('/Path/to/.config/solana/id.json', 'utf8').trim()
);
const wallet = Keypair.fromSecretKey(new Uint8Array(privateKeyArray));