RPC Basics
10 examples to get you started with Solana JSON-RPC - 7 basic and 3 intermediate.
Prerequisites
- RPC endpoint URL (public devnet or provider)
@solana/kit7.0.0 for TypeScript examples
npm install @solana/kit@7.0.0Basic Examples
1. HTTP JSON-RPC Shape
Solana nodes speak JSON-RPC 2.0 over HTTP POST.
curl https://api.devnet.solana.com -s -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0",
"id": 1,
"method": "getHealth"
}'- Methods are
camelCaselikegetBalance,getSlot. - Responses include
resultorerrorwith code and message. - Same URL often supports WebSocket on a different port/path for subscriptions.
2. Create an RPC Client with Kit
Type-safe reads with @solana/kit 7.0.0.
import { createSolanaRpc, address } from "@solana/kit";
const rpc = createSolanaRpc("https://api.devnet.solana.com");
const health = await rpc.getHealth().send();
console.log(health);createSolanaRpcwraps fetch-based JSON-RPC calls..send()executes the request and returns typed results.- Swap URL for provider endpoints in production.
Related: Core RPC Methods - common methods
3. Commitment: processed
Fastest reads - may roll back on forks.
import { createSolanaRpc, address } from "@solana/kit";
const rpc = createSolanaRpc("https://api.devnet.solana.com");
const slot = await rpc.getSlot({ commitment: "processed" }).send();
console.log("processed slot:", slot);- Use for UI polling where slight staleness is OK.
- Not ideal for financial finality decisions.
- Default client behavior may differ - set explicitly.
4. Commitment: confirmed
Default for most application reads.
const balance = await rpc
.getBalance(address("11111111111111111111111111111111"), {
commitment: "confirmed",
})
.send();
console.log("lamports:", balance.value);- Supermajority voted on slot - practical balance for dApps.
- Pair with
confirmedon sends before showing success UI. - CLI default commitment aligns with this level.
5. Commitment: finalized
Safest reads for treasury and accounting.
const account = await rpc
.getAccountInfo(address("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), {
commitment: "finalized",
encoding: "base64",
})
.send();
console.log(account.value?.lamports);- Finalized cannot be rolled back under normal network conditions.
- Slower - wait minutes on mainnet after writes.
- Use for withdrawals and irreversible UI states.
6. Check Rate Limit Headers
Providers return HTTP 429 when throttled.
curl -i https://api.devnet.solana.com -s -X POST -H "Content-Type: application/json" -d '
{"jsonrpc":"2.0","id":1,"method":"getSlot"}' | head -20- Public endpoints throttle aggressively - expect 429 in CI.
- Back off exponentially and cache reads.
- Dedicated provider keys raise limits.
Related: RPC Providers - provider comparison
7. CLI Uses the Same RPC
Terminal commands hit the same JSON-RPC as your app.
solana config set --url https://api.devnet.solana.com
solana slotsolana slotmaps togetSlot.- Debugging starts by aligning CLI URL and app
RPC_URL. - Commitment flags mirror RPC parameters.
Intermediate Examples
8. Batch Requests (Concept)
Reduce round trips by batching JSON-RPC in one HTTP body.
import { createSolanaRpc, address } from "@solana/kit";
const rpc = createSolanaRpc("https://api.devnet.solana.com");
const [slot, blockHeight] = await Promise.all([
rpc.getSlot().send(),
rpc.getBlockHeight().send(),
]);
console.log({ slot, blockHeight });- Parallel
Promise.allstill uses multiple HTTP requests unless provider supports batch arrays. - Prefer
getMultipleAccountsover NgetAccountInfocalls. - See pagination page for scan patterns.
Related: Pagination & Efficiency - efficient reads
9. WebSocket vs HTTP
Subscriptions need WebSocket transport.
# HTTP for one-shot reads
curl https://api.devnet.solana.com ...
# WS for accountSubscribe (kit or ws client)accountSubscribe,logsSubscriberequire persistent socket.- Provider WSS URL may differ from HTTPS RPC URL.
- HTTP remains best for stateless reads at scale.
Related: WebSocket Subscriptions - live updates
10. DAS and Standard RPC
Digital Asset Standard API extends reads for NFTs and compressed assets.
// DAS methods (e.g. getAsset) live on provider DAS-enabled endpoints
// Standard getAccountInfo still underlies raw account bytes- Not every public RPC exposes DAS - check provider feature matrix.
- Use DAS for asset metadata; use
getAccountInfofor custom program accounts. - See DAS page for method list.
Related: The DAS API - asset reads
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.