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

  1. pubkeys (array of string, 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"]
  2. 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), the data 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 for base58, base64, or base64+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 or object): The account data. If encoding is jsonParsed 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.

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.

# Replace <api-key> with your Helius API key
# SOL Llama Mint: Abug4qgG1x23AEdjS2h9CEJ1m6ha2Z22LdK2kL2pys3F
# Serum Dex Program v3: 9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin
curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
  '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getMultipleAccounts",
    "params": [
      [
        "Abug4qgG1x23AEdjS2h9CEJ1m6ha2Z22LdK2kL2pys3F",
        "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin"
      ]
    ]
  }'

2. Fetch Parsed Token Account Data

This example fetches data for two SPL Token accounts and requests jsonParsed encoding to get structured data.

# Replace <api-key> with your Helius API key
# Example USDC Token Account 1: GqoZ2MCrdTtygoX1F2b8X7F2tDXxNxyvMvykR9RzQW8p
# Example USDT Token Account 2: HYnLMbkaPMh9W2aPNy2yP4LzLSWWw9zSCYEZdX2g2E7m
curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
  '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getMultipleAccounts",
    "params": [
      [
        "GqoZ2MCrdTtygoX1F2b8X7F2tDXxNxyvMvykR9RzQW8p",
        "HYnLMbkaPMh9W2aPNy2yP4LzLSWWw9zSCYEZdX2g2E7m"
      ],
      {
        "encoding": "jsonParsed"
      }
    ]
  }'

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 for null.
  • jsonParsed Convenience: Using jsonParsed 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), use dataSlice to fetch only the necessary bytes to avoid excessive data transfer.
  • Error Handling: Be prepared to handle null entries in the response value 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.