getProgramAccounts - Solana
Example code for the getProgramAccounts JSON-RPC method. Complete guide on how to use the getProgramAccounts JSON-RPC method in the GetBlock Web3 documentation.
This method returns all accounts owned by a program, with optional filters on data size and byte content. This is the primary method for indexing program state such as SPL Token accounts and Anchor PDAs.
Parameters
programId
string
Yes
Base58-encoded Pubkey of the owning program
config
object
No
Configuration object controlling filters, encoding, and commitment
Config Object
encoding
string
No
Data encoding: base58, base64, base64+zstd, or jsonParsed
commitment
string
No
Commitment level: processed, confirmed, or finalized. Defaults to finalized
dataSlice
object
No
Byte range to return, with offset and length fields
filters
array
No
Up to 4 filter objects, each a dataSize or memcmp filter
withContext
boolean
No
Wrap the result in an RpcResponse context object
minContextSlot
number
No
Minimum slot the request can be evaluated at
Memcmp Filter
offset
number
Yes
Byte offset into the account data to compare from
bytes
string
Yes
Data to match, base58 or base64 encoded
encoding
string
No
Encoding of the bytes field: base58 or base64
Request
Response
Response Parameters
jsonrpc
string
JSON-RPC protocol version ("2.0")
id
string
Request identifier matching the request
result
array
Array of account objects owned by the program, each with pubkey and account fields
Result Entry
pubkey
string
Base58 Pubkey of the account
account
object
Account object with lamports, owner, data, executable, rentEpoch, and space
Use Cases
Token Holder Indexes: Enumerate every SPL Token account for a given mint
Anchor Discriminators: Filter PDAs by the 8-byte account discriminator at offset 0
Order Book Crawls: List open orders accounts owned by a DEX program
Stake Analysis: Query stake accounts delegated to a specific vote account
Protocol Dashboards: Rebuild protocol state without an off-chain indexer
Error Handling
-32602
Invalid params
More than 4 filters, malformed memcmp bytes, or unsupported encoding
-32603
Internal error
Scan exceeded the node's account scan limits
-32016
Minimum context slot has not been reached
The node has not yet processed minContextSlot
-32005
Node is unhealthy
The node has fallen behind the cluster tip
Last updated
Was this helpful?