Logo

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

Key Takeaways

  • The Solana API uses JSON-RPC 2.0 over HTTP POST and WebSocket for subscriptions.
  • Key HTTP methods include getBalance, getTransaction, sendTransaction, and getProgramAccounts.
  • Public RPC endpoints are rate-limited and unsuitable for production; use a dedicated provider.
  • Solana offers three main clusters: Mainnet, Devnet, and Testnet, each with distinct purposes.
  • WebSocket subscriptions enable real-time updates for accounts, logs, and programs.
  • Choosing a provider with low latency and high reliability is critical for user experience.
  • OnFinality provides scalable Solana RPC endpoints with dedicated node options for production workloads.

What is the Solana API?

The Solana API is a JSON-RPC interface that enables applications to communicate with the Solana blockchain. It follows the JSON-RPC 2.0 specification and supports both HTTP and WebSocket transports. Through this API, developers can query account balances, send transactions, fetch block data, and subscribe to real-time events.

The official Solana documentation provides a comprehensive list of RPC methods, but for production applications, relying on public endpoints is not recommended due to rate limits and reliability concerns. Instead, developers typically use third-party RPC providers that offer dedicated infrastructure.

Solana API Methods Overview

The Solana API includes dozens of methods organized into categories: accounts, tokens, transactions, blocks, cluster information, and economics. Some of the most commonly used methods are:

  • getBalance – Returns the SOL balance of an account.
  • getTransaction – Retrieves a confirmed transaction by signature.
  • sendTransaction – Submits a signed transaction to the cluster.
  • getProgramAccounts – Returns all accounts owned by a program.
  • getLatestBlockhash – Gets the latest block hash for transaction construction.
  • simulateTransaction – Simulates a transaction without committing it.
  • getSlot – Returns the current slot number.
  • getBlock – Fetches a block by its slot number.

Connecting to Solana Clusters

Solana operates three main clusters: Mainnet, Devnet, and Testnet. Each cluster has a public RPC endpoint, but these are shared and rate-limited. For production applications, you should use a dedicated RPC provider that offers higher throughput and reliability.

The public endpoints are:

Mainnet: https://api.mainnet.solana.com

Devnet: https://api.devnet.solana.com

Testnet: https://api.testnet.solana.com

  • Mainnet – Production network using real SOL. Use for live dApps.
  • Devnet – Developer testing network with free SOL from the faucet. Ideal for development and testing.
  • Testnet – Validator testing network. Used for network performance testing.

Making Your First API Call

Here's a simple example using curl to call the getSlot method on the Solana Mainnet via a public endpoint:

curl https://api.mainnet.solana.com -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"getSlot","params":[]}'

For production, you would replace the public endpoint with a provider-specific URL. For example, using OnFinality's Solana RPC endpoint:

curl https://solana.api.onfinality.io/public -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"getSlot","params":[]}'

WebSocket Subscriptions

The Solana API also supports WebSocket subscriptions for real-time updates. You can subscribe to account changes, program logs, and slot updates. This is useful for building responsive dApps that react to on-chain events.

Example subscription to account changes:

wscat -c wss://api.mainnet.solana.com

> {"jsonrpc":"2.0","id":1,"method":"accountSubscribe","params":["PUBLIC_KEY",{"commitment":"finalized"}]}

Choosing a Solana RPC Provider

When selecting a Solana RPC provider for production, consider the following criteria:

CriterionWhat to checkWhy it matters
Uptime and ReliabilityService Level Agreements (SLAs) and historical uptime statistics.Downtime can break your application and cause loss of user trust.
LatencyGeographic distribution of endpoints and response times.Low latency improves user experience, especially for real-time applications.
Rate LimitsRequests per second (RPS) limits and whether they scale with your needs.Rate limits can throttle your application during peak usage.
Dedicated vs Shared NodesWhether the provider offers dedicated nodes with guaranteed resources.Dedicated nodes provide consistent performance and are essential for high-traffic applications.
Network SupportAvailability of Mainnet, Devnet, and Testnet endpoints.You need access to test networks for development and staging.

Why Use OnFinality for Solana RPC?

OnFinality offers reliable Solana RPC endpoints with support for Mainnet, Devnet, and Testnet. Our infrastructure is designed for production workloads, with low latency and high availability. Whether you need a shared public endpoint or a dedicated node, OnFinality provides flexible options to match your scale.

Get started with a free API key and experience the performance and reliability that OnFinality delivers.

Frequently Asked Questions

What is the Solana API?

The Solana API is a JSON-RPC interface that allows developers to interact with the Solana blockchain. It provides methods for querying network state, sending transactions, and subscribing to real-time events.

How do I get a Solana API key?

You can get a free API key from providers like OnFinality. Simply sign up and create an API key to access Solana RPC endpoints.

What is the difference between Solana Mainnet, Devnet, and Testnet?

Mainnet is the production network using real SOL. Devnet is for developers to test applications with free SOL. Testnet is for validator testing and network performance.

Can I use the public Solana RPC endpoint for production?

Public endpoints are rate-limited and shared, making them unsuitable for production. Use a dedicated RPC provider for reliable, scalable access.

What are the most common Solana API methods?

Common methods include getBalance, getTransaction, sendTransaction, getProgramAccounts, getLatestBlockhash, and getSlot.

solana api
RPC Knowledge Base

Related RPC details

Background

永远不用担心基础设施

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

开始