Skip to main content

RPC Client Usage guide

To interface with the Solana network, a client needs to construct and send JSON RPC requests to an RPC endpoint.

This guide will teach you how to use the SolanaRpcClient and send these RPC requests.

Add dependencies

The rpc-core library provides a convenient SolanaRpcClient that implements an API to call these RPC methods and return responses.

dependencies {
implementation("com.solanamobile:rpc-core:0.2.6")
}

Create an RPC Client

To create an instance of a SolanaRpcClient, pass in:

  • an RPC url that the client will send requests.
  • a networkDriver used to send HTTP requests.

In this example, we construct an RPC client pointed at devnet and Ktor as a network driver:

import com.solana.rpc.SolanaRpcClient
import com.solana.networking.KtorNetworkDriver

val rpcClient = SolanaRpcClient("https://api.devnet.solana.com", KtorNetworkDriver())

Example: Fetching latest blockhash

Calling the getLatestBlockhash method returns an RpcResponse.

  • If successful, the response result will contain a BlockhashResult.

  • If an error occured, the response will contain an RpcError.

import com.solana.rpc.SolanaRpcClient
import com.solana.networking.KtorNetworkDriver

val rpcClient = SolanaRpcClient("https://api.devnet.solana.com", KtorNetworkDriver())

val response = rpcClient.getLatestBlockhash()

if (response.result) {
println("Latest blockhash: ${response.result.blockhash}")
} else if (response.error) {
println("Failed to fetch latest blockhash: ${response.error.message}")
}

Example: Sending a transaction

To submit a transaction to the RPC, use the sendTransaction method.

  • If successful, the response result will contain a transaction signature string.

  • If an error occured, the response will contain an RpcError.

import com.solana.rpc.SolanaRpcClient
import com.solana.networking.KtorNetworkDriver

val rpcClient = SolanaRpcClient("https://api.devnet.solana.com", KtorNetworkDriver())

val transaction = Transaction(/* ... */)

/* ...sign the transaction... */

val response = rpc.sendTransaction(transaction)

if (response.result) {
println("Transaction signature: ${response.result}")
} else if (response.error) {
println("Failed to send transaction: ${response.error.message}")
}

Next steps

These examples are just some of the methods supported by SolanaRpcClient. Here are suggestions to continue learning:

  • Read the following guide to learn how to build Solana program instructions and transactions.
  • For a complete reference of the RPC methods supported, view the SolanaRpcClient source code and unit tests.
  • Read the Building JSON RPC requests deep dive to learn how to create requests for RPC methods that aren't immediately supported by SolanaRpcClient.