Runtime API Calls¶
Introduction¶
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.
Runtimes expose additional APIs for the pallets they include. For example, a runtime with staking pallets exposes staking-related APIs.
Call Runtime APIs with Subxt¶
This section uses Subxt, Parity's Rust library for Polkadot SDK-based chains, to call runtime APIs on a local development node. The example queries an account nonce and the supported metadata versions.
The same runtime APIs can be called from third-party client libraries, for example Polkadot API (PAPI), Dedot, Polkadot.js API, and Python Substrate Interface.
Prerequisites¶
- Rust toolchain (latest stable) and Cargo.
- A local development node listening on
ws://127.0.0.1:9944, for example the parachain template node described in Run the Node Locally.
Set Up the Project¶
-
Create a new Rust project:
-
Install the Subxt CLI:
-
Download the metadata from your local node:
To target a different chain, pass its WebSocket endpoint to
--urland update theNODE_URLconstant in the example to match. -
Update
Cargo.tomlwith the required dependencies:
Call the APIs¶
The following example demonstrates calling two runtime APIs:
AccountNonceApi.account_nonce: Gets the current nonce using the static interface generated from metadata.Metadata.metadata_versions: Retrieves supported metadata versions using the dynamic interface.
Create a file at src/bin/runtime_apis.rs and add the following code:
use std::str::FromStr;
use subxt::dynamic::Value;
use subxt::utils::AccountId32;
use subxt::{OnlineClient, PolkadotConfig};
// Generate an interface from the node's metadata
#[subxt::subxt(runtime_metadata_path = "metadata.scale")]
pub mod runtime {}
// WebSocket endpoint of your local development node
const NODE_URL: &str = "ws://127.0.0.1:9944";
// Address to query (Alice's development account by default)
const ADDRESS: &str = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize the Subxt client
let api = OnlineClient::<PolkadotConfig>::from_url(NODE_URL).await?;
// Anchor to the current block
let at_block = api.at_current_block().await?;
println!("Connected to {}", NODE_URL);
println!("Querying runtime APIs for: {}\n", ADDRESS);
// Parse the address
let account = AccountId32::from_str(ADDRESS)?;
// Call AccountNonceApi using static interface
let nonce_call = runtime::runtime_apis()
.account_nonce_api()
.account_nonce(account.clone());
let nonce = at_block.runtime_apis().call(nonce_call).await?;
println!("AccountNonceApi Results:");
println!(" Account Nonce: {}", nonce);
// Call Metadata API to get supported versions using dynamic call
let metadata_versions_call =
subxt::dynamic::runtime_api_call::<_, Value>("Metadata", "metadata_versions", ());
let versions_result = at_block
.runtime_apis()
.call(metadata_versions_call)
.await?;
println!("\nMetadata API Results:");
println!(
" Supported Metadata Versions: {}",
versions_result
);
Ok(())
}
By default, the example queries Alice's development account. Replace the ADDRESS value to query a different account. Then run the script:
The script prints the account nonce and the list of metadata versions that the runtime supports.
Available Runtime APIs¶
The following runtime APIs are commonly available on Polkadot SDK-based chains:
| API | Description |
|---|---|
AccountNonceApi | Get current transaction nonce for an account |
TransactionPaymentApi | Query transaction fees and weight information |
TransactionPaymentCallApi | Estimate fees for a call without creating an extrinsic |
Metadata | Query metadata versions and runtime metadata |
BlockBuilder | Access block building functionality |
Core | Core runtime version and execution |
TaggedTransactionQueue | Validate transactions in the pool |
OffchainWorkerApi | Offchain worker functionality |
SessionKeys | Session key management |
GrandpaApi | GRANDPA finality information |
BabeApi | BABE consensus information |
Note
Available runtime APIs vary by chain. Check your target chain's metadata to see which APIs are exposed.
Where to Go Next¶
Now that you understand how to call runtime APIs to execute logic within the runtime, explore these related topics:
-
Query with SDKs
Use the Subxt Rust library to query storage.
-
Query On-Chain State with Polkadot REST API
Learn how to query on-chain state using the Polkadot REST API.
-
Send Transactions
Learn to construct and submit transactions.
| Created: November 17, 2026