getBalance

The getBalance RPC method provides a simple and efficient way to retrieve the native SOL balance of any account on the Solana blockchain. The balance is returned in lamports.

The getBalance RPC method provides a simple and efficient way to retrieve the native SOL balance of any account on the Solana blockchain. The balance is returned in lamports, where 1 SOL = 1,000,000,000 lamports. Unlike getAccountInfo, which returns detailed metadata, getBalance is ideal for lightweight balance checks.


🎯 Primary Use Case

  • Quick SOL Balance Check Instantly determine how much SOL an account—such as a wallet or program-owned address—holds.


🛠 Parameters

  • publicKey (string, required): The base-58 encoded public key of the account to query.

  • config (object, optional): Optional fields to control query behavior:

    • commitment (string): Determines the commitment level for the request:

      • finalized (default) – Highest level of confirmation.

      • confirmed – Recent vote-confirmed block.

      • processed – Most recent block (possibly unconfirmed).

    • minContextSlot (number): Minimum slot at which the query may be evaluated.

Public Key: 4UJuvGZ7Ge8H3je63Nsb9ZRNVBAd3CG2ajibnRaVSbw5

Sample Request (CoinVera RPC):

const fetch = require('node-fetch');

async function getBalance(rpcUrl) {
  try {
    const response = await fetch(rpcUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        id: 1,
        method: 'getBalance',
        "params": [
            "4UJuvGZ7Ge8H3je63Nsb9ZRNVBAd3CG2ajibnRaVSbw5"
          ]
      }),
    });

    const data = await response.json();
    
    // Print the exact full response
    console.log('Full RPC Response:');
    console.log(JSON.stringify(data, null, 2));
    
    return data;
  } catch (error) {
    console.error('Error getting health:', error.message);
    return null;
  }
}

// Example usage
const RPC_URL = 'https://rpc.coinvera.io/?x-api-key=your-coinvera-x-api-key';

getBalance(RPC_URL);

Example Response

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "context": {
      "slot": 355180145,
      "apiVersion": "2.2.7"
    },
    "value": 196248541
  }
}

🧠 Developer Tips (with CoinVera)

  • Use getBalance for Simplicity When you only need SOL balance, this method is significantly lighter than getAccountInfo and avoids unnecessary data transfer.

  • Account Existence Check If the account hasn't been initialized on-chain, getBalance returns 0. This makes it a quick way to check if an account exists for balance purposes only.

  • Convert Lamports to SOL Don’t forget: divide the result by LAMPORTS_PER_SOL (1,000,000,000) to display balances in SOL.

  • Commitment Strategy Choose your commitment level based on the application:

    • confirmed – Best for UI and general info.

    • finalized – Best for financial or high-assurance operations. See Solana’s commitment model for more on consistency vs. speed.

  • Scaling with getMultipleAccounts Need balances for multiple accounts? While getBalance only handles one at a time, using getMultipleAccounts and extracting lamport balances can be more efficient at scale.

Last updated