---
title: accountSubscribe
description:
  Subscribe to notifications when an account's lamports or data change.
url: /docs/rpc/websocket/accountsubscribe
type: reference
hideTableOfContents: true
---

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

<Callout type="info" title="Source">
  [`account_subscribe`](https://github.com/anza-xyz/agave/blob/v3.1.8/rpc/src/rpc_pubsub.rs#L425)
</Callout>

<APIMethod>

```jsonc !!request curl
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "accountSubscribe",
  "params": [
    // !hover 0
    "CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12",
    // !hover(1:4) 1
    {
      // !hover encoding
      "encoding": "jsonParsed",
      // !hover commitment
      "commitment": "finalized"
    }
  ]
}
```

```ts !!request title="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 !!request title="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 !!request title="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()),
        encoding: None,
        data_slice: None,
        min_context_slot: None,
    };

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

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

    unsubscribe().await;

    Ok(())
}
```

### !params

#### !! 0

!type string !required

Account Pubkey, as base-58 encoded string

#### !! 1

!type object

Optional configuration object.

##### !! commitment

!type string !values processed confirmed finalized !default finalized

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

##### !! encoding

!type string !values base58 base64 base64+zstd binary jsonParsed !default binary

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.

##### !! 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 title="Example"
{ "offset": 0, "length": 32 }
```

<Callout type="info">
  Data slicing is only available for `base58`, `base64`, `base64+zstd`, and
  `binary` encodings.
</Callout>

<Callout type="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.
</Callout>

<Callout type="info">
  `accountSubscribe` accepts the same config fields as
  [getAccountInfo](/docs/rpc/http/getaccountinfo), but PubSub subscriptions
  currently ignore `minContextSlot`.
</Callout>

### !!result

```jsonc !response
{
  "jsonrpc": "2.0",
  // !hover result
  "result": 23784,
  "id": 1
}
```

!type number

Subscription id. Pass this to
[accountUnsubscribe](/docs/rpc/websocket/accountunsubscribe).

</APIMethod>

### Notification format

Notifications are delivered as `accountNotification`.

<CodeReference>

```jsonc !!
{
  // !hover jsonrpc
  "jsonrpc": "2.0",
  // !hover method
  "method": "accountNotification",
  // !hover(1:29) params
  "params": {
    // !hover(1:26) params.result
    "result": {
      // !hover(1:3) params.result.context
      "context": {
        // !hover params.result.context.slot
        "slot": 559
      },
      // !hover(1:21) params.result.value
      "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
      }
    },
    // !hover params.subscription
    "subscription": 10
  }
}
```

## !reference

### !! jsonrpc

!type string

Always `"2.0"`.

### !! method

!type string

Always `"accountNotification"`.

### !! params

!type object

Notification wrapper with the updated account payload and the subscription id.

#### !! result

!type object

Notification result object with `context` and `value`. For PubSub notifications,
`context` includes `slot` and omits `apiVersion`.

##### !! context

!type object

Context for the bank slot that produced the account snapshot.

###### !! slot

!type u64

Slot associated with the notification.

##### !! value

!type object

Updated account state using the same structure documented in
[Account Data](/docs/rpc/json-structures#account-data).

#### !! subscription

!type integer

Subscription id that produced this notification.

</CodeReference>
