Installation and usage of the platform

Using the JS SDK in a node with oAuth authorization

If the node uses the oAuth authorization, it is necessary to initialize the Waves API with the authorization headers for the call.

To automatically update tokens when developing applications with the JS SDK, we recommend using the external module api-token-refresher. However, you can use your solution instead.

To work with api-token-refresher, install dependencies using npm:

npm i @wavesenterprise/api-token-refresher@3.1.0 --save, axios --save-dev, cross-fetch --save-dev, @wavesenterprise/js-sdk@3.1.1 --save

Initialize api-token-refresher as follows:

import { init: initRefresher } from '@wavesenterprise/api-token-refresher/dist/fetch'

const { fetch } = initRefresher({
  authorization: {
    access_token,
    refresh_token
  }
});

const Waves = WeSdk.create({
    initialConfiguration: config,
    fetchInstance: fetch
});

The access_token and refresh_token parameters are given in the authorization response to the loginSecure request, which is available in the browser.

The following listing contains the initialization of the library followed by the first block check:

const WeSdk = require('@wavesenterprise/js-sdk');
const { ApiTokenRefresher } = require('@wavesenterprise/api-token-refresher');

const apiTokenRefresher = new ApiTokenRefresher({
    authorization: {
        access_token: 'access_token',
        refresh_token: 'refresh_token'
    }
})

const { fetch } = apiTokenRefresher.init()

const Waves = WeSdk.create({
    initialConfiguration: {
        ...WeSdk.MAINNET_CONFIG,
        nodeAddress: 'https://hoover.welocal.dev/node-1',
        crypto: 'waves',
        networkByte: 'V'.charCodeAt(0)
    },
    fetchInstance: fetch
});

const testFirstBlock = async () => {
    const data = await Waves.API.Node.blocks.first()
    console.log('First block:', data)
}

testFirstBlock()
See also