How to Use getTokenAccountsByOwner
Learn getTokenAccountsByOwner use cases, code examples, request parameters, response structure, and tips.
The getTokenAccountsByOwner
RPC method is used to retrieve all SPL Token accounts owned by a specific public key. This is a fundamental method for wallets and applications that need to display a user’s token holdings or interact with their various token accounts.
You must filter the query by either a specific token mint
or a programId
(e.g., the SPL Token Program or Token-2022 Program).
Common Use Cases
- Displaying User Portfolio: Fetching all token accounts (and thus balances) for a given user’s wallet address to show their complete token portfolio.
- Application Logic: Identifying a user’s specific token account for a particular mint before initiating a transfer or other interaction.
- Verification: Checking which token accounts an owner possesses for a certain type of token.
- Indexing Token Holders: While less efficient for global indexing than other methods, it can be used to find accounts for a known set of owners.
Request Parameters
-
ownerPubkey
(string, required): The base-58 encoded public key of the account owner whose token accounts you want to retrieve. -
filter
(object, required): A JSON object that must specify eithermint
orprogramId
:mint
(string): The base-58 encoded public key of a specific token mint. If provided, only token accounts for this mint owned byownerPubkey
will be returned.programId
(string): The base-58 encoded public key of the Token Program that governs the accounts. Common values are:- SPL Token Program:
TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
- Token-2022 Program:
TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxAPds
- SPL Token Program:
-
options
(object, optional): An optional configuration object that can include:commitment
(string, optional): Specifies the commitment level.encoding
(string, optional): The encoding for account data."jsonParsed"
is highly recommended. Other options:"base64"
,"base64+zstd"
. Defaults to"base64"
.dataSlice
(object, optional): To retrieve a specific slice of the account data (offset
: usize,length
: usize). Only forbase58
,base64
, orbase64+zstd
encodings.minContextSlot
(u64, optional): The minimum slot for the query.
Response Structure
The result.value
field in the JSON-RPC response is an array of objects. Each object corresponds to an SPL Token account owned by ownerPubkey
and matching the filter
.
Each object in the value
array contains:
pubkey
(string): The base-58 encoded public key of the token account itself.account
(object): Detailed information about the token account:lamports
(u64): Lamport balance for rent exemption.owner
(string): The owning program (e.g., the Token Program public key).data
: Account data. If"jsonParsed"
encoding is used, this contains:program
(string): e.g.,"spl-token"
.parsed
: An object with structured information:info
: Details such as:mint
(string): The mint address of the token.owner
(string): The owner of the token account (this should matchownerPubkey
from the request).tokenAmount
(object): The balance of tokens (amount
,decimals
,uiAmount
,uiAmountString
).state
(string): State of the token account (e.g.,"initialized"
).isNative
(boolean): If the account holds wrapped SOL.delegate
(string, optional): The delegate address if one is set.delegatedAmount
(object, optional): The delegated amount if a delegate is set.
type
(string): e.g.,"account"
.
executable
(boolean): Whether the account is executable.rentEpoch
(u64): Next epoch rent is due.space
(u64, if notjsonParsed
): Length of raw account data in bytes.
Example Response (with jsonParsed
encoding, filtered by programId
):
Code Examples
Developer Tips
- Filter Requirement: You must supply either a
mint
or aprogramId
in the filter. It’s not possible to query all token accounts for an owner across all token types without one of these primary filters. - Associated Token Accounts: This method will return all token accounts owned by the public key, including standard Associated Token Accounts (ATAs) and any other SPL token accounts they might own (e.g., from older wallet implementations or custom setups).
- Encoding: Using
"jsonParsed"
for theencoding
option is highly recommended. It decodes the binary account data into a more usable JSON structure. - Performance: If an owner has a very large number of token accounts (especially when filtering only by
programId
), the response can be large. Consider pagination if your RPC provider or library supports it, or if you anticipate very large result sets, though this method itself doesn’t directly support pagination in its standard parameters. - Token-2022 (Token Extensions): If you are working with tokens created using the Token-2022 program (which supports extensions like transfer fees, interest, etc.), ensure you use the correct
programId
:TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxAPds
.
This guide provides a thorough understanding of the getTokenAccountsByOwner
RPC method, enabling you to efficiently retrieve token account information for any Solana address.