getTokenAccountsByOwner – Solana
The getTokenAccountsByOwner JSON-RPC method retrieves all SPL Token accounts associated with a specified token owner.
The getTokenAccountsByOwner RPC Solana method allows developers to query SPL Token accounts by specifying an account Pubkey of the owner.
This method supports optional filtering by mint or programId and offers various encoding formats for flexibility when working with token data in Solana’s Core API.
Supported Networks
This method is available on the following API endpoints:
Mainnet
Devnet
Parameters
Required Parameters
string (required): The Pubkey of the token owner, provided as a base-58 encoded string.
object (required): A JSON object containing one of the following fields:
mint (string): The Pubkey of a specific token Mint to filter accounts.
programId (string): The Pubkey of the Token Program that owns the accounts.
Optional Parameters
object (optional): A configuration object containing:
commitment (string): The level of commitment for the request.
minContextSlot (number): The minimum slot at which the request can be evaluated.
dataSlice (object): Requests a slice of the account’s data.
length (usize): Number of bytes to return.
offset (usize): Byte offset from which to start reading.
encoding (string): The encoding format for the account data.
Supported values: base58, base64, base64+zstd, jsonParsed.
Result
The response is an RpcResponse JSON object containing:
context (object): Provides contextual information about the slot.
apiVersion (string): The version of the API.
slot (u64): The slot number when the data was retrieved.
value (array): An array of JSON objects representing the token accounts.
Each object includes:
pubkey (string): The account Pubkey in base-58 encoding.
account (object):
lamports (u64): Number of lamports in the account.
owner (string): The Pubkey of the program owning the account.
data (object): Encoded account data or parsed JSON representation.
executable (bool): Whether the account contains a program.
rentEpoch (u64): The epoch at which the account will next owe rent.
space (u64): The data size of the account.
Request Example
API Endpoints
cURL Example
Response
A successful request returns an array of SPL Token accounts associated with the specified owner.
Example Response
Error Handling
Common getTokenAccountsByOwner error scenarios:
Invalid Pubkey: If the provided Pubkey is incorrectly formatted.
Network errors: Connectivity issues with the Solana JSON-RPC API endpoints.
Invalid encoding: If an unsupported encoding type is specified.
Example Error Response
Use Cases
The Solana getTokenAccountsByOwner method is useful for:
Wallet applications: Displaying token balances for user accounts.
Web3 analytics tools: Tracking token balances across multiple accounts.
DeFi applications: Analyzing token holdings and transfers.
Blockchain explorers: Displaying token account information in real-time.
Code getTokenAccountsByOwner Example – Web3 Integration
Integration with Web3
By integrating Web3 getTokenAccountsByOwner into Solana’s Core API, developers can efficiently track token accounts, monitor transaction activity, and manage account state. This JSON-RPC method is essential for applications that handle SPL Token balances and ownership records in Web3 environments.
Last updated