Skip to content

Query On-Chain State with SDKs

Polkadot SDK-based blockchains store data in a key-value database that external applications can query. This on-chain state includes account balances, asset information, governance proposals, and any other data the runtime manages.

This guide demonstrates how to query on-chain storage using five popular SDKs:

Select your preferred SDK below to see complete, runnable examples that query Polkadot Hub for account balances and asset information.

Prerequisites

  • Node.js v18 or higher
  • npm, pnpm, or yarn package manager

Environment Setup

  1. Create and initialize a new project:

    Terminal window
    mkdir papi-query-example && cd papi-query-example && \
    npm init -y && npm pkg set type=module
  2. Install dependencies:

    Terminal window
    npm install polkadot-api@3.3.0 && \
    npm install --save-dev @types/node tsx typescript
  3. Generate types for Polkadot Hub:

    Terminal window
    npx papi add pah -n polkadot_asset_hub

Query Account Balance

The following example queries the System.Account storage to retrieve an account's native token balance.

Create a file named query-balance.ts and add the following code to it:

query-balance.ts
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws';
import { pah } from '@polkadot-api/descriptors';
const ASSET_HUB_RPC = 'INSERT_WS_ENDPOINT';
// Example address to query (Polkadot Hub address)
const ADDRESS = 'INSERT_ADDRESS';
async function main() {
// Create the client connection
const client = createClient(getWsProvider(ASSET_HUB_RPC));
// Get the typed API for Polkadot Hub
const api = client.getTypedApi(pah);
console.log('Connected to Polkadot Hub');
console.log(`Querying balance for: ${ADDRESS}\n`);
// Query the System.Account storage
const accountInfo = await api.query.System.Account.getValue(ADDRESS);
// Extract balance information
const { data, nonce } = accountInfo;
const { free, reserved, frozen } = data;
console.log('Account Information:');
console.log(` Nonce: ${nonce}`);
console.log(` Free Balance: ${free}`);
console.log(` Reserved: ${reserved}`);
console.log(` Frozen: ${frozen}`);
// Disconnect the client
client.destroy();
}
main().catch(console.error);

Run the script:

Terminal window
npx tsx query-balance.ts

You should see output similar to:

npx tsx query-balance.ts Connected to Polkadot Hub Querying balance for: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3 Account Information: Nonce: 0 Free Balance: 0 Reserved: 0 Frozen: 0

Query Asset Information

The following example queries the Assets pallet to retrieve metadata and balance information for USDT (asset ID 1984).

Create a file named query-asset.ts and add the following code to it:

query-asset.ts
import { Binary, createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws';
import { pah } from '@polkadot-api/descriptors';
const ASSET_HUB_RPC = 'INSERT_WS_ENDPOINT';
// USDT on Polkadot Hub
const USDT_ASSET_ID = 1984;
// Example address to query asset balance
const ADDRESS = 'INSERT_ADDRESS';
async function main() {
// Create the client connection
const client = createClient(getWsProvider(ASSET_HUB_RPC));
// Get the typed API for Polkadot Hub
const api = client.getTypedApi(pah);
console.log('Connected to Polkadot Hub');
console.log(`Querying asset ID: ${USDT_ASSET_ID}\n`);
// Query asset metadata
const assetMetadata = await api.query.Assets.Metadata.getValue(USDT_ASSET_ID);
if (assetMetadata) {
console.log('Asset Metadata:');
console.log(` Name: ${Binary.toText(assetMetadata.name)}`);
console.log(` Symbol: ${Binary.toText(assetMetadata.symbol)}`);
console.log(` Decimals: ${assetMetadata.decimals}`);
}
// Query asset details
const assetDetails = await api.query.Assets.Asset.getValue(USDT_ASSET_ID);
if (assetDetails) {
console.log('\nAsset Details:');
console.log(` Owner: ${assetDetails.owner}`);
console.log(` Supply: ${assetDetails.supply}`);
console.log(` Accounts: ${assetDetails.accounts}`);
console.log(` Min Balance: ${assetDetails.min_balance}`);
console.log(` Status: ${assetDetails.status.type}`);
}
// Query account's asset balance
console.log(`\nQuerying asset balance for: ${ADDRESS}`);
const assetAccount = await api.query.Assets.Account.getValue(
USDT_ASSET_ID,
ADDRESS
);
if (assetAccount) {
console.log('\nAsset Account:');
console.log(` Balance: ${assetAccount.balance}`);
console.log(` Status: ${assetAccount.status.type}`);
} else {
console.log('\nNo asset balance found for this account');
}
// Disconnect the client
client.destroy();
}
main().catch(console.error);

Run the script:

Terminal window
npx tsx query-asset.ts

You should see output similar to:

npx tsx query-asset.ts Connected to Polkadot Hub Querying asset ID: 1984 Asset Metadata: Name: Tether USD Symbol: USDT Decimals: 6 Asset Details: Owner: 15uPcYeUE2XaMiMJuR6W7QGW2LsLdKXX7F3PxKG8gcizPh3X Supply: 77998622834581 Accounts: 13544 Min Balance: 10000 Status: Live Querying asset balance for: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3 No asset balance found for this account
  • Runtime API Calls


    Execute runtime APIs for specialized queries.

    → Get Started

  • Send Transactions


    Learn how to construct and submit transactions.

    → Get Started

Last updated: