# getProgramAccountsV2

> For the complete documentation index, see [llms.txt](/docs/llms.txt).

POST https://solana-mainnet.g.alchemy.com/v2/{apiKey}

Paginated variant of `getProgramAccounts`. Reach for this when a program owns enough accounts that the single-response version times out or exhausts memory: indexing every token account for a mint, backfilling a DeFi protocol's state, or feeding analytics dashboards from raw on-chain data.

Requests accept the same `commitment`, `encoding`, `dataSlice`, and `filters` fields as `getProgramAccounts`, with three additions on the configuration object:

* `limit` bounds a single page to between 1 and 10,000 accounts (default 1,000).
* `paginationKey` is the cursor returned by the previous page; omit it on the first request.
* `changedSinceSlot` restricts the page to accounts written at or after the given slot, so you can keep a downstream index in sync without re-scanning the whole program.

Keep paging while `paginationKey` is a string. It only turns `null` once the server has no more accounts to hand out; a page shorter than `limit` on its own does not mean the walk is finished, because filters can drop accounts server-side before the page ships.

Set `withContext: true` on the configuration object to receive the wrapped Solana RPC shape: a `context` object (with at minimum the `slot` the node evaluated the request at, plus `apiVersion` when the node reports one) beside a `value` payload holding `accounts` and `paginationKey`. Leave `withContext` unset or `false` and those two fields are returned directly on `result` with no `context` sibling. Every other aspect of the response is identical between the two shapes.


Reference: https://www.alchemy.com/docs/chains/solana/solana-api-endpoints/get-program-accounts-v-2

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| Pubkey | string | Yes | The Pubkey of the program. |
| Configuration | object | No | Optional configuration object. Accepts every field `getProgramAccounts` accepts, with the pagination and incremental-update fields (`limit`, `paginationKey`, `changedSinceSlot`) layered on top. |

## Result

**Program accounts page** (object): A page of accounts (up to `limit`) alongside the cursor for the next request. When the request sets `withContext: true`, the same page is placed inside a `{context, value}` envelope instead of being returned directly on `result`.

## Example

### Request

```json
{
  "jsonrpc": "2.0",
  "method": "getProgramAccountsV2",
  "params": [
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
    {
      "encoding": "base64",
      "limit": 1000,
      "filters": [
        {
          "dataSize": 165
        }
      ]
    }
  ],
  "id": 1
}
```

## Code Examples

### cURL

```bash
curl --request POST \
  --url https://solana-mainnet.g.alchemy.com/v2/docs-demo \
  --header 'Content-Type: application/json' \
  --data '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getProgramAccountsV2",
  "params": [
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
    {
      "encoding": "base64",
      "limit": 1000,
      "filters": [
        {
          "dataSize": 165
        }
      ]
    }
  ]
}'
```

### JavaScript

```javascript
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: 1,
    method: 'getProgramAccountsV2',
    params: [
      'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
      {encoding: 'base64', limit: 1000, filters: [{dataSize: 165}]}
    ]
  })
};

fetch('https://solana-mainnet.g.alchemy.com/v2/docs-demo', options)
  .then(res => res.json())
  .then(res => console.log(res))
  .catch(err => console.error(err));
```

### Python

```python
import requests

url = "https://solana-mainnet.g.alchemy.com/v2/docs-demo"

payload = {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getProgramAccountsV2",
    "params": [
        "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
        {
            "encoding": "base64",
            "limit": 1000,
            "filters": [{ "dataSize": 165 }]
        }
    ]
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
```

### Go

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://solana-mainnet.g.alchemy.com/v2/docs-demo"

	payload := strings.NewReader("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"getProgramAccountsV2\",\n  \"params\": [\n    \"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\",\n    {\n      \"encoding\": \"base64\",\n      \"limit\": 1000,\n      \"filters\": [\n        {\n          \"dataSize\": 165\n        }\n      ]\n    }\n  ]\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(string(body))

}
```

### Java

```java
HttpResponse<String> response = Unirest.post("https://solana-mainnet.g.alchemy.com/v2/docs-demo")
  .header("Content-Type", "application/json")
  .body("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"getProgramAccountsV2\",\n  \"params\": [\n    \"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\",\n    {\n      \"encoding\": \"base64\",\n      \"limit\": 1000,\n      \"filters\": [\n        {\n          \"dataSize\": 165\n        }\n      ]\n    }\n  ]\n}")
  .asString();
```

### C#

```csharp
using RestSharp;


var options = new RestClientOptions("https://solana-mainnet.g.alchemy.com/v2/docs-demo");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddJsonBody("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"getProgramAccountsV2\",\n  \"params\": [\n    \"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\",\n    {\n      \"encoding\": \"base64\",\n      \"limit\": 1000,\n      \"filters\": [\n        {\n          \"dataSize\": 165\n        }\n      ]\n    }\n  ]\n}", false);
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);

```


## OpenRPC Method Specification

```yaml
name: getProgramAccountsV2
description: |
  Paginated variant of `getProgramAccounts`. Reach for this when a program owns enough accounts that the single-response version times out or exhausts memory: indexing every token account for a mint, backfilling a DeFi protocol's state, or feeding analytics dashboards from raw on-chain data.

  Requests accept the same `commitment`, `encoding`, `dataSlice`, and `filters` fields as `getProgramAccounts`, with three additions on the configuration object:

  * `limit` bounds a single page to between 1 and 10,000 accounts (default 1,000).
  * `paginationKey` is the cursor returned by the previous page; omit it on the first request.
  * `changedSinceSlot` restricts the page to accounts written at or after the given slot, so you can keep a downstream index in sync without re-scanning the whole program.

  Keep paging while `paginationKey` is a string. It only turns `null` once the server has no more accounts to hand out; a page shorter than `limit` on its own does not mean the walk is finished, because filters can drop accounts server-side before the page ships.

  Set `withContext: true` on the configuration object to receive the wrapped Solana RPC shape: a `context` object (with at minimum the `slot` the node evaluated the request at, plus `apiVersion` when the node reports one) beside a `value` payload holding `accounts` and `paginationKey`. Leave `withContext` unset or `false` and those two fields are returned directly on `result` with no `context` sibling. Every other aspect of the response is identical between the two shapes.
x-compute-units: 20
params:
  - name: Pubkey
    required: true
    description: The Pubkey of the program.
    schema:
      title: Pubkey
      type: string
      description: Base-58 encoded public key.
  - name: Configuration
    required: false
    description: Optional configuration object. Accepts every field `getProgramAccounts` accepts, with the pagination and incremental-update fields (`limit`, `paginationKey`, `changedSinceSlot`) layered on top.
    schema:
      title: GetProgramAccountsV2 Configuration
      type: object
      description: Options for a single `getProgramAccountsV2` request. Every field that `GetProgramAccountsConfig` accepts is honored here; the pagination and incremental-update fields (`limit`, `paginationKey`, `changedSinceSlot`) sit alongside them.
      properties:
        commitment:
          title: Commitment Level
          type: string
          description: Configures the state commitment for querying.
          enum:
            - processed
            - confirmed
            - finalized
        minContextSlot:
          title: Minimum Context Slot
          type: integer
          description: The minimum slot that the request can be evaluated at.
        withContext:
          type: boolean
          description: 'Set to `true` to receive the wrapped Solana RPC shape: a `context` object (at minimum a `slot` field, plus `apiVersion` when the node reports one) beside a `value` object holding `accounts` and `paginationKey`. Leave unset or set to `false` and those two fields are placed at the top of `result` with no `context` sibling. Filtering, page size, and cursor behavior are identical either way — only the outer JSON shape changes.'
        encoding:
          default: json
          title: Data Encoding
          type: string
          description: Encoding format for data.
          enum:
            - base58
            - base64
            - base64+zstd
            - jsonParsed
        dataSlice:
          title: Data Slice
          type: object
          properties:
            length:
              type: integer
              description: Number of bytes to return.
            offset:
              type: integer
              description: Byte offset from which to start reading.
        filters:
          type: array
          description: Filters to apply using up to 4 filter objects.
        limit:
          type: integer
          minimum: 1
          maximum: 10000
          default: 1000
          description: Upper bound on the number of accounts returned per page (1-10,000). Server-side filtering can shrink a page below `limit` without meaning the walk is finished — keep paging until `paginationKey` comes back `null`.
        paginationKey:
          type: string
          description: Cursor for the next page, base-58 encoded. Take the value from the previous response's `paginationKey`; leave this field out on the first call.
        changedSinceSlot:
          type: integer
          minimum: 0
          description: Restrict the page to accounts written at or after this slot. Useful for keeping a downstream index or cache aligned with recent on-chain state without re-scanning the whole program.
examples:
  - name: getProgramAccountsV2 example
    params:
      - name: Pubkey
        value: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
      - name: Configuration
        value:
          encoding: base64
          limit: 1000
          filters:
            - dataSize: 165
result:
  name: Program accounts page
  description: 'A page of accounts (up to `limit`) alongside the cursor for the next request. When the request sets `withContext: true`, the same page is placed inside a `{context, value}` envelope instead of being returned directly on `result`.'
  schema:
    title: Program Accounts V2 Result
    description: 'Shape of the `result` object. With `withContext` unset or `false`, the page''s `accounts` and `paginationKey` are returned at the top level. With `withContext: true`, the same page is placed under `value`, and a `context` sibling carries the slot the node used to build the response.'
    oneOf:
      - title: without withContext
        type: object
        description: A single page of program accounts plus the cursor for the next one. These fields sit directly on `result`, or under `result.value`, depending on the request's `withContext` flag.
        properties:
          accounts:
            type: array
            description: Accounts owned by the program that fell into this page.
            items:
              title: Program Account
              type: object
              properties:
                pubkey:
                  title: Pubkey
                  type: string
                  description: The account Pubkey as a base-58 encoded string.
                account:
                  title: Account Information
                  type: object
                  properties:
                    lamports:
                      type: integer
                      description: Number of lamports assigned to this account.
                    owner:
                      title: Pubkey
                      type: string
                      description: Program owner of this account.
                    data:
                      title: Account Data
                      type: array
                      description: Account data in the specified encoding format.
                      items:
                        type: string
                    executable:
                      type: boolean
                      description: Indicates if the account contains a program.
                    rentEpoch:
                      type: integer
                      description: The epoch at which this account will next owe rent.
                    size:
                      type: integer
                      description: The data size of the account.
          paginationKey:
            type: string
            nullable: true
            description: Cursor to feed into the next request, base-58 encoded. Turns to `null` once the server confirms there are no further accounts to hand out. A page shorter than the requested `limit` does not by itself mean the walk is done — keep calling until this field is `null`.
      - type: object
        title: with withContext
        description: 'Envelope returned when the request has `withContext: true`.'
        required:
          - context
          - value
        properties:
          context:
            type: object
            description: Node metadata for the moment this response was built.
            properties:
              slot:
                type: integer
                description: Slot at which the node built this response.
              apiVersion:
                type: string
                description: RPC API version when available.
          value:
            title: Program Accounts V2 Page
            type: object
            description: A single page of program accounts plus the cursor for the next one. These fields sit directly on `result`, or under `result.value`, depending on the request's `withContext` flag.
            properties:
              accounts:
                type: array
                description: Accounts owned by the program that fell into this page.
                items:
                  title: Program Account
                  type: object
                  properties:
                    pubkey:
                      title: Pubkey
                      type: string
                      description: The account Pubkey as a base-58 encoded string.
                    account:
                      title: Account Information
                      type: object
                      properties:
                        lamports:
                          type: integer
                          description: Number of lamports assigned to this account.
                        owner:
                          title: Pubkey
                          type: string
                          description: Program owner of this account.
                        data:
                          title: Account Data
                          type: array
                          description: Account data in the specified encoding format.
                          items:
                            type: string
                        executable:
                          type: boolean
                          description: Indicates if the account contains a program.
                        rentEpoch:
                          type: integer
                          description: The epoch at which this account will next owe rent.
                        size:
                          type: integer
                          description: The data size of the account.
              paginationKey:
                type: string
                nullable: true
                description: Cursor to feed into the next request, base-58 encoded. Turns to `null` once the server confirms there are no further accounts to hand out. A page shorter than the requested `limit` does not by itself mean the walk is done — keep calling until this field is `null`.
```
