|
1 | | -TODO |
| 1 | +--- |
| 2 | +title: Query On-Chain State with Sidecar REST API |
| 3 | +description: Learn how to query on-chain storage data using the Substrate API Sidecar REST API service. |
| 4 | +categories: Chain Interactions |
| 5 | +--- |
| 6 | + |
| 7 | +# Query On-Chain State with Sidecar REST API |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +[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. |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- [curl](https://curl.se/){target=\_blank} or any HTTP client |
| 18 | +- Access to a Sidecar instance (public or self-hosted) |
| 19 | + |
| 20 | +## Public Sidecar Endpoints |
| 21 | + |
| 22 | +Parity provides public Sidecar instances for Polkadot ecosystem chains: |
| 23 | + |
| 24 | +| Chain | Endpoint | |
| 25 | +|-------|----------| |
| 26 | +| Polkadot | `https://polkadot-public-sidecar.parity-chains.parity.io` | |
| 27 | +| Kusama | `https://kusama-public-sidecar.parity-chains.parity.io` | |
| 28 | +| Polkadot Hub | `https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io` | |
| 29 | +| Kusama Asset Hub | `https://kusama-asset-hub-public-sidecar.parity-chains.parity.io` | |
| 30 | + |
| 31 | +For production applications, consider running your own Sidecar instance. See [Running Sidecar Locally](#running-sidecar-locally) for instructions. |
| 32 | + |
| 33 | +## Query Account Balance |
| 34 | + |
| 35 | +The `/accounts/{accountId}/balance-info` endpoint returns an account's native token balance, including free, reserved, and frozen amounts. |
| 36 | + |
| 37 | +### Request |
| 38 | + |
| 39 | +```bash |
| 40 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3/balance-info" |
| 41 | +``` |
| 42 | + |
| 43 | +### Response |
| 44 | + |
| 45 | +```json |
| 46 | +--8<-- "code/chain-interactions/query-data/query-rest/balance-info-response.json" |
| 47 | +``` |
| 48 | + |
| 49 | +### Response Fields |
| 50 | + |
| 51 | +| Field | Description | |
| 52 | +|-------|-------------| |
| 53 | +| `at.hash` | Block hash at which the query was executed | |
| 54 | +| `at.height` | Block number at which the query was executed | |
| 55 | +| `nonce` | Number of transactions sent from this account | |
| 56 | +| `tokenSymbol` | Native token symbol of the chain | |
| 57 | +| `free` | Balance available for transfers (in planck) | |
| 58 | +| `reserved` | Balance locked for on-chain activities | |
| 59 | +| `frozen` | Balance frozen and unavailable for transfers | |
| 60 | +| `transferable` | Actual balance available to transfer | |
| 61 | +| `locks` | Array of balance locks with their reasons | |
| 62 | + |
| 63 | +### Query at a Specific Block |
| 64 | + |
| 65 | +You can query the balance at a specific block height or hash using the `at` query parameter: |
| 66 | + |
| 67 | +```bash |
| 68 | +# Query at block height |
| 69 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3/balance-info?at=10000000" |
| 70 | + |
| 71 | +# Query at block hash |
| 72 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3/balance-info?at=0x..." |
| 73 | +``` |
| 74 | + |
| 75 | +## Query Asset Balances |
| 76 | + |
| 77 | +The `/accounts/{accountId}/asset-balances` endpoint returns an account's balances for assets managed by the Assets pallet. |
| 78 | + |
| 79 | +### Request All Asset Balances |
| 80 | + |
| 81 | +```bash |
| 82 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3/asset-balances" |
| 83 | +``` |
| 84 | + |
| 85 | +### Request Specific Asset Balance |
| 86 | + |
| 87 | +To query a specific asset (e.g., USDT with asset ID 1984): |
| 88 | + |
| 89 | +```bash |
| 90 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3/asset-balances?assets[]=1984" |
| 91 | +``` |
| 92 | + |
| 93 | +### Response |
| 94 | + |
| 95 | +```json |
| 96 | +--8<-- "code/chain-interactions/query-data/query-rest/asset-balances-response.json" |
| 97 | +``` |
| 98 | + |
| 99 | +### Response Fields |
| 100 | + |
| 101 | +| Field | Description | |
| 102 | +|-------|-------------| |
| 103 | +| `assetId` | Unique identifier for the asset | |
| 104 | +| `balance` | Account's balance of this asset (in smallest unit) | |
| 105 | +| `isFrozen` | Whether the account's asset balance is frozen | |
| 106 | +| `isSufficient` | Whether this account's existence is being paid for by this asset balance (per-account flag, not the asset's global sufficiency setting) | |
| 107 | + |
| 108 | +!!! note |
| 109 | + 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. |
| 110 | + |
| 111 | +### Query Multiple Assets |
| 112 | + |
| 113 | +You can query multiple assets in a single request: |
| 114 | + |
| 115 | +```bash |
| 116 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3/asset-balances?assets[]=1984&assets[]=1337" |
| 117 | +``` |
| 118 | + |
| 119 | +## Query Asset Metadata |
| 120 | + |
| 121 | +Use the pallet storage endpoint to query asset metadata like name, symbol, and decimals. |
| 122 | + |
| 123 | +### Request |
| 124 | + |
| 125 | +```bash |
| 126 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/pallets/assets/storage/Metadata?keys[]=1984" |
| 127 | +``` |
| 128 | + |
| 129 | +### Response |
| 130 | + |
| 131 | +```json |
| 132 | +--8<-- "code/chain-interactions/query-data/query-rest/asset-metadata-response.json" |
| 133 | +``` |
| 134 | + |
| 135 | +The `name` and `symbol` fields are returned as hex-encoded strings. To decode them: |
| 136 | + |
| 137 | +- `0x54657468657220555344` decodes to "Tether USD" |
| 138 | +- `0x55534474` decodes to "USDt" |
| 139 | + |
| 140 | +## Query Asset Details |
| 141 | + |
| 142 | +Query the asset configuration including owner, supply, and account count: |
| 143 | + |
| 144 | +### Request |
| 145 | + |
| 146 | +```bash |
| 147 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/pallets/assets/storage/Asset?keys[]=1984" |
| 148 | +``` |
| 149 | + |
| 150 | +### Response |
| 151 | + |
| 152 | +```json |
| 153 | +--8<-- "code/chain-interactions/query-data/query-rest/asset-details-response.json" |
| 154 | +``` |
| 155 | + |
| 156 | +### Response Fields |
| 157 | + |
| 158 | +| Field | Description | |
| 159 | +|-------|-------------| |
| 160 | +| `owner` | Account that owns the asset | |
| 161 | +| `issuer` | Account authorized to mint new tokens | |
| 162 | +| `admin` | Account with administrative privileges | |
| 163 | +| `freezer` | Account that can freeze balances | |
| 164 | +| `supply` | Total supply of the asset (in smallest unit) | |
| 165 | +| `minBalance` | Minimum balance required to hold this asset | |
| 166 | +| `isSufficient` | Whether this asset can pay for account existence (accounts holding only this asset don't need native tokens) | |
| 167 | +| `accounts` | Number of accounts holding this asset | |
| 168 | +| `sufficients` | Number of accounts whose existence is paid for by this asset | |
| 169 | +| `status` | Asset status (Live, Frozen, or Destroying) | |
| 170 | + |
| 171 | +## Query Block Information |
| 172 | + |
| 173 | +The `/blocks/{blockId}` endpoint returns detailed block information including extrinsics and events. |
| 174 | + |
| 175 | +### Request Latest Block |
| 176 | + |
| 177 | +```bash |
| 178 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/blocks/head" |
| 179 | +``` |
| 180 | + |
| 181 | +### Request Specific Block |
| 182 | + |
| 183 | +```bash |
| 184 | +# By block number |
| 185 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/blocks/10000000" |
| 186 | + |
| 187 | +# By block hash |
| 188 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/blocks/0x..." |
| 189 | +``` |
| 190 | + |
| 191 | +### Response |
| 192 | + |
| 193 | +```json |
| 194 | +--8<-- "code/chain-interactions/query-data/query-rest/block-response.json" |
| 195 | +``` |
| 196 | + |
| 197 | +## Query Foreign Asset Balances |
| 198 | + |
| 199 | +For cross-chain assets (foreign assets), use the `/accounts/{accountId}/foreign-asset-balances` endpoint: |
| 200 | + |
| 201 | +```bash |
| 202 | +curl -s "https://polkadot-asset-hub-public-sidecar.parity-chains.parity.io/accounts/14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3/foreign-asset-balances" |
| 203 | +``` |
| 204 | + |
| 205 | +## Running Sidecar Locally |
| 206 | + |
| 207 | +For production applications or high-frequency queries, run your own Sidecar instance. |
| 208 | + |
| 209 | +### Using npm |
| 210 | + |
| 211 | +```bash |
| 212 | +# Install globally |
| 213 | +npm install -g @substrate/api-sidecar |
| 214 | + |
| 215 | +# Run with default settings (connects to ws://127.0.0.1:9944) |
| 216 | +substrate-api-sidecar |
| 217 | + |
| 218 | +# Run with custom node URL |
| 219 | +SAS_SUBSTRATE_URL=wss://polkadot-asset-hub-rpc.polkadot.io substrate-api-sidecar |
| 220 | +``` |
| 221 | + |
| 222 | +### Using Docker |
| 223 | + |
| 224 | +```bash |
| 225 | +docker run --rm -p 8080:8080 \ |
| 226 | + -e SAS_SUBSTRATE_URL=wss://polkadot-asset-hub-rpc.polkadot.io \ |
| 227 | + parity/substrate-api-sidecar |
| 228 | +``` |
| 229 | + |
| 230 | +Once running, access your local instance at `http://localhost:8080`. |
| 231 | + |
| 232 | +## API Reference |
| 233 | + |
| 234 | +For a complete list of endpoints and parameters, see the [Sidecar API Documentation](https://paritytech.github.io/substrate-api-sidecar/docsv2/){target=\_blank}. |
| 235 | + |
| 236 | +Common endpoints include: |
| 237 | + |
| 238 | +| Endpoint | Description | |
| 239 | +|----------|-------------| |
| 240 | +| `/accounts/{accountId}/balance-info` | Native token balance | |
| 241 | +| `/accounts/{accountId}/asset-balances` | Assets pallet balances | |
| 242 | +| `/accounts/{accountId}/foreign-asset-balances` | Foreign assets balances | |
| 243 | +| `/accounts/{accountId}/pool-asset-balances` | Pool asset balances | |
| 244 | +| `/blocks/{blockId}` | Block information | |
| 245 | +| `/blocks/head` | Latest block | |
| 246 | +| `/pallets/{palletId}/storage/{storageItemId}` | Pallet storage queries | |
| 247 | +| `/transaction/material` | Transaction construction material | |
| 248 | + |
| 249 | +## Where to Go Next |
| 250 | + |
| 251 | +Now that you understand how to query on-chain state with the REST API, explore these related topics: |
| 252 | + |
| 253 | +- **[Query with SDKs](/chain-interactions/query-data/query-sdks/)** - Use TypeScript, Python, or Rust SDKs for programmatic access |
| 254 | +- **[Runtime API Calls](/chain-interactions/query-data/runtime-api-calls/)** - Execute runtime APIs for specialized queries |
| 255 | +- **[Send Transactions](/chain-interactions/send-transactions/with-sdks/)** - Learn to construct and submit transactions |
0 commit comments