-
Notifications
You must be signed in to change notification settings - Fork 78
Query with REST API #1302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brunopgalvao
wants to merge
4
commits into
staging/product-ia
Choose a base branch
from
bruno/query-state-with-rest-api
base: staging/product-ia
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Query with REST API #1302
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,242 @@ | ||
| TODO | ||
| --- | ||
| title: Query On-Chain State with Sidecar REST API | ||
| description: Learn how to query on-chain storage data using the Substrate API Sidecar REST API service. | ||
| categories: Chain Interactions | ||
| --- | ||
|
|
||
| # Query On-Chain State with Sidecar REST API | ||
|
|
||
| ## Introduction | ||
|
|
||
| [Substrate API Sidecar](https://github.com/paritytech/substrate-api-sidecar){target=\_blank} is a REST service that makes it easy to interact with Polkadot SDK-based blockchains. It provides a simple HTTP interface to query account balances, asset information, block data, and other on-chain state without requiring WebSocket connections or SDK integrations. | ||
|
|
||
| This guide demonstrates how to query on-chain storage using the Sidecar REST API with `curl` commands. You'll learn to retrieve account balances, asset metadata, and block information from Polkadot Hub. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [curl](https://curl.se/){target=\_blank} or any HTTP client | ||
| - Access to a Sidecar instance (public or self-hosted) | ||
|
|
||
| ## Running Sidecar Locally | ||
|
|
||
| For production applications or high-frequency queries, run your own Sidecar instance. | ||
|
|
||
| === "Using npm" | ||
|
|
||
| ```bash | ||
| # Install globally | ||
| npm install -g @substrate/api-sidecar | ||
|
|
||
| # Run with default settings (connects to ws://127.0.0.1:9944) | ||
| substrate-api-sidecar | ||
|
|
||
| # Run with custom node URL | ||
| SAS_SUBSTRATE_URL=wss://polkadot-asset-hub-rpc.polkadot.io substrate-api-sidecar | ||
| ``` | ||
|
|
||
| === "Using Docker" | ||
|
|
||
| ```bash | ||
| docker run --rm -p 8080:8080 \ | ||
| -e SAS_SUBSTRATE_URL=wss://polkadot-asset-hub-rpc.polkadot.io \ | ||
| parity/substrate-api-sidecar | ||
| ``` | ||
|
|
||
| Once running, access your local instance at `http://localhost:8080`. | ||
|
|
||
| ## Public Sidecar Endpoints | ||
|
|
||
| Parity provides public Sidecar instances for Polkadot ecosystem chains: | ||
|
|
||
| | Chain | Endpoint | | ||
| |-------|----------| | ||
| | Polkadot | `https://polkadot-public-sidecar.parity-chains.parity.io` | | ||
| | Kusama | `https://kusama-public-sidecar.parity-chains.parity.io` | | ||
| | Polkadot Hub | `https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io` | | ||
| | Kusama Asset Hub | `https://kusama-asset-hub-public-sidecar.parity-chains.parity.io` | | ||
|
|
||
| For production applications, consider running your own Sidecar instance. See [Running Sidecar Locally](#running-sidecar-locally) for instructions. | ||
|
|
||
| ## Query Account Balance | ||
|
|
||
| The `/accounts/{accountId}/balance-info` endpoint returns an account's native token balance, including free, reserved, and frozen amounts. | ||
|
|
||
| ### Request | ||
|
|
||
| ```bash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/<INSERT_ADDRESS>/balance-info" | ||
| ``` | ||
|
|
||
| ### Response | ||
|
|
||
| ```json | ||
| --8<-- "code/chain-interactions/query-data/query-rest/balance-info-response.json" | ||
| ``` | ||
|
|
||
| ### Response Fields | ||
|
|
||
| | Field | Description | | ||
| |-------|-------------| | ||
| | `at.hash` | Block hash at which the query was executed | | ||
| | `at.height` | Block number at which the query was executed | | ||
| | `nonce` | Number of transactions sent from this account | | ||
| | `tokenSymbol` | Native token symbol of the chain | | ||
| | `free` | Balance available for transfers (in planck) | | ||
| | `reserved` | Balance locked for on-chain activities | | ||
| | `frozen` | Balance frozen and unavailable for transfers | | ||
| | `transferable` | Actual balance available to transfer | | ||
| | `locks` | Array of balance locks with their reasons | | ||
|
|
||
| ### Query at a Specific Block | ||
|
|
||
| You can query the balance at a specific block height or hash using the `at` query parameter: | ||
|
|
||
| ```bash | ||
| # Query at block height | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/<INSERT_ADDRESS>/balance-info?at=10000000" | ||
|
|
||
| # Query at block hash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/<INSERT_ADDRESS>/balance-info?at=0x..." | ||
| ``` | ||
|
|
||
| ## Query Asset Balances | ||
|
|
||
| The `/accounts/{accountId}/asset-balances` endpoint returns an account's balances for assets managed by the Assets pallet. | ||
|
|
||
| ### Request All Asset Balances | ||
|
|
||
| ```bash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/<INSERT_ADDRESS>/asset-balances" | ||
| ``` | ||
|
|
||
| ### Request Specific Asset Balance | ||
|
|
||
| To query a specific asset (e.g., USDT with asset ID 1984): | ||
|
|
||
| ```bash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/<INSERT_ADDRESS>/asset-balances?assets[]=1984" | ||
| ``` | ||
|
|
||
| ### Response | ||
|
|
||
| ```json | ||
| --8<-- "code/chain-interactions/query-data/query-rest/asset-balances-response.json" | ||
| ``` | ||
|
|
||
| ### Response Fields | ||
|
|
||
| | Field | Description | | ||
| |-------|-------------| | ||
| | `assetId` | Unique identifier for the asset | | ||
| | `balance` | Account's balance of this asset (in smallest unit) | | ||
| | `isFrozen` | Whether the account's asset balance is frozen | | ||
| | `isSufficient` | Whether this account's existence is being paid for by this asset balance (per-account flag, not the asset's global sufficiency setting) | | ||
|
|
||
| !!! note | ||
| The `isSufficient` field in the asset balance response is a per-account flag. To check if an asset is configured as a sufficient asset (can pay for account existence), query the [Asset Details](#query-asset-details) endpoint and check the `isSufficient` field there. | ||
|
|
||
| ### Query Multiple Assets | ||
|
|
||
| You can query multiple assets in a single request: | ||
|
|
||
| ```bash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/<INSERT_ADDRESS>/asset-balances?assets[]=1984&assets[]=1337" | ||
| ``` | ||
|
|
||
| ## Query Asset Metadata | ||
|
|
||
| Use the pallet storage endpoint to query asset metadata like name, symbol, and decimals. | ||
|
|
||
| ### Request | ||
|
|
||
| ```bash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/pallets/assets/storage/Metadata?keys[]=1984" | ||
| ``` | ||
|
|
||
| ### Response | ||
|
|
||
| ```json | ||
| --8<-- "code/chain-interactions/query-data/query-rest/asset-metadata-response.json" | ||
| ``` | ||
|
|
||
| The `name` and `symbol` fields are returned as hex-encoded strings. To decode them: | ||
|
|
||
| - `0x54657468657220555344` decodes to "Tether USD" | ||
| - `0x55534474` decodes to "USDt" | ||
|
|
||
| ## Query Asset Details | ||
|
|
||
| Query the asset configuration including owner, supply, and account count: | ||
|
|
||
| ### Request | ||
|
|
||
| ```bash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/pallets/assets/storage/Asset?keys[]=1984" | ||
| ``` | ||
|
|
||
| ### Response | ||
|
|
||
| ```json | ||
| --8<-- "code/chain-interactions/query-data/query-rest/asset-details-response.json" | ||
| ``` | ||
|
|
||
| ### Response Fields | ||
|
|
||
| | Field | Description | | ||
| |-------|-------------| | ||
| | `owner` | Account that owns the asset | | ||
| | `issuer` | Account authorized to mint new tokens | | ||
| | `admin` | Account with administrative privileges | | ||
| | `freezer` | Account that can freeze balances | | ||
| | `supply` | Total supply of the asset (in smallest unit) | | ||
| | `minBalance` | Minimum balance required to hold this asset | | ||
| | `isSufficient` | Whether this asset can pay for account existence (accounts holding only this asset don't need native tokens) | | ||
| | `accounts` | Number of accounts holding this asset | | ||
| | `sufficients` | Number of accounts whose existence is paid for by this asset | | ||
| | `status` | Asset status (Live, Frozen, or Destroying) | | ||
|
|
||
| ## Query Foreign Asset Balances | ||
|
|
||
| For cross-chain assets (foreign assets), use the `/accounts/{accountId}/foreign-asset-balances` endpoint: | ||
|
|
||
| ```bash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/<INSERT_ADDRESS>/foreign-asset-balances" | ||
| ``` | ||
|
|
||
| ## Query Block Information | ||
|
|
||
| The `/blocks/{blockId}` endpoint returns detailed block information including extrinsics and events. | ||
|
|
||
| ### Request Latest Block | ||
|
|
||
| ```bash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/blocks/head" | ||
| ``` | ||
|
|
||
| ### Request Specific Block | ||
|
|
||
| ```bash | ||
| # By block number | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/blocks/10000000" | ||
|
|
||
| # By block hash | ||
| curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/blocks/0x..." | ||
| ``` | ||
|
|
||
| ### Response | ||
|
|
||
| ```json | ||
| --8<-- "code/chain-interactions/query-data/query-rest/block-response.json" | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| For a complete list of endpoints and parameters, see the [Sidecar API Documentation](https://paritytech.github.io/substrate-api-sidecar/docsv2/){target=\_blank}. | ||
|
|
||
| ## Where to Go Next | ||
|
|
||
| Now that you understand how to query on-chain state with the REST API, explore these related topics: | ||
|
|
||
| - **[Query with SDKs](/chain-interactions/query-data/query-sdks/)** - Use TypeScript, Python, or Rust SDKs for programmatic access | ||
| - **[Runtime API Calls](/chain-interactions/query-data/runtime-api-calls/)** - Execute runtime APIs for specialized queries | ||
| - **[Send Transactions](/chain-interactions/send-transactions/with-sdks/)** - Learn to construct and submit transactions | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.