getTokenAccountBalance
Returns the token balance for an SPL Token account.
Request
Send a JSON-RPC 2.0 POST request with method: "getTokenAccountBalance". The params array takes the base-58 encoded public key of an SPL Token account and an optional configuration object.
curl · JSON-RPCjson
{ "jsonrpc": "2.0", "id": 1, "method": "getTokenAccountBalance", "params": [ "48gpnn8nsmkvkgso7462Z1nFhUrprGQ71u1YLBPzizbY", { "commitment": "finalized" } ]}@solana/kit
kit.tsts
import { address, createSolanaRpc } from "@solana/kit"; const rpc_url = "https://rpc.openinfra.sh";const rpc = createSolanaRpc(rpc_url); let tokenAddress = address("48gpnn8nsmkvkgso7462Z1nFhUrprGQ71u1YLBPzizbY"); let tokenBalance = await rpc.getTokenAccountBalance(tokenAddress).send(); console.log(tokenBalance);@solana/web3.js
web3.tsts
import { Connection, PublicKey } from "@solana/web3.js"; const connection = new Connection("https://rpc.openinfra.sh", "confirmed"); let tokenAddress = new PublicKey( "48gpnn8nsmkvkgso7462Z1nFhUrprGQ71u1YLBPzizbY"); let tokenBalance = await connection.getTokenAccountBalance(tokenAddress); console.log(tokenBalance);Rust
main.rsrs
use anyhow::Result;use solana_client::nonblocking::rpc_client::RpcClient;use solana_commitment_config::CommitmentConfig;use solana_sdk::pubkey; #[tokio::main]async fn main() -> Result<()> { let client = RpcClient::new_with_commitment( String::from("https://rpc.openinfra.sh"), CommitmentConfig::confirmed(), ); let token_address = pubkey!("48gpnn8nsmkvkgso7462Z1nFhUrprGQ71u1YLBPzizbY"); let token_acc_bal = client.get_token_account_balance(&token_address).await?; println!("{:#?}", token_acc_bal); Ok(())}Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| pubkey | string | Yes | Pubkey of the Token account to query, as a base-58 encoded string. |
| config | object | No | Optional configuration object. See fields below. |
| config.commitment | string | No | Desired finality level for the query. Accepted values: processed, confirmed, finalized (default). |
commitment values
| Value | Description |
|---|---|
| processed | Return data from the highest slot this node has processed on the fork it currently considers best. This is the newest view, but it can still change if the cluster switches forks. |
| confirmed | Return data from the highest slot that at least two-thirds of active stake has directly voted to confirm. This is more stable than processed, but it is still a weaker guarantee than finalized. |
| finalized | Return data from the highest slot that the cluster recognizes as finalized. In practice, this means the slot has reached maximum vote lockout in validators' vote towers and is recognized by at least two-thirds of active stake. This is the strongest commitment level. |
Response
response.jsonjson
{ "jsonrpc": "2.0", "result": { "context": { "apiVersion": "3.1.8", "slot": 1114 }, "value": { "amount": "9864", "decimals": 2, "uiAmount": 98.64, "uiAmountString": "98.64" } }, "id": 1}The result is an RpcResponse object with a context field and a value field containing token amount details.
context
| Field | Type | Description |
|---|---|---|
| slot | u64 | Slot at which the node evaluated this request. |
| apiVersion | string | RPC API version reported by the node. This field may be omitted by older nodes. |
value
| Field | Type | Description |
|---|---|---|
| amount | string | Raw token balance as a base-10 integer string with no decimal point. |
| decimals | u8 | Number of decimal places configured on the mint. |
| uiAmount | number | null | Decimal-scaled token balance as a floating-point number. Deprecated in favor of uiAmountString. |
| uiAmountString | string | Decimal-scaled token balance as a string. |