API

Overview

The API provides application developers the ability to query a node and interact with the Polkadot or Substrate chains using Javascript. Here you will find documentation and examples to get you started.
A complete overview on using the API in your projects, from installation all the way through to making it do magic. Have things working and want tips? Have a look at the complete library: https://polkadot.js.org/docs/​
Separated from the API you will also find the Substrate metadata section in the documentation that has an overview of the RPC, extrinsics, events, and errors available on a typical Substrate node.
There are a number of organisations that maintain free RPC WebSocket endpoints for the Astar community.
Shiden:
  • Stake Technologies: wss://rpc.shiden.plasmnet.io
  • OnFinality: wss://shiden.api.onfinality.io/public-ws
Plasm:
  • Stake Technologies: wss://rpc.plasmnet.io/
  • Patract Elara: wss://plasm.elara.patract.io
Dusty:
  • Stake Technologies: wss://rpc.dusty.plasmnet.io/

Installation

Follow these steps to use Polkadot API on Plasm Network. Yes, it really is as simple as installing from npm, so we are not going to waste too much time with the bare basics, just install the necessary APIs via:
1
npm i @polkadot/api
2
npm i @plasm/types
Copied!

Example

This example shows how to subscribe to new blocks. It will listen to our testnet server Dusty. It displays the block number every time a new block is seen by the node you are connected to.
NOTE: The example runs until you stop it with CTRL+C
1
const { ApiPromise, WsProvider, Keyring } = require('@polkadot/api');
2
const plasmDefinitions = require('@plasm/types/interfaces/definitions');
3
​
4
async function main() {
5
const types = Object.values(plasmDefinitions).reduce((res, { types }) => ({ ...res, ...types }), {
6
SmartContract: {
7
_enum: {
8
Wasm: 'AccountId',
9
Evm: 'H160'
10
}
11
},
12
});
13
​
14
// Other public RPC endpoints listed above
15
const wsProvider = new WsProvider('wss://rpc.dusty.plasmnet.io');
16
​
17
const api = await ApiPromise.create({
18
provider: wsProvider,
19
types: {
20
...types,
21
// aliases that don't do well as part of interfaces
22
'voting::VoteType': 'VoteType',
23
'voting::TallyType': 'TallyType',
24
// chain-specific overrides
25
Address: 'GenericAddress',
26
Keys: 'SessionKeys4',
27
StakingLedger: 'StakingLedgerTo223',
28
Votes: 'VotesTo230',
29
ReferendumInfo: 'ReferendumInfoTo239',
30
},
31
// override duplicate type name
32
typesAlias: { voting: { Tally: 'VotingTally' } },
33
});
34
​
35
const [chain, nodeName, nodeVersion] = await Promise.all([
36
api.rpc.system.chain(),
37
api.rpc.system.name(),
38
api.rpc.system.version()
39
]);
40
console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);
41
​
42
api.rpc.chain.subscribeNewHeads(async (header) => {
43
console.log(`Chain is at #${header.number}`);
44
});
45
}
46
main().catch(console.error);
Copied!
When you run this script you will see the following:

Support

Please join our developer's hub on Discord.
Last modified 4mo ago