Skip to main content

Making RPC requests

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

Add dependencies

The @solana/web3.js library provides a convenient RPC client Connection class that has an API for submitting RPC requests to a JSON RPC endpoint.

yarn install @solana/web3.js \
react-native-get-random-values \
buffer

Add polyfills

After installing, ensure you have also added these polyfills to the index.js of your React native app. These are needed in some parts of @solana/web3.js because it is originally written as a web/node library and, as a result, certain expected APIs are missing in a React Native environment.

Creating a Connection client

The Connection class represents a connection to a Solana RPC endpoint and provides convenient functions to make RPC requests.

Construct a Connection client by passing in an RPC endpoint and an optional commitment config:

const connection = new Connection("https://api.devnet.solana.com", "confirmed");

The Connection class created can be reused throughout your application.

Usage

After creation, call various asynchronous RPC functions and receive responses from the RPC endpoint.

// `getLatestBlockhash` RPC request
const blockhash = await connection.getLatestBlockhash();

// `getBalance` RPC request
const balanceInLamports = await connection.getBalance();

// Sending a Transaction
const txSignature = await sendTransaction(tx);

View the official documentation to see the full list of available RPC functions, parameter types, and response types.

Next steps

  • Read the following Building transactions guide to learn how to create transactions that interact with on-chain Solana Programs.
  • Browse the full list of Solana RPC HTTP Methods