getTokenAccountsByOwner
Returns SPL Token accounts whose owner matches the supplied address.
Request
Send a JSON-RPC 2.0 POST request with method: "getTokenAccountsByOwner". The params array takes the owner pubkey, a token account filter object, and an optional configuration object.
curl · JSON-RPCjson
{ "jsonrpc": "2.0", "id": 1, "method": "getTokenAccountsByOwner", "params": [ "A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd", { "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" }, { "commitment": "finalized", "encoding": "jsonParsed" } ]}@solana/kit
kit.tsts
import { address, createSolanaRpc } from "@solana/kit"; const rpc_url = "https://rpc.openinfra.sh";const rpc = createSolanaRpc(rpc_url); let owner = address("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF"); let tokenProgram = address("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); let tokenAccounts = await rpc .getTokenAccountsByOwner( owner, { programId: tokenProgram }, { commitment: "finalized", encoding: "jsonParsed" } ) .send(); console.log(tokenAccounts);@solana/web3.js
web3.tsts
import { Connection, PublicKey } from "@solana/web3.js"; const connection = new Connection("https://rpc.openinfra.sh", "confirmed"); let owner = new PublicKey("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF"); let tokenProgram = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); let tokenAccounts = await connection.getTokenAccountsByOwner(owner, { programId: tokenProgram}); console.log(tokenAccounts);Rust
main.rsrs
use anyhow::Result;use solana_client::{nonblocking::rpc_client::RpcClient, rpc_request::TokenAccountsFilter};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 owner = pubkey!("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF"); let token_program = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); let token_accounts = client .get_token_accounts_by_owner(&owner, TokenAccountsFilter::ProgramId(token_program)) .await?; println!("{:#?}", token_accounts); Ok(())}Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| pubkey | string | Yes | Pubkey of the account owner to query, as a base-58 encoded string. |
| filter | object | Yes | Token account filter. Must supply exactly one of mint or programId. See sub-fields below. |
| filter.mint | string | Conditional | Pubkey of the specific token mint to limit accounts to, as a base-58 encoded string. |
| filter.programId | string | Conditional | Pubkey of the Token program that owns the accounts, as a base-58 encoded string. |
| config | object | No | Optional configuration object. See fields below. |
| config.commitment | string | No | Desired finality level. Accepted values: processed, confirmed, finalized (default). |
| config.minContextSlot | number | No | The minimum slot at which the request may be evaluated. |
| config.dataSlice | object | No | Request a slice of account data via offset (usize) and length (usize) byte fields. Only available for non-jsonParsed encodings. |
| config.encoding | string | No | Encoding format for account data. Accepted values: base58, base64, base64+zstd, jsonParsed, binary (deprecated). Default is binary. |
filter examples
Supply mint to return accounts for a specific token mint:
{ "mint": "So11111111111111111111111111111111111111112" }Supply programId to return all accounts owned by a given Token program:
{ "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" }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. More stable than processed, but a weaker guarantee than finalized. |
| finalized | Return data from the highest slot that the cluster recognizes as finalized. 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. |
encoding values
| Encoding | Data format | Notes |
|---|---|---|
| base64 | [data, "base64"] | Recommended. |
| base58 | [data, "base58"] | Slow. The account's data field must be 128 bytes or fewer. |
| base64+zstd | [data, "base64+zstd"] | Zstandard compressed. |
| jsonParsed | {program, parsed, space} | Falls back to [data, "base64"] if no parser is found. |
| binary | string | Deprecated. Legacy base58 encoding returned as a plain string. Use base64 instead. |
minContextSlot example
{ "minContextSlot": 341197000 }dataSlice example
{ "offset": 0, "length": 32 }Response
response.jsonjson
{ "jsonrpc": "2.0", "result": { "context": { "apiVersion": "3.1.8", "slot": 341197933 }, "value": [ { "pubkey": "BGocb4GEpbTFm8UFV2VsDSaBXHELPfAXrvd4vtt8QWrA", "account": { "data": { "program": "spl-token", "parsed": { "info": { "isNative": false, "mint": "2cHr7QS3xfuSV8wdxo3ztuF4xbiarF6Nrgx3qpx3HzXR", "owner": "A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd", "state": "initialized", "tokenAmount": { "amount": "420000000000000", "decimals": 6, "uiAmount": 420000000.0, "uiAmountString": "420000000" } }, "type": "account" }, "space": 165 }, "executable": false, "lamports": 2039280, "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", "rentEpoch": 18446744073709551615, "space": 165 } } ] }, "id": 1}The result is an RpcResponse object containing a context and a value array of keyed-account objects.
Top-level fields
| Field | Type | Description |
|---|---|---|
| context | object | Slot and API version the node used to answer this request. |
| context.slot | u64 | Slot at which the node evaluated this request. |
| context.apiVersion | string | RPC API version reported by the node. May be omitted by older nodes. |
| value | array | Array of keyed-account objects. Each element contains a pubkey and an account data object. |
value[] item fields
| Field | Type | Description |
|---|---|---|
| pubkey | string | Account pubkey, as a base-58 encoded string. |
| account | object | Account data object using the shared Account Data structure. |
| account.data | string | array | object | Token account data. Binary forms depend on the encoding parameter. When encoding is jsonParsed, this field is an object with program, parsed, and space keys. |
| account.executable | bool | Whether the account contains a program and is therefore read-only. |
| account.lamports | u64 | Lamports assigned to the account. |
| account.owner | string | Program owner pubkey, as a base-58 encoded string. |
| account.rentEpoch | u64 | Next epoch at which the account owes rent. |
| account.space | u64 | null | Account data size in bytes. |
account.data fields (jsonParsed)
| Field | Type | Description |
|---|---|---|
| program | string | Name of the parser that produced the decoded data. Either spl-token or spl-token-2022. |
| parsed | object | Parsed SPL Token account payload. |
| space | u64 | Account data size in bytes. |
parsed fields
| Field | Type | Description |
|---|---|---|
| type | string | Always account for this method. |
| info | object | Parsed SPL Token account fields. See parsed.info fields below. |
parsed.info fields
| Field | Type | Description |
|---|---|---|
| mint | string | Token mint pubkey, as a base-58 encoded string. |
| owner | string | Token-account owner pubkey, as a base-58 encoded string. |
| tokenAmount | object | Token amount object with raw and decimal-scaled representations. |
| delegate | string | null | Delegate pubkey, when a delegate is set. |
| state | string | Token-account state. One of uninitialized, initialized, or frozen. |
| isNative | bool | Whether this account wraps the native SOL mint. |
| rentExemptReserve | object | null | Rent-exempt reserve amount for wrapped SOL accounts, when present. Uses the same token-amount schema as tokenAmount. |
| delegatedAmount | object | null | Amount delegated to the delegate, when present. Uses the same token-amount schema as tokenAmount. |
| closeAuthority | string | null | Close-authority pubkey, when present. |
| extensions | array | Parsed SPL Token 2022 extension entries. Each element has an extension discriminator and an extension-specific state payload. |
tokenAmount fields
| Field | Type | Description |
|---|---|---|
| amount | string | Raw token amount, as a base-10 integer string. |
| decimals | u8 | Number of decimal places configured on the mint. |
| uiAmount | number | null | Decimal-scaled amount as a floating-point number. Deprecated in favor of uiAmountString. |
| uiAmountString | string | Decimal-scaled amount as a string. |