Skip to content

Runtime API Calls

Polkadot SDK runtime APIs provide direct access to the blockchain's WebAssembly (Wasm) runtime, enabling specialized queries and computations that go beyond simple storage reads. Unlike storage queries that fetch static data, runtime APIs execute logic within the runtime to compute results dynamically.

Common runtime APIs include:

  • AccountNonceApi: Retrieves the current transaction nonce for an account.
  • Metadata: Queries available metadata versions and runtime information.
  • TransactionPaymentApi: Estimates transaction fees before submission.
  • NominationPoolsApi: Retrieves staking pool information and pending rewards.
  • StakingApi: Accesses staking-related computations.

This section demonstrates how to call runtime APIs using the following SDKs:

  • Polkadot API (PAPI): Modern TypeScript library with type-safe APIs.
  • Polkadot.js API: Comprehensive JavaScript library (maintenance mode).
  • Dedot: Lightweight TypeScript library optimized for performance.
  • Python Substrate Interface: Python library for Substrate chains.
  • Subxt: Rust library with compile-time type safety.

Select your preferred SDK below to see complete, runnable examples that query Polkadot Hub TestNet for account nonces and metadata 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-runtime-api-example && cd papi-runtime-api-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 TestNet:

    Terminal window
    npx papi add polkadotTestNet -w wss://asset-hub-paseo.dotters.network

Call Runtime APIs

The following example demonstrates calling several runtime APIs:

  • AccountNonceApi.account_nonce: Gets the current nonce for an account.
  • Metadata.metadata_versions: Retrieves supported metadata versions.

Create a file named runtime-apis.ts and add the following code:

runtime-apis.ts
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws';
import { polkadotTestNet } from '@polkadot-api/descriptors';
const POLKADOT_TESTNET_RPC = 'INSERT_WS_ENDPOINT';
// Example address to query
const ADDRESS = 'INSERT_ADDRESS';
async function main() {
// Create the client connection
const client = createClient(getWsProvider(POLKADOT_TESTNET_RPC));
// Get the typed API for Polkadot Hub TestNet
const api = client.getTypedApi(polkadotTestNet);
console.log('Connected to Polkadot Hub TestNet');
console.log(`Querying runtime APIs for: ${ADDRESS}\n`);
// Call AccountNonceApi to get the account nonce
const nonce = await api.apis.AccountNonceApi.account_nonce(ADDRESS);
console.log('AccountNonceApi Results:');
console.log(` Account Nonce: ${nonce}`);
// Query metadata versions using Metadata runtime API
const metadataVersions = await api.apis.Metadata.metadata_versions();
console.log('\nMetadata API Results:');
console.log(` Supported Metadata Versions: [${metadataVersions.join(', ')}]`);
// Disconnect the client
client.destroy();
}
main().catch(console.error);

Ensure to replace INSERT_WS_ENDPOINT with a valid WebSocket endpoint (e.g., wss://asset-hub-paseo.dotters.network) and INSERT_ADDRESS with the account address you want to query.

Run the script:

Terminal window
npx tsx runtime-apis.ts

You should see output similar to:

npx tsx runtime-apis.ts Connected to Polkadot Hub TestNet Querying runtime APIs for: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5 AccountNonceApi Results: Account Nonce: 11 Metadata API Results: Supported Metadata Versions: [14, 15, 16]

The following runtime APIs are commonly available on Polkadot SDK-based chains:

APIDescription
AccountNonceApiGet current transaction nonce for an account
TransactionPaymentApiQuery transaction fees and weight information
TransactionPaymentCallApiEstimate fees for a call without creating an extrinsic
MetadataQuery metadata versions and runtime metadata
BlockBuilderAccess block building functionality
CoreCore runtime version and execution
TaggedTransactionQueueValidate transactions in the pool
OffchainWorkerApiOffchain worker functionality
SessionKeysSession key management
GrandpaApiGRANDPA finality information
BabeApiBABE consensus information
NominationPoolsApiNomination pools data and pending rewards
StakingApiStaking-related computations

Now that you understand how to call runtime APIs to execute logic within the runtime, explore these related topics:

  • Query with SDKs


    Use TypeScript, Python, or Rust SDKs for programmatic access.

    → Get Started

  • Query On-Chain State with Sidecar REST API


    Learn how to query on-chain state using the Sidecar REST API

    → Get Started

  • Send Transactions


    Learn to construct and submit transactions.

    → Get Started

Last updated: