Files
api/packages/api
2018-10-01 14:37:10 +00:00
..
2018-10-01 14:11:43 +02:00
2018-06-13 22:27:29 +02:00
2018-10-01 14:37:10 +00:00
2018-09-19 16:14:37 +02:00

polkadotjs isc style npm travis maintainability coverage dependency devDependency

@polkadot/api

This library provides a clean wrapper around all the methods exposed by a Polkadot network client.

Usage

Installation -

npm install --save @polkadot/api

Initialisation -

import Api from '@polkadot/api';
import WsProvider from '@polkadot/api-provider/ws';

const provider = new WsProvider('http://127.0.0.1:9944');
const api = new Api(provider);

Making calls -

api.chain
  .getHeader('0x1234567890')
  .then((header) => console.log(header))
  .catch((error) => console.error(error));

Retrieving the best block (once-off) -

api.chain
  .getHead()
  .then((headerHash) => {
    return api.chain.getHeader(headerHash);
  })
  .then((header) => {
    console.log(`best #${header.number.toString()}`);
  })
  .catch((error) => {
    console.error('error:', error);
  });

Retrieving best header via subscription -

api.chain
  .newHead((error, header) => {
    if (error) {
      console.error('error:', error);
    }

    console.log(`best #${header.number.toString()}`);
  })
  .then((subscriptionId) => {
    // id for the subscription, can unsubscribe via
    // api.chain.newHead.unsubscribe(subscriptionId)
  })
  .catch((error) => {
    console.error('error subscribing:', error);
  });