Skip to content

Getting Started

Start by installing polkadot-api

npm i polkadot-api

Next, download the latest metadata from the chain you want to connect to and generate the types:

# `papi add` is the command
# `dot` is the name we're giving to this chain (can be any JS variable name)
# `-n polkadot` specifies to download the metadata from the well-known chain polkadot
npx papi add dot -n polkadot
# Wait for the latest metadata to download, then generate the types:
npx papi

Now you can create a PolkadotClient with a provider of your choice and start interacting with the API:

// `dot` is the name we gave to `npx papi add`
import { dot } from "@polkadot-api/descriptors"
import { createClient } from "polkadot-api"
import { getSmProvider } from "polkadot-api/sm-provider";
import { chainSpec } from "polkadot-api/chains/polkadot";
import { startFromWorker } from "polkadot-api/smoldot/from-worker";
 
// Using vite
import SmWorker from "polkadot-api/smoldot/worker?worker";
const worker = new SmWorker();
 
// Using Webpack
// const worker = new Worker(
//   new URL("polkadot-api/smoldot/worker", import.meta.url)
// );
 
const smoldot = startFromWorker(worker);
const chain = await smoldot.addChain({ chainSpec });
 
// Connect to the polkadot relay chain.
const client = createClient(
  getSmProvider(chain)
);
 
// With the `client`, you can get information such as subscribing to the last
// block to get the latest hash:
client.finalizedBlock$.subscribe((finalizedBlock) =>
  console.log(finalizedBlock.number, finalizedBlock.hash),
)
 
// To interact with the chain, you need to get the `TypedApi`, which includes
// all the types for every call in that chain:
const dotApi = client.getTypedApi(dot)
 
// get the value for an account
const accountInfo = await dotApi.query.System.Account.getValue(
  "16JGzEsi8gcySKjpmxHVrkLTHdFHodRepEz8n244gNZpr9J",
)