accountSubscribe
The Solana accountSubscribe method creates a subscription to receive real-time notifications whenever a specific account's lamports or data changes.
- Must be called via a WebSocket endpoint; HTTP is not supported.
- If the WebSocket connection is dropped, subscriptions are automatically cancelled. You must re-subscribe upon reconnection.
- CU is consumed based on the volume of subscribed data, so it is recommended to use appropriate filtering options to subscribe only to the data you need.
1. Request
Parameters
The subscription request accepts the following parameters.
| Parameter | Type | Required | Description |
|---|---|---|---|
| id | integer or string | required | A unique identifier for the request. Used by the client to match requests with responses. |
| jsonrpc | string | required | The JSON-RPC protocol version. Always set to "2.0". |
| method | string | required | The name of the method to execute. Set to "accountSubscribe" here. |
| params | array | required | Account information and options to subscribe to. The first element is the account public key, and the second is a configuration object. |
| params[1].commitment | string | optional | Specifies the level of block commitment. - finalized: Queries the most recent block confirmed by a supermajority of the cluster at maximum lockout. The cluster recognizes this block as finalized. - confirmed: Queries the most recent block voted on by a supermajority of the cluster. - processed: Queries the node's most recent block. This block may still be skipped by the cluster. |
| params[1].encoding | string | optional | Encoding format for account data. - base58 (slow) - base64 - base64+zstd - jsonParsed (applies a parser if available, otherwise returns a binary string) |
Example
{
"jsonrpc": "2.0",
"id": 1,
"method": "accountSubscribe",
"params": [
"CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12",
{
"encoding": "jsonParsed",
"commitment": "finalized"
}
]
}
2. Response
Subscription Response
Upon successful subscription creation, a subscription ID is returned.
{
"jsonrpc": "2.0",
"result": 23784,
"id": 1
}
This subscription ID is required when calling the accountUnsubscribe method.
Notifications
Once the subscription is active, the server pushes notifications whenever the account state changes.
The notification format is identical to the response from the getAccountInfo RPC.
Response data format:
- Response data is delivered in one of the following formats depending on the
encodingoption:base58,base64,base64+zstd, orjsonParsed. jsonParsedconverts data to a human-readable format via program-specific parsers, but returns a binary string if no parser is available.
Base58 Encoding Example
{
"jsonrpc": "2.0",
"method": "accountNotification",
"params": {
"result": {
"context": { "slot": 5199307 },
"value": {
"data": [
"11116bv5nS2h3y12kD1yUKeMZvGcKLSjQgX6BeV7u1FrjeJcKfsHPXHRDEHrBesJhZyqnnq9qJeUuF7WHxiuLuL5twc38w2TXNLxnDbjmuR",
"base58"
],
"executable": false,
"lamports": 33594,
"owner": "11111111111111111111111111111111",
"rentEpoch": 635,
"space": 80
}
},
"subscription": 23784
}
}
JSON Parsed Encoding Example
{
"jsonrpc": "2.0",
"method": "accountNotification",
"params": {
"result": {
"context": { "slot": 5199307 },
"value": {
"data": {
"program": "nonce",
"parsed": {
"type": "initialized",
"info": {
"authority": "Bbqg1M4YVVfbhEzwA9SpC9FhsaG83YMTYoR4a8oTDLX",
"blockhash": "LUaQTmM7WbMRiATdMMHaRGakPtCkc2GHtH57STKXs6k",
"feeCalculator": { "lamportsPerSignature": 5000 }
}
}
},
"executable": false,
"lamports": 33594,
"owner": "11111111111111111111111111111111",
"rentEpoch": 635,
"space": 80
}
},
"subscription": 23784
}
}
3. How to Use
Connect to WebSocket Channel
wscat -c wss://api.mainnet-beta.solana.com
Subscribe an Account
{
"jsonrpc": "2.0",
"id": 1,
"method": "accountSubscribe",
"params": [
"CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12",
{ "encoding": "jsonParsed", "commitment": "finalized" }
]
}
Receive Notifications
- Initial response: returns a subscription ID
- Subsequent: receive "accountNotification" events when account changes occur
Unsubscribe
There are two ways to cancel a subscription:
-
Close the connection: Enter
Ctrl+Cin the terminal window to close the WebSocket connection, which automatically cancels all subscriptions. -
Cancel a specific subscription: Use accountUnsubscribe to cancel a specific subscription while keeping the connection open.
Unsubscribe request:
{
"jsonrpc": "2.0",
"id": 1,
"method": "accountUnsubscribe",
"params": [23784]
}
Response after unsubscribe:
{
"jsonrpc": "2.0",
"id": 1,
"result": true
}