Solana
准备好在生产环境中调用了吗?
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
Solana
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
@solana/web3.js is the official JavaScript and TypeScript client for Solana, maintained by Solana Labs. Install it with npm install @solana/web3.js. It ships its own TypeScript type definitions, so RPC responses, PublicKey, Keypair, and transaction types are fully typed out of the box, and the same package runs in Node.js and in the browser — making it the natural choice for both backends and web wallets.
The Connection object is the entry point for every RPC call you make to Solana. Its second argument sets the default commitment (processed, confirmed, or finalized) applied to reads, and passing a wsEndpoint alongside it enables WebSocket PubSub subscriptions over wss://solana.therpc.io/YOUR_API_KEY.
Construct a Connection against the Solana HTTP endpoint https://solana.therpc.io/YOUR_API_KEY. A default commitment of confirmed suits most applications: it returns state a supermajority of the cluster has voted on, arriving in roughly a slot or two (slots are about 400ms) while still being safe to act on. From there getBalance returns lamports — divide by LAMPORTS_PER_SOL for SOL — and getAccountInfo returns the account's owner program, lamports, and data.
To move SOL, build a Transaction with a SystemProgram.transfer instruction. sendAndConfirmTransaction handles the busywork for you: it fetches a fresh blockhash from getLatestBlockhash (Solana transactions expire fast, so a recent blockhash is required), signs with the provided keypairs, broadcasts the transaction, and polls its status until it reaches confirmation before returning the signature.
Connection RPC client, transaction building, Keypair and PublicKey helpers, WebSocket subscriptions, and SPL token support via the companion @solana/spl-token package.Methods like onAccountChange, onLogs, and onSlotChange open a Solana PubSub stream over the wss://solana.therpc.io/YOUR_API_KEY endpoint, pushing updates as soon as the watched account, log, or slot advances — no polling required. Each returns a numeric subscription id; pass it to the matching remover (for example removeAccountChangeListener) to unsubscribe and free the server-side resources when you no longer need the stream.
@solana/web3.js is written in TypeScript and bundles its own declaration files, so you get full typing without installing any @types/* packages. That means IDE autocompletion across the API, compile-time checking that catches mistakes like passing a string where a PublicKey is expected, and strongly typed RPC responses such as AccountInfo<Buffer> from getAccountInfo.