How to Use getMultipleAccounts
Learn getMultipleAccounts use cases, code examples, request parameters, response structure, and tips.
The getMultipleAccounts
RPC method is a highly efficient way to fetch information for a list of Solana accounts simultaneously. Instead of making individual getAccountInfo
requests for each account, getMultipleAccounts
allows you to batch these requests, reducing network overhead and improving the responsiveness of your application.
Common Use Cases
- Batch Loading Account Data: When your application needs to display or process data from multiple known accounts (e.g., a user’s token accounts, a list of on-chain program configurations).
- Portfolio Trackers: Fetching balances and states for numerous token accounts owned by a user.
- Marketplace UIs: Displaying details of multiple NFTs or listed items by fetching their account data in one go.
- Improving dApp Performance: Significantly reducing the number of RPC calls, leading to faster load times and a better user experience, especially when dealing with many accounts.
Request Parameters
-
pubkeys
(array
ofstring
, required):- An array of base-58 encoded public key strings for the accounts you want to query.
- Maximum of 100 public keys per request.
- Example:
["So11111111111111111111111111111111111111112", "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"]
-
options
(object
, optional): A configuration object containing one or more of the following fields:commitment
(string
): Specifies the commitment level for the query (e.g.,"finalized"
,"confirmed"
,"processed"
).encoding
(string
): The encoding for the account data. Options include:"base64"
(default): Standard base64 encoding."base58"
: Slower, but may be useful in some contexts."base64+zstd"
: Base64 encoded zstd compressed data."jsonParsed"
: If the account is owned by a program for which the RPC node has a parser (e.g., SPL Token Program, Stake Program), thedata
field will be a JSON object. This is very useful for structured data.
dataSlice
(object
): Allows you to fetch only a specific portion of the account data. This is useful for large accounts where you only need a small piece of information.offset
(usize
): The offset in bytes from the start of the account data.length
(usize
): The number of bytes to return from the offset.- Note:
dataSlice
is only available forbase58
,base64
, orbase64+zstd
encodings.
minContextSlot
(u64
): The minimum slot that the request can be evaluated at.
Response Structure
The JSON-RPC response object will have a result
field containing:
context
(object
):slot
(u64
): The slot at which the information was retrieved.apiVersion
(string
, optional): The API version of the node.
value
(array
):- An array where each element corresponds to the public key at the same index in the request’s
pubkeys
array. - Each element will either be:
null
: If the account at the specified public key does not exist or an error occurred for that specific account.- An Account Object with the following fields:
lamports
(u64
): The number of lamports owned by the account.owner
(string
): The base-58 encoded public key of the program that owns the account.data
(array
orobject
): The account data. Ifencoding
isjsonParsed
and a parser exists, this will be a JSON object. Otherwise, it’s typically an array["encoded_string", "encoding_format"]
(e.g.,["SGVsbG8=", "base64"]
).executable
(boolean
): Whether the account contains a program (is executable).rentEpoch
(u64
): The next epoch at which this account will owe rent.space
(u64
): The data length of the account in bytes.
- An array where each element corresponds to the public key at the same index in the request’s
Examples
1. Fetch Basic Info for Two Accounts
This example fetches data for two accounts: the SOL Llama (an NFT) and the Serum Dex Program v3.
2. Fetch Parsed Token Account Data
This example fetches data for two SPL Token accounts and requests jsonParsed
encoding to get structured data.
Developer Tips
- Maximum 100 Accounts: You can request a maximum of 100 accounts per call.
- Atomicity: The request is not atomic in the sense that if one account lookup fails, others might still succeed. Check each element in the
value
array fornull
. jsonParsed
Convenience: UsingjsonParsed
encoding is highly recommended when dealing with common account types like SPL Token accounts, as it saves you from manual deserialization.dataSlice
for Large Accounts: For very large accounts (e.g., some program state accounts), usedataSlice
to fetch only the necessary bytes to avoid excessive data transfer.- Error Handling: Be prepared to handle
null
entries in the responsevalue
array, indicating that an account was not found or could not be fetched.
By leveraging getMultipleAccounts
, you can build more performant and scalable Solana applications.