getTokenAccountBalance – Solana
The getTokenAccountBalance JSON-RPC method retrieves the token balance of a specified SPL Token account.
Last updated
Was this helpful?
Was this helpful?
https://go.getblock.io/<ACCESS-TOKEN>/curl --location "https://go.getblock.io/<ACCESS-TOKEN>/" -XPOST \
--header "Content-Type: application/json" \
--data '{
"jsonrpc": "2.0",
"id": 1,
"method": "getTokenAccountBalance",
"params": [
"7fUAJdStEuGbc3sM84cKRL6yYaaSstyLSU4ve5oovLS7"
]
}'{
"jsonrpc": "2.0",
"result": {
"context": {
"slot": 1114
},
"value": {
"amount": "9864",
"decimals": 2,
"uiAmount": 98.64,
"uiAmountString": "98.64"
}
},
"id": 1
}{
"jsonrpc": "2.0",
"error": {
"code": -32602,
"message": "Invalid Token Account Pubkey"
},
"id": 1
}const axios = require('axios');
const url = "https://go.getblock.io/<ACCESS-TOKEN>/";
const headers = { "Content-Type": "application/json" };
const payload = {
jsonrpc: "2.0",
id: 1,
method: "getTokenAccountBalance",
params: [
"7fUAJdStEuGbc3sM84cKRL6yYaaSstyLSU4ve5oovLS7"
]
};
const fetchTokenAccountBalance = async () => {
try {
const response = await axios.post(url, payload, { headers });
if (response.status === 200 && response.data.result?.value) {
const balance = response.data.result.value;
console.log("Token Account Balance:");
console.log(` Amount: ${balance.amount}`);
console.log(` Decimals: ${balance.decimals}`);
console.log(` UI Amount: ${balance.uiAmount}`);
console.log(` UI Amount String: ${balance.uiAmountString}`);
} else {
console.error("Unexpected response:", response.data);
}
} catch (error) {
console.error("getTokenAccountBalance error:", error.response?.data || error.message);
}
};
fetchTokenAccountBalance();