> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thornode.io/llms.txt
> Use this file to discover all available pages before exploring further.

# getTokenAccountsByOwnerV2

> Return the token accounts owned by a wallet in pages, filtered by mint or token program.

`getTokenAccountsByOwnerV2` returns the SPL Token and Token-2022 accounts owned by a wallet, one page at a time, ordered by pubkey. Use it instead of `getTokenAccountsByOwner` when a wallet can hold more than 10,000 token accounts or you want bounded page sizes.

Send it to the complete ThorEdge RPC URL like any other method.

## Parameters

<ParamField body="owner" type="string" required>
  Wallet pubkey as a base-58 string.
</ParamField>

<ParamField body="filter" type="object" required>
  Exactly one of:

  * `{ "mint": "<pubkey>" }`: token accounts for one mint.
  * `{ "programId": "<pubkey>" }`: all token accounts owned by that token program. Must be the SPL Token or Token-2022 program id.

  An unknown mint returns an empty page, not an error.
</ParamField>

<ParamField body="config" type="object">
  Options for the query. All fields are optional.

  <Expandable title="properties">
    <ParamField body="encoding" type="string" default="base64">
      `base64`, `base58`, `base64+zstd`, or `jsonParsed`.
    </ParamField>

    <ParamField body="dataSlice" type="object">
      `{ "offset": <int>, "length": <int> }`. Returns only that slice of `data`. Ignored with `jsonParsed`.
    </ParamField>

    <ParamField body="limit" type="integer" default="1000">
      Accounts per page, from `1` through `10000`. Values outside the range are clamped, not rejected.
    </ParamField>

    <ParamField body="paginationKey" type="string">
      The `paginationKey` returned by the previous page. Omit it for the first page.
    </ParamField>

    <ParamField body="commitment" type="string">
      Accepted and ignored. Results reflect FastGate's indexed slot.
    </ParamField>
  </Expandable>
</ParamField>

`filters` and `changedSinceSlot` are not supported on this method.

## Response

<ResponseField name="context" type="object">
  `{ "slot": <int> }`: the indexed slot the page reflects.
</ResponseField>

<ResponseField name="value" type="object">
  <Expandable title="properties">
    <ResponseField name="accounts" type="array">
      Matching token accounts for this page. Each item is `{ "pubkey", "account" }`; `account` holds `lamports`, `data`, `owner`, `executable`, `rentEpoch`, `space`.
    </ResponseField>

    <ResponseField name="count" type="integer">
      Number of accounts in this page, not the total.
    </ResponseField>

    <ResponseField name="paginationKey" type="string | null">
      Cursor for the next page. `null` when this page was shorter than `limit`. A page that exactly fills `limit` still returns a key; the following request then returns an empty `accounts` array and `null`.
    </ResponseField>
  </Expandable>
</ResponseField>

## Paginate

Repeat the request with `paginationKey` set to the value you received, keeping the filter, `encoding`, and `limit` unchanged, until `paginationKey` is `null`.

## Errors

| Code     | Message                                                                                 | Cause                                                                  |
| -------- | --------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `-32602` | `Missing param at index 1`                                                              | The filter object is missing.                                          |
| `-32602` | `Invalid param at index 1: invalid value: map, expected map with a single key`          | The filter has zero, two, or an unknown key.                           |
| `-32602` | `Error getting token program id and mint: Invalid param: unrecognized Token program id` | `programId` is not a token program.                                    |
| `-32602` | `Invalid param: could not decode pubkey <value>` / `Invalid param: WrongSize`           | A pubkey or `paginationKey` is not a valid 32-byte base-58 key.        |
| `-32602` | `Invalid param: limit must be a number`                                                 | `limit` is not an integer.                                             |
| `-32003` | `Query exceeded the read budget; narrow the filters or use pagination`                  | The query ran past its time budget. Lower `limit` or filter by `mint`. |

<RequestExample>
  ```bash cURL theme={null}
  curl --fail-with-body "$THORNODE_RPC_URL" \
    --header 'content-type: application/json' \
    --data '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getTokenAccountsByOwnerV2",
      "params": [
        "BghNaiR8KfW3Qt2hTaS6hqperaXvfumWhpP4EEMksXxX",
        { "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" },
        { "encoding": "jsonParsed", "limit": 2 }
      ]
    }'
  ```

  ```javascript Node.js theme={null}
  const response = await fetch(process.env.THORNODE_RPC_URL, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({
      jsonrpc: "2.0",
      id: 1,
      method: "getTokenAccountsByOwnerV2",
      params: [
        "BghNaiR8KfW3Qt2hTaS6hqperaXvfumWhpP4EEMksXxX",
        { programId: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" },
        { encoding: "jsonParsed", limit: 2 },
      ],
    }),
  });
  const { result } = await response.json();
  console.log(result.value.count, result.value.paginationKey);
  ```
</RequestExample>

<ResponseExample>
  ```json Response theme={null}
  {
    "jsonrpc": "2.0",
    "result": {
      "context": { "slot": 439745294 },
      "value": {
        "accounts": [
          {
            "pubkey": "BCVfzZWUYiFQGQ4rCh9ycUoXZcu8aTLW5aiqsjhj7YAf",
            "account": {
              "lamports": 2039280,
              "data": {
                "program": "spl-token",
                "parsed": {
                  "info": {
                    "isNative": false,
                    "mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
                    "owner": "BghNaiR8KfW3Qt2hTaS6hqperaXvfumWhpP4EEMksXxX",
                    "state": "initialized",
                    "tokenAmount": {
                      "amount": "4192",
                      "decimals": 6,
                      "uiAmount": 0.004192,
                      "uiAmountString": "0.004192"
                    }
                  },
                  "type": "account"
                },
                "space": 165
              },
              "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
              "executable": false,
              "rentEpoch": 18446744073709551615,
              "space": 165
            }
          }
        ],
        "count": 1,
        "paginationKey": null
      }
    },
    "id": 1
  }
  ```
</ResponseExample>
