How to Use getBalance
Learn getBalance use cases, code examples, request parameters, response structure, and tips.
The getBalance
RPC method is a straightforward way to find out the native SOL balance of any account on the Solana blockchain. It returns the balance in lamports (1 SOL = 1,000,000,000 lamports).
This method is more lightweight than getAccountInfo
if you only need the SOL balance and no other account details.
Main Use Case
- Quickly Checking an Account’s SOL Holdings: The primary use is to determine how much SOL an account (wallet, program, etc.) holds.
Parameters
-
publicKey
(string, required): The base-58 encoded public key of the account to query. -
config
(object, optional): A configuration object with the following fields:commitment
(string, optional): Specifies the commitment level to use for the query. Defaults tofinalized
.finalized
: The node will query the most recent block confirmed by the supermajority of the cluster as having reached maximum lockout.confirmed
: The node will query the most recent block that has been voted on by a supermajority of the cluster.processed
: The node will query its most recent block. Note that the block may not be complete.
minContextSlot
(number, optional): The minimum slot that the request can be evaluated at.
Response
The result
field of the JSON-RPC response will be an object containing:
context
(object):slot
(number): The slot at which the balance was retrieved.apiVersion
(string, optional): The RPC API version (may not be present from all nodes).
value
(number): The balance of the account in lamports (unsigned 64-bit integer).
If the account does not exist on-chain, getBalance
will typically return a value of 0
lamports.
Example: Fetching an Account’s Balance
Let’s check the SOL balance of the Serum Program V3 ID (9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin
) on mainnet. This program account itself holds SOL for rent exemption.
Note: Replace YOUR_API_KEY
with your actual Helius API key in the examples below.
Developer Tips
- Simplicity for SOL Balance: If you only need an account’s SOL balance and no other on-chain data (like owner, data, or executable status),
getBalance
is more efficient thangetAccountInfo
as it fetches less data. - Non-Existent Accounts: If an account does not exist on-chain (has never been initialized or had SOL),
getBalance
will return0
. This can be a quick way to check for account existence if you only care about its SOL balance. - Lamports vs. SOL: Remember that the balance is returned in lamports. You’ll need to divide by
LAMPORTS_PER_SOL
(1,000,000,000) to convert it to SOL. - Commitment Levels: The choice of
commitment
can affect how quickly you get the balance and how confirmed that balance is. For most UI display purposes,confirmed
offers a good balance. For critical financial transactions,finalized
provides the highest assurance. See Solana Commitment Levels for detailed information. - Batching with
getMultipleAccounts
: WhilegetBalance
is for a single account, if you need balances for many accounts, usinggetMultipleAccounts
and then extracting the lamport balance from each account’s info can be more performant than many individualgetBalance
calls.