# accountSubscribe
Solana API method that subscribes to an account to receive notifications when the lamports or data for a given account public key changes.
# Solana.py
To use the Solana API subscriptions with the solana.py
library, install the asyncstdlib
package with:
pip install asyncstdlib
# cURL
To use the Solana API subscriptions with cURL, use the code example as a message body in a WebSocket request in Postman.
Parameters:
<string>
— the account public key as a base58 encoded string.<object>
— (optional) a configuration object containing the following optional fields:commitment: <string>
— (optional) the commitment.encoding: <string>
— the encoding for account data, eitherbase58
(slow),base64
,base64+zstd
, orjsonParsed
.base58
is limited to account data of less than 128 bytes.base64
will return base64 encoded data for account data of any size.base64+zstd
compresses the account data using Zstandard and base64 encodes the result.jsonParsed
encoding attempts to use program-specific state parsers to return more human-readable and explicit account state data. IfjsonParsed
is requested, but a parser cannot be found, the field falls back to base64 encoding, detectable when the data field is<string>
type.
Returns:
<number>
— the subscription ID which is needed to unsubscribe.
Example:
- Solana web3.js
- Solana.py
- cURL
import { PublicKey, Connection } from "@solana/web3.js";
const web3 = new Connection("CHAINSTACK_HTTPS_URL", {
wsEndpoint: "CHAINSTACK_WSS_URL",
});
(async () => {
const publicKey = new PublicKey(
"HSH3LftAhgNEQmpNRuE1ghnbqVHsxt8edvid1zdLxH5C"
);
web3.onAccountChange(
publicKey,
(updatedAccountInfo) =>
console.log("Updated account info: ", updatedAccountInfo),
"confirmed"
);
})();