Web3.js & Solana SDK
JavaScript and TypeScript development with @solana/web3.js.
@solana/web3.js is the official JavaScript/TypeScript SDK for interacting with Solana. It provides classes and functions for connecting to RPC nodes, building transactions, managing keypairs, and interacting with programs.
Connection
Create a Connection object with your RPC endpoint URL. Use the 'confirmed' commitment level for most reads. For production, use a premium RPC provider rather than the public endpoint.
Keypairs and Wallets
Use Keypair.generate() for new keypairs or Keypair.fromSecretKey() to load existing ones. For browser wallets, use the @solana/wallet-adapter library which provides a unified interface for Phantom, Solflare, and other wallets.
Building Transactions
Use TransactionMessage and VersionedTransaction for modern transaction construction. Include ComputeBudgetProgram instructions to set compute limits and priority fees. Use address lookup tables (ALTs) when you need to reference more than 32 accounts.
Sending Transactions
Use connection.sendTransaction() with appropriate options. Set skipPreflight: true for time-sensitive transactions to avoid simulation overhead. Implement retry logic with exponential backoff for failed transactions.
Version 2 vs Version 1
@solana/web3.js v2 is a complete rewrite with a functional API, better TypeScript types, and improved tree-shaking. For new projects starting in 2025, consider v2. For existing projects, v1 is stable and well-supported.