UNPKG

6.16 kBMarkdownView Raw
1# @polkadot/api
2
3The Polkadot-JS API provides easy-to-use wrappers around JSONRPC calls that flow from an application to a node. It handles all the encoding and decoding or parameters, provides access to RPC functions and allows for the query of chain state and the submission of transactions.
4
5The API wrappers provide a standard interface for use -
6
7- A static `.create(<optional ApiOptions>)` that returns an API instance when connected, decorated and ready-to use. ApiOptions can include an optional WsProvider and optional custom type definitions `{ provider: <Optional WsProvider>, types: <Optional RegistryTypes> }`.
8- The above is just a wrapper for `new Api(<optional ApiOptions>) `, exposing the `isReady` getter
9- `api.rpc.<section>.<method>` provides access to actual RPC calls, be it for queries, submission or retrieving chain information
10 - [RPC (node interface)](https://polkadot.js.org/docs/substrate/rpc)
11- `api.query.<section>.<method>` provides access to chain state queries. These are dynamically populated based on what the runtime provides
12 - [Storage chain state (runtime node interface)](https://polkadot.js.org/docs/substrate/storage)
13- `api.tx.<section>.<method>` provides the ability to create a transaction, like chain state, this list is populated from a runtime query
14 - [Extrinsics (runtime node interface)](https://polkadot.js.org/docs/substrate/extrinsics)
15- `api.consts.<section>.<constant>` provides access to the module constants (parameter types).
16 - [Constants (runtime node interface)](https://polkadot.js.org/docs/substrate/constants)
17
18## API Selection
19
20There are two flavours of the API provided, one allowing a standard interface via JavaScript Promises and the second provides an Observable wrapper using [RxJS](https://github.com/ReactiveX/rxjs). Depending on your use-case and familiarity, you can choose either (or even both) for your application.
21
22- [[ApiPromise]] All interface calls returns Promises, including the static `.create(...)`. Additionally any subscription method uses `(value) => {}` callbacks, returning the value as the subscription is updated.
23- [[ApiRx]] All interface calls return RxJS Observables, including the static `.create(...)`. In the same fashion subscription-based methods return long-running Observables that update with the latest values.
24
25## Dynamic by default
26
27Substrate (upon which Polkadot is built) uses on-chain WASM runtimes, allowing for upgradability. Each runtime defining the actual chain extrinsics (submitted transactions and block intrinsics) as well as available entries in the chain state. Due to this, the API endpoints for queries and transactions are dynamically populated from the running chain.
28
29Due to this dynamic nature, this API departs from traditional APIs which only has fixed endpoints, driving use by what is available by the runtime. As a start, this generic nature has a learning curve, although the provided documentation, examples and linked documentation tries to make that experience as seamless as possible.
30
31## Installation & import
32
33Installation -
34
35```
36npm install --save @polkadot/api
37```
38
39Subscribing to blocks via Promise-based API -
40
41```javascript
42import { ApiPromise } from '@polkadot/api';
43
44// initialise via static create
45const api = await ApiPromise.create();
46
47// make a call to retrieve the current network head
48api.rpc.chain.subscribeNewHeads((header) => {
49 console.log(`Chain is at #${header.number}`);
50});
51```
52
53Subscribing to blocks via RxJS-based API -
54
55```javascript
56import { ApiRx } from '@polkadot/api';
57
58// initialise via static create
59const api = await ApiRx.create().toPromise();
60
61// make a call to retrieve the current network head
62api.rpc.chain.subscribeNewHeads().subscribe((header) => {
63 console.log(`Chain is at #${header.number}`);
64});
65```
66
67## Registering custom types
68
69Additional types used by runtime modules can be added when a new instance of the API is created. This is necessary if the runtime modules use types which are not available in the base Substrate runtime.
70
71```javascript
72import { ApiPromise } from '@polkadot/api';
73
74// initialise via static create and register custom types
75const api = await ApiPromise.create({
76 types: {
77 CustomTypesExample: {
78 "id": "u32",
79 "data": "Vec<u8>",
80 "deposit": "Balance",
81 "owner": "AccountId",
82 "application_expiry": "Moment",
83 "whitelisted": "bool",
84 "challenge_id": "u32"
85 }
86 }
87});
88```
89
90## Users
91
92Some of the users of the API (let us know if you are missing from the list), include -
93
94- [Polkadot-JS UI](https://github.com/polkadot-js/apps) A user-interface that allows you to make transactions, query the network or participate in actions on the network such as referendums and staking
95- [KodaDot](https://github.com/vue-polkadot/apps) ([twitter](https://twitter.com/KodaDot)) - Vue.js web wallet, governance dashboard and aspiring performance (lightweight) alternative to original apps, mobile-first.
96- [Polkabot](https://gitlab.com/Polkabot) Polkabot is a Matrix chatbot that keeps an eye on the Polkadot network. You can see Polkabot in action in https://matrix.to/#/#polkadot-network-status:matrix.org
97- [Polkawallet.io](https://polkawallet.io) and [Polkawallet (Github)](https://github.com/polkawallet-io/polkawallet-RN/) A mobile wallet for the Polkadot network to manage funds and make transactions, available on both Androind and iOS
98- [PolkaStats.io](https://polkastats.io), [PolkaStats frontend GitHub repository](https://github.com/Colm3na/polkastats-v2) and [PolkaStats backend GitHub repository](https://github.com/Colm3na/polkastats-backend-v2) Polkadot network statistics (currently Kusama and Alexander). Shows network information and staking details from validators and intentions.
99- [Polkadot API Server (GitHub)](https://github.com/SimplyVC/polkadot_api_server) A lightweight server for querying Polkadot nodes from any language, built primarily as a backend for [PANIC for Polkadot (GitHub)](https://github.com/SimplyVC/panic_polkadot/), a validator monitoring and alerting tool.
100- [Identity Registrar #1 from Chevdor on Westend, Kusama and Polkadot](https://www.chevdor.com/tags/registrar/)