|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +Start by installing `polkadot-api`, and download the latest metadata from the chain you want to connect to and generate the types: |
| 4 | + |
| 5 | +:::code-group |
| 6 | + |
| 7 | +```sh [npm] |
| 8 | +npm i polkadot-api |
| 9 | + |
| 10 | +# `papi add` is the command |
| 11 | +# `dot` is the name we're giving to this chain (can be any JS variable name) |
| 12 | +# `-n polkadot` specifies to download the metadata from the well-known chain polkadot |
| 13 | +npx papi add dot -n polkadot |
| 14 | +# Wait for the latest metadata to download, then generate the types: |
| 15 | +npx papi |
| 16 | +``` |
| 17 | + |
| 18 | +```sh [pnpm] |
| 19 | +pnpm add polkadot-api |
| 20 | + |
| 21 | +# `papi add` is the command |
| 22 | +# `dot` is the name we're giving to this chain (can be any JS variable name) |
| 23 | +# `-n polkadot` specifies to download the metadata from the well-known chain polkadot |
| 24 | +pnpm papi add dot -n polkadot |
| 25 | +# Wait for the latest metadata to download, then generate the types: |
| 26 | +pnpm papi |
| 27 | +``` |
| 28 | + |
| 29 | +```sh [bun] |
| 30 | +bun add polkadot-api |
| 31 | + |
| 32 | +# `papi add` is the command |
| 33 | +# `dot` is the name we're giving to this chain (can be any JS variable name) |
| 34 | +# `-n polkadot` specifies to download the metadata from the well-known chain polkadot |
| 35 | +bun papi add dot -n polkadot |
| 36 | +# Wait for the latest metadata to download, then generate the types: |
| 37 | +bun papi |
| 38 | +``` |
| 39 | + |
| 40 | +::: |
| 41 | + |
| 42 | +:::info |
| 43 | +It's a really good idea to add papi to the "postinstall" script in package.json to automate generating the types after installation. |
| 44 | +::: |
| 45 | + |
| 46 | +Now you can create a `PolkadotClient` instance with a [provider](/providers) of your choice and start interacting with the API: |
| 47 | + |
| 48 | +### 1. Create the provider and Start the client |
| 49 | + |
| 50 | +:::code-group |
| 51 | + |
| 52 | +```typescript [Smoldot] |
| 53 | +// [!include ~/snippets/gettingStarted.ts:import] |
| 54 | +import { getSmProvider } from "polkadot-api/sm-provider" |
| 55 | +import { chainSpec } from "polkadot-api/chains/polkadot" |
| 56 | +import { start } from "polkadot-api/smoldot" |
| 57 | + |
| 58 | +// if interested, check out how to create a smoldot instance in a WebWorker |
| 59 | +// http://papi.how/providers/sm#webworker |
| 60 | +const smoldot = start() |
| 61 | +const chain = await smoldot.addChain({ chainSpec }) |
| 62 | + |
| 63 | +// Connect to the polkadot relay chain. |
| 64 | +const client = createClient(getSmProvider(chain)) |
| 65 | +``` |
| 66 | + |
| 67 | +```typescript [WebSocket] |
| 68 | +// [!include ~/snippets/gettingStarted.ts:import] |
| 69 | +import { getWsProvider } from "polkadot-api/ws-provider" |
| 70 | +import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat" |
| 71 | + |
| 72 | +// Connect to the polkadot relay chain. |
| 73 | +const client = createClient( |
| 74 | + // Polkadot-SDK Nodes have issues, see the documentation for more info |
| 75 | + // on this enhancer https://papi.how/providers/enhancers#polkadot-sdk-compatibility-layer |
| 76 | + withPolkadotSdkCompat(getWsProvider("wss://dot-rpc.stakeworld.io")), |
| 77 | +) |
| 78 | +``` |
| 79 | + |
| 80 | +::: |
| 81 | + |
| 82 | +### 2. Start consuming the client! |
| 83 | + |
| 84 | +```typescript |
| 85 | +// [!include ~/snippets/gettingStarted.ts:usage] |
| 86 | +``` |
0 commit comments