Logo

What is the Solana RPC API and how do you use it?

Key Takeaways

  • The Solana RPC API uses JSON-RPC 2.0 over HTTP and WebSocket for request-response and subscription methods.
  • Public endpoints like api.mainnet.solana.com are rate-limited and not suitable for production apps.
  • Key RPC methods include getAccountInfo, getBalance, getTransaction, sendTransaction, and getProgramAccounts.
  • State commitment levels (processed, confirmed, finalized) control how recently data is considered final.
  • For production, use a managed RPC provider like OnFinality to get dedicated nodes, higher throughput, and reliable uptime.
  • WebSocket subscriptions enable real-time updates for accounts, logs, and transactions.
  • Optimize RPC usage by caching responses, batching requests, and avoiding polling where possible.

What is the Solana RPC API?

The Solana RPC API is a JSON-RPC 2.0 interface that allows applications to communicate with Solana nodes. It exposes methods for querying account balances, retrieving transaction history, sending transactions, and subscribing to events via WebSocket. Every interaction with the Solana blockchain—whether from a wallet, dApp, or backend service—goes through an RPC endpoint.

Solana's architecture is designed for high throughput, and the RPC API reflects that with methods optimized for performance. However, public RPC endpoints are shared infrastructure and come with rate limits that can hinder production applications. For reliable access, many developers turn to managed RPC providers like OnFinality.

  • JSON-RPC 2.0 over HTTP POST for request-response calls
  • WebSocket connections for real-time subscriptions
  • Supports mainnet, devnet, and testnet clusters
  • Methods cover accounts, tokens, transactions, blocks, and cluster info

Solana RPC Endpoints and Clusters

Solana operates three public clusters: mainnet-beta, devnet, and testnet. Each has a public RPC endpoint, but these are rate-limited and not recommended for production use. The official endpoints are:

For development and testing, devnet and testnet provide free access with faucets for test SOL. Production applications should use a dedicated RPC provider to ensure reliability and scalability.

CriterionWhat to checkWhy it matters
ClusterPublic EndpointUse for testing or light usage
Mainnethttps://api.mainnet.solana.comProduction network with real SOL
Devnethttps://api.devnet.solana.comDeveloper testing with free SOL from faucet
Testnethttps://api.testnet.solana.comValidator testing, less stable

Key Solana RPC Methods

Each method accepts parameters and returns a JSON response. For example, getBalance takes a base58-encoded public key and returns the lamport balance. Always check the official documentation for parameter details and response formats.

  • Accounts: getAccountInfo, getBalance, getMultipleAccounts
  • Transactions: getTransaction, sendTransaction, simulateTransaction
  • Tokens: getTokenAccountBalance, getTokenAccountsByOwner, getTokenSupply
  • Blocks: getBlock, getBlocks, getBlockHeight
  • Cluster: getEpochInfo, getSlot, getHealth
  • Programs: getProgramAccounts (heavy method, use with filters)

State Commitment and Finality

Solana RPC methods accept an optional commitment parameter that controls how recently the data must be confirmed. The three levels are:

Choosing the right commitment level is crucial for your application. For example, a wallet might use 'confirmed' for transaction status, while a block explorer might use 'finalized' for historical data.

CriterionWhat to checkWhy it matters
CommitmentDescriptionTrade-off between speed and finality
processedNode's most recent block (may be rolled back)Fastest, but not final
confirmedBlock voted by cluster (supermajority)Good balance for most apps
finalizedBlock confirmed by full cluster (>= 32 confirmations)Maximum safety, slower

WebSocket Subscriptions

In addition to HTTP methods, the Solana RPC API supports WebSocket subscriptions for real-time updates. You can subscribe to account changes, program logs, transaction confirmations, and slot updates. This is more efficient than polling.

To use subscriptions, connect to the WebSocket endpoint (e.g., wss://api.mainnet.solana.com) and send a JSON-RPC subscribe request. The server will push notifications until you unsubscribe.

  • accountSubscribe: Notify when an account's data changes
  • logsSubscribe: Listen to program logs
  • signatureSubscribe: Get notified when a transaction is confirmed
  • slotSubscribe: Receive updates when a new slot is produced

Best Practices for Using the Solana RPC API

For high-throughput applications, consider dedicated nodes. OnFinality offers dedicated Solana nodes with isolated resources, ensuring consistent performance even under heavy load.

  • Avoid polling for data that changes infrequently; use WebSocket subscriptions instead.
  • Cache responses for expensive methods like getProgramAccounts and getSignaturesForAddress.
  • Batch requests when possible using JSON-RPC batch calls.
  • Use a managed RPC provider for production to avoid rate limits and ensure uptime.
  • Monitor your RPC usage and set up alerts for error rates or latency spikes.
  • Test on devnet before deploying to mainnet.

Choosing a Solana RPC Provider

Public RPC endpoints are fine for development but insufficient for production. Managed providers offer higher rate limits, dedicated nodes, enhanced APIs, and support. When evaluating providers, consider:

OnFinality provides Solana RPC endpoints with flexible plans, including shared and dedicated node options. You can get started quickly with a free tier and scale as your application grows.

CriterionWhat to checkWhy it matters
FeatureWhy It MattersImpact on your app
Rate limitsRequests per second allowedLimits your throughput
Uptime SLAGuaranteed availabilityReliability for users
Dedicated nodesIsolated infrastructureConsistent performance
Enhanced APIsAdditional endpoints beyond standard RPCSimplify development
Global coverageGeographic distribution of endpointsLower latency worldwide

Frequently Asked Questions

What is the difference between Solana RPC HTTP and WebSocket methods?

HTTP methods are request-response: you send a request and get a response. WebSocket methods allow you to subscribe to events and receive push notifications when data changes, which is more efficient for real-time updates.

How do I get free SOL for testing on Solana devnet?

You can use the Solana Faucet at https://faucet.solana.com to request devnet SOL. Some RPC providers also offer faucet endpoints.

What is a 'heavy' RPC method on Solana?

Heavy methods like getProgramAccounts can consume significant node resources because they scan large account spaces. Always use filters (e.g., by data size or owner) to limit the result set.

Can I use the public Solana RPC endpoint for production?

Public endpoints are rate-limited and shared among all users, making them unsuitable for production. Use a managed RPC provider like OnFinality for reliable, scalable access.

How do I send a transaction via the Solana RPC API?

Use the sendTransaction method with a signed transaction in base58 or base64 encoding. The method returns the transaction signature if successful.

solana rpc api
RPC Knowledge Base

Related RPC details

Background

永远不用担心基础设施

OnFinality 消除了 DevOps 的繁重工作,让您能够更聪明、更快地构建。

开始