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

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# getProgramAccounts

> Returns accounts owned by the specified program, optionally filtered by data content or size.

<Info>
  **Source**

  [`get_program_accounts`](https://github.com/anza-xyz/agave/blob/v4.3.2/rpc/src/rpc.rs#L606)
</Info>

<RequestExample>
  ```bash cURL
  curl https://api.devnet.solana.com -s -X POST \
    -H "Content-Type: application/json" \
    -d '
    {
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getProgramAccounts",
      "params": [
        "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
        {
          "commitment": "finalized",
          "encoding": "base64",
          "filters": [
            { "dataSize": 165 },
            {
              "memcmp": {
                "offset": 0,
                "bytes": "Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr"
              }
            },
            {
              "memcmp": {
                "offset": 32,
                "bytes": "5wx11hXBHQALycTQNkeQ5w1N9vgup4ardN2yLiDK4JyK"
              }
            }
          ],
          "sortResults": true
        }
      ]
    }
  '
  ```

  ```ts Kit
  import {
    address,
    createSolanaRpc,
    type Base58EncodedBytes
  } from "@solana/kit";

  const rpc_url = "https://api.devnet.solana.com";
  const rpc = createSolanaRpc(rpc_url);

  let program = address("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");

  let accounts = await rpc
    .getProgramAccounts(
      program,
      {
        commitment: "finalized",
        encoding: "base64",
        filters: [
          {
            dataSize: BigInt(165)
          },
          {
            memcmp: {
              bytes: "Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr" as Base58EncodedBytes,
              encoding: "base58",
              offset: BigInt(0)
            }
          },
          {
            memcmp: {
              bytes: "5wx11hXBHQALycTQNkeQ5w1N9vgup4ardN2yLiDK4JyK" as Base58EncodedBytes,
              encoding: "base58",
              offset: BigInt(32)
            }
          }
        ]
      }
    )
    .send();

  console.log(accounts);
  ```

  ```ts web3.js
  import {
    Connection,
    PublicKey,
    clusterApiUrl,
    type GetProgramAccountsConfig
  } from "@solana/web3.js";

  const connection = new Connection(clusterApiUrl("devnet"), "confirmed");

  let programId = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");

  let config: GetProgramAccountsConfig = {
    commitment: "finalized",
    encoding: "base64",
    filters: [
      {
        dataSize: 165
      },
      {
        memcmp: {
          bytes: "Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr",
          offset: 0
        }
      },
      {
        memcmp: {
          bytes: "5wx11hXBHQALycTQNkeQ5w1N9vgup4ardN2yLiDK4JyK",
          offset: 32
        }
      }
    ]
  };

  let accounts = await connection.getProgramAccounts(programId, config);

  console.log(accounts);
  ```

  ```rs Rust
  use anyhow::Result;
  use solana_account_decoder::UiAccountEncoding;
  use solana_client::{
      nonblocking::rpc_client::RpcClient,
      rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig},
      rpc_filter::{Memcmp, MemcmpEncodedBytes, RpcFilterType},
  };
  use solana_commitment_config::CommitmentConfig;
  use solana_sdk::pubkey;

  #[tokio::main]
  async fn main() -> Result<()> {
      let client = RpcClient::new_with_commitment(
          String::from("https://api.devnet.solana.com"),
          CommitmentConfig::confirmed(),
      );

      let program = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");

      let config = RpcProgramAccountsConfig {
          filters: vec![
              RpcFilterType::DataSize(165),
              RpcFilterType::Memcmp(Memcmp::new(
                  0,
                  MemcmpEncodedBytes::Base58("Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr".to_string()),
              )),
              RpcFilterType::Memcmp(Memcmp::new(
                  32,
                  MemcmpEncodedBytes::Base58("5wx11hXBHQALycTQNkeQ5w1N9vgup4ardN2yLiDK4JyK".to_string()),
              )),
          ]
          .into(),
          account_config: RpcAccountInfoConfig {
              encoding: UiAccountEncoding::Base64.into(),
              commitment: CommitmentConfig::finalized().into(),
              ..Default::default()
          },
          sort_results: true.into(),
          ..Default::default()
      };

      let accounts = client
          .get_program_ui_accounts_with_config(&program, config)
          .await?;

      println!("{:#?}", accounts);

      Ok(())
  }
  ```
</RequestExample>

<ResponseExample>
  ```json Response
  {
    "jsonrpc": "2.0",
    "result": [
      {
        "pubkey": "CxELquR1gPP8wHe33gZ4QxqGB3sZ9RSwsJ2KshVewkFY",
        "account": {
          "data": [
            "KgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
            "base64"
          ],
          "executable": false,
          "lamports": 15298080,
          "owner": "4Nd1mBQtrMJVYVfKf2PJy9NZUZdTAsp7D4xWLs4gDB4T",
          "rentEpoch": 28,
          "space": 42
        }
      }
    ],
    "id": 1
  }
  ```
</ResponseExample>

## Parameters

<ParamField body="pubkey" type="string" required>
  Pubkey of program, as base-58 encoded string
</ParamField>

<ParamField body="config" type="object">
  Configuration object containing the following fields:

  <Expandable title="properties" defaultOpen>
    <ParamField body="commitment" type="string" default="finalized">
      Values: `processed`, `confirmed`, `finalized`

      Solana RPC uses the following commitment levels:

      | Value       | Description                                                                                                                                                                               |
      | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
      | `processed` | Return data from the highest slot this node has processed. This is the newest view, but the block can still be skipped.                                                                   |
      | `confirmed` | Return data from the same slot as `finalized`. Kept for compatibility and planned for removal in a later release.                                                                         |
      | `finalized` | Return data from the highest slot with a finalization certificate, from votes by 80% of stake in one round or 60% of stake in each of two rounds. This is the strongest commitment level. |
    </ParamField>

    <ParamField body="minContextSlot" type="number">
      The minimum slot that the request can be evaluated at

      ```json Example
      { "minContextSlot": 341197000 }
      ```
    </ParamField>

    <ParamField body="withContext" type="bool" default="false">
      Values: `true`, `false`

      Wrap the result in an RpcResponse JSON object

      ```json Example
      { "withContext": true }
      ```
    </ParamField>

    <ParamField body="encoding" type="string" default="binary">
      Values: `base58`, `base64`, `base64+zstd`, `binary`, `jsonParsed`

      Encoding format for account data.

      | Encoding      | Data format                | Notes                                                                                                                                                                                                                            |
      | ------------- | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
      | `base64`      | `[data, "base64"]`         | Recommended                                                                                                                                                                                                                      |
      | `base58`      | `[data, "base58"]`         | Slow. The account's data field must be 128 bytes or fewer                                                                                                                                                                        |
      | `base64+zstd` | `[data, "base64+zstd"]`    | [Zstandard](https://github.com/facebook/zstd) compressed                                                                                                                                                                        |
      | `jsonParsed`  | `{program, parsed, space}` | Falls back to `[data, "base64"]` if no parser found                                                                                                                                                                              |
      | `binary`      | `string`                   | Deprecated default. Legacy encoding retained for backwards compatibility. Same base58 encoding as `base58` with the same 128-byte data field limit, but returns data as a plain string instead of an array. Use `base64` instead |

      If you use `jsonParsed`, each returned account uses the stable wrapper
      `{program, parsed, space}`. The `program` field identifies the parser that
      produced `parsed`. If no parser is available, the RPC node falls back to
      `[data, "base64"]`.

      | `program` value          | Parsed account type                            |
      | ------------------------ | ---------------------------------------------- |
      | `address-lookup-table`   | Address lookup table accounts                  |
      | `bpf-upgradeable-loader` | Upgradeable loader program and buffer accounts |
      | `config`                 | Config accounts                                |
      | `nonce`                  | Durable nonce accounts                         |
      | `spl-token`              | SPL Token accounts                             |
      | `spl-token-2022`         | SPL Token 2022 accounts                        |
      | `stake`                  | Stake accounts                                 |
      | `sysvar`                 | Sysvar accounts                                |
      | `vote`                   | Vote accounts                                  |

      The nested `parsed` payload remains program-specific.

      For the shared account wrapper returned in each `account` field, see
      [Account Data](/developers/docs/rpc/json-structures#account-data).
    </ParamField>

    <ParamField body="dataSlice" type="object">
      Request a slice of the account's data.

      | Field    | Type    | Description                                           |
      | -------- | ------- | ----------------------------------------------------- |
      | `offset` | `usize` | Byte offset from which to start reading account data. |
      | `length` | `usize` | Number of bytes to return.                            |

      ```json Example
      { "offset": 0, "length": 32 }
      ```

      <Info>
        Data slicing is only available for `base58`, `base64`, `base64+zstd`, and
        `binary` encodings.
      </Info>
    </ParamField>

    <ParamField body="filters" type="array">
      Filter results using up to 4 filter objects. The resultant account(s) must meet
      **all** filter criteria to be included in the returned results.

      The following filter types are supported:

      | Filter              | Type     | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
      | ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
      | `memcmp`            | `object` | Compares a provided series of bytes with account data at a particular offset. Fields: `offset` (`usize`), byte offset into account data; `bytes` (`string \| array`), data to match, provided as a base-58 string by default, a base-64 string when `encoding` is `"base64"`, or a raw byte array; `encoding` (`string`, optional), encoding for string `bytes`, either `"base58"` (default), `"base64"`, or `"bytes"`. Decoded data is limited to 128 bytes. |
      | `dataSize`          | `u64`    | Compares the account data length with the provided size.                                                                                                                                                                                                                                                                                                                                                                                                      |
      | `tokenAccountState` | N/A      | Filters for Token or Token-2022 accounts that have been initialized. No value is needed.                                                                                                                                                                                                                                                                                                                                                                      |
    </ParamField>

    <ParamField body="sortResults" type="bool" default="true">
      Values: `true`, `false`

      Whether to sort returned accounts before returning them. If disabled, do not
      assume any particular response order.
    </ParamField>
  </Expandable>
</ParamField>

## Result

<ResponseField name="result" type="array">
  By default, returns an array of JSON objects. If `withContext` flag is set, the
  array will be wrapped in an RpcResponse JSON object.

  Each object contains:

  <Expandable title="properties" defaultOpen>
    <ResponseField name="pubkey" type="string">
      The account Pubkey as base-58 encoded string
    </ResponseField>

    <ResponseField name="account" type="object">
      Account data object.

      This field uses the shared
      [Account Data](/developers/docs/rpc/json-structures#account-data) structure.

      | Field        | Type                                     | Description                                                                                                                                                                                                         |
      | ------------ | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
      | `data`       | `string \| [string, encoding] \| object` | Account data. Binary forms depend on the `encoding` parameter. When `encoding` is `jsonParsed`, this field is `{program, parsed, space}`; if no parser is available, the RPC node falls back to `[data, "base64"]`. |
      | `executable` | `bool`                                   | Whether the account contains a program and is therefore read-only.                                                                                                                                                  |
      | `lamports`   | `u64`                                    | Lamports assigned to the account.                                                                                                                                                                                   |
      | `owner`      | `string`                                 | Program owner pubkey, as a base-58 encoded string.                                                                                                                                                                  |
      | `rentEpoch`  | `u64`                                    | Next epoch at which the account owes rent.                                                                                                                                                                          |
      | `space`      | `u64 \| null`                            | Account data size in bytes. Current Solana RPC responses populate this field for encoded account responses.                                                                                                         |

      When `account.data` is returned as an object, it contains:

      | Field     | Type     | Description                                                                                 |
      | --------- | -------- | ------------------------------------------------------------------------------------------- |
      | `program` | `string` | Name of the parser that produced the decoded account data.                                  |
      | `parsed`  | `object` | Parser-specific JSON payload. Its structure depends on the owning program and account type. |
      | `space`   | `u64`    | Account data size in bytes.                                                                 |
    </ResponseField>
  </Expandable>
</ResponseField>
