Skip to content

Query Account Information with SDKs

Querying account information is a fundamental operation when interacting with Polkadot SDK-based blockchains. Account queries allow you to retrieve balances, nonces, account data, and other state information stored on-chain. Each SDK provides different methods for accessing this data efficiently.

This guide demonstrates how to query account information using five popular SDKs:

Select your preferred SDK below to see complete, runnable examples that query account information on Polkadot Hub.

  • Access to a Polkadot SDK-compatible blockchain endpoint (WebSocket URL)
  • An account address to query (can be any valid SS58 address)

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-account-example && cd papi-query-account-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 polkadotTestNet -w wss://asset-hub-paseo.dotters.network

Query Account Data

The following example queries account information including balance, nonce, and other account data.

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

query-account.ts
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws';
import { polkadotTestNet } from '@polkadot-api/descriptors';
const POLKADOT_HUB_RPC = 'INSERT_WS_ENDPOINT';
const ACCOUNT_ADDRESS = 'INSERT_ACCOUNT_ADDRESS';
const PAS_UNITS = 10_000_000_000;
async function main() {
try {
// Create the client connection
const client = createClient(getWsProvider(POLKADOT_HUB_RPC));
// Get the typed API
const api = client.getTypedApi(polkadotTestNet);
console.log('Connected to Polkadot Hub');
console.log(`\nQuerying account: ${ACCOUNT_ADDRESS}\n`);
// Query account information
const accountInfo = await api.query.System.Account.getValue(
ACCOUNT_ADDRESS
);
// Display account information
console.log('Account Information:');
console.log('===================');
console.log(`Nonce: ${accountInfo.nonce}`);
console.log(`Consumers: ${accountInfo.consumers}`);
console.log(`Providers: ${accountInfo.providers}`);
console.log(`Sufficients: ${accountInfo.sufficients}`);
console.log('\nBalance Details:');
console.log('================');
console.log(
`Free Balance: ${accountInfo.data.free} (${
Number(accountInfo.data.free) / PAS_UNITS
} PAS)`
);
console.log(
`Reserved Balance: ${accountInfo.data.reserved} (${
Number(accountInfo.data.reserved) / PAS_UNITS
} PAS)`
);
console.log(
`Frozen Balance: ${accountInfo.data.frozen} (${
Number(accountInfo.data.frozen) / PAS_UNITS
} PAS)`
);
const total =
Number(accountInfo.data.free) + Number(accountInfo.data.reserved);
console.log(`\nTotal Balance: ${total} (${total / PAS_UNITS} PAS)`);
await client.destroy();
console.log('\nDisconnected');
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
main();

Run the script:

Terminal window
npx tsx query-account.ts

You should see output similar to:

npx tsx query-account.ts Connected to Polkadot Hub Querying account: 5GgbDVeKZwCmMHzn58iFSgSZDTojRMM52arXnuNXto28R7mg Account Information: =================== Nonce: 15 Consumers: 0 Providers: 1 Sufficients: 0 Balance Details: ================ Free Balance: 59781317040 (5.978131704 PAS) Reserved Balance: 0 (0 PAS) Frozen Balance: 0 (0 PAS) Total Balance: 59781317040 (5.978131704 PAS) Disconnected

When querying account information, you'll receive several key fields:

  • Nonce: The number of transactions sent from this account, used to prevent replay attacks.
  • Consumers: The number of modules depending on this account's existence.
  • Providers: The number of modules providing for this account's existence.
  • Sufficients: The number of modules that allow this account to exist on its own.
  • Free Balance: The transferable balance available for transactions.
  • Reserved Balance: Balance that is locked for specific purposes (staking, governance, etc.).
  • Frozen Balance: Balance that cannot be used for transfers but may be used for other operations.

The total balance is the sum of free and reserved balances.

  • Query On-Chain State


    Explore other types of storage queries.

    → Get Started

  • Send Transactions


    Learn how to construct and submit transactions.

    → Get Started

Last updated: