Skip to main content

Connecting with PAPI

PAPI (Polkadot API) is the next-generation TypeScript library for interacting with Polkadot and Substrate-based chains, designed as a modern alternative to Polkadot.js.

Installation

npm install polkadot-api

Adding the Asset Hub chain descriptor

npx papi add polkadot_asset_hub -n polkadot_asset_hub

Connecting to the geo-steered endpoint

import { polkadot_asset_hub } from '@polkadot-api/descriptors';
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws-provider';
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat';

const client = createClient(
withPolkadotSdkCompat(getWsProvider('wss://asset-hub.polkadot.rpc.deserve.network')),
);
const api = client.getTypedApi(polkadot_asset_hub);
const blockNumber = await api.query.System.Number.getValue();
console.log(`Current block number is ${blockNumber}.`);
client.destroy();

Connecting to a regional endpoint

import { polkadot_asset_hub } from '@polkadot-api/descriptors';
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws-provider';
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat';

const client = createClient(
// replace with your preferred regional endpoint
withPolkadotSdkCompat(getWsProvider('wss://london.asset-hub.polkadot.rpc.deserve.network')),
);
const api = client.getTypedApi(polkadot_asset_hub);
const blockNumber = await api.query.System.Number.getValue();
console.log(`Current block number is ${blockNumber}.`);
client.destroy();

Available endpoints

NetworkEndpoints
Polkadot Asset HubLink
Polkadot CoretimeLink