> ## 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.

# accountSubscribe

> Subscribe to notifications when an account's lamports or data change.

<Info>
  **Source**

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

<RequestExample>
  ```json WebSocket
  {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "accountSubscribe",
    "params": [
      "CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12",
      {
        "encoding": "jsonParsed",
        "commitment": "finalized"
      }
    ]
  }
  ```

  ```ts Kit
  import { address, createSolanaRpcSubscriptions } from "@solana/kit";

  const rpc = createSolanaRpcSubscriptions("wss://api.devnet.solana.com");

  const subscription = await rpc
    .accountNotifications(
      address("So11111111111111111111111111111111111111112"),
      {
        encoding: "jsonParsed",
        commitment: "finalized"
      }
    )
    .subscribe({ abortSignal: AbortSignal.timeout(50_000) });

  for await (const notification of subscription) {
    console.log(notification);
  }
  ```

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

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

  const publicKey = new PublicKey("CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12");

  const subscriptionId = connection.onAccountChange(
    publicKey,
    (accountInfo, context) => {
      console.log("Account changed:", accountInfo);
      console.log("Context:", context);
    },
    "finalized"
  );
  ```

  ```rs Rust
  use anyhow::Result;
  use futures::StreamExt;
  use solana_client::{nonblocking::pubsub_client::PubsubClient, rpc_config::RpcAccountInfoConfig};
  use solana_commitment_config::CommitmentConfig;
  use solana_sdk::pubkey::Pubkey;

  #[tokio::main]
  async fn main() -> Result<()> {
      let pubsub_client = PubsubClient::new("wss://api.devnet.solana.com/").await?;

      // using WSOL address
      let pubkey = Pubkey::from_str_const(&"So11111111111111111111111111111111111111112");

      let config = RpcAccountInfoConfig {
          commitment: Some(CommitmentConfig::confirmed()),
          ..Default::default()
      };

      let (mut notifications, unsubscribe) = pubsub_client
          .account_subscribe(&pubkey, Some(config))
          .await?;

      while let Some(notification) = notifications.next().await {
          println!("{:?}", notification);
      }

      unsubscribe().await;

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

<ResponseExample>
  ```json Response
  {
    "jsonrpc": "2.0",
    "result": 23784,
    "id": 1
  }
  ```
</ResponseExample>

## Parameters

<ParamField body="0" type="string" required>
  Account Pubkey, as base-58 encoded string
</ParamField>

<ParamField body="1" type="object">
  Optional configuration object.

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

      The commitment describes how finalized a block is at that point in time. See
      [Configuring State Commitment](/developers/docs/rpc#configuring-state-commitment).
    </ParamField>

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

      Encoding format for account data.

      - `base58` is slow.
      - `jsonParsed` attempts to use program-specific parsers to return more
        human-readable account state data.
      - If `jsonParsed` is requested but no parser is available, the RPC node falls
        back to `[data, "base64"]`.
      - `binary` is a deprecated legacy alias for base58 data returned as a plain
        string.
    </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>

      <Info>
        `base58` and `binary` use base-58 encoding and fail if the selected payload is
        more than 128 bytes after slicing. Use `base64` for larger accounts.
      </Info>

      <Info>
        `accountSubscribe` accepts the same config fields as
        [getAccountInfo](/developers/docs/rpc/http/getaccountinfo), but PubSub subscriptions
        currently ignore `minContextSlot`.
      </Info>
    </ParamField>
  </Expandable>
</ParamField>

## Result

<ResponseField name="result" type="number">
  Subscription id. Pass this to
  [accountUnsubscribe](/developers/docs/rpc/websocket/accountunsubscribe).
</ResponseField>

## Notification format

Notifications are delivered as `accountNotification`.

```json
{
  "jsonrpc": "2.0",
  "method": "accountNotification",
  "params": {
    "result": {
      "context": {
        "slot": 559
      },
      "value": {
        "lamports": 1169280,
        "data": {
          "program": "sysvar",
          "parsed": {
            "info": {
              "epoch": 0,
              "epochStartTimestamp": 1774643544,
              "leaderScheduleEpoch": 1,
              "slot": 559,
              "unixTimestamp": 1774643807
            },
            "type": "clock"
          },
          "space": 40
        },
        "owner": "Sysvar1111111111111111111111111111111111111",
        "executable": false,
        "rentEpoch": 0,
        "space": 40
      }
    },
    "subscription": 10
  }
}
```

<ResponseField name="jsonrpc" type="string">
  Always `"2.0"`.
</ResponseField>

<ResponseField name="method" type="string">
  Always `"accountNotification"`.
</ResponseField>

<ResponseField name="params" type="object">
  Notification wrapper with the updated account payload and the subscription id.

  <Expandable title="properties" defaultOpen>
    <ResponseField name="result" type="object">
      Notification result object with `context` and `value`. For PubSub notifications,
      `context` includes `slot` and omits `apiVersion`.

      <Expandable title="properties">
        <ResponseField name="context" type="object">
          Context for the bank slot that produced the account snapshot.

          <Expandable title="properties">
            <ResponseField name="slot" type="u64">
              Slot associated with the notification.
            </ResponseField>
          </Expandable>
        </ResponseField>

        <ResponseField name="value" type="object">
          Updated account state using the same structure documented in
          [Account Data](/developers/docs/rpc/json-structures#account-data).
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="subscription" type="integer">
      Subscription id that produced this notification.
    </ResponseField>
  </Expandable>
</ResponseField>
