Skip to content

Library Initialization

Aleksei Lebedev edited this page Mar 10, 2020 · 13 revisions

You can install API JS library using npm or add it to project's dependencies. See Readme for reference.

To use library in Node.js project, initialize it, passing passPhrase of ADAMANT account, list of ADAMANT nodes and log object:

// file api.js
const log = require('../helpers/log');
const config = require('./configReader');
module.exports = require('adamant-api')({passPhrase: config.passPhrase, node: config.node_ADM, logLevel: 'error'}, log);

List of ADAMANT nodes node is an array including nodes you want to connect. The library does the Health Check — pings all the nodes in the list using status endpoint, and connect to node which alive and with actual height. See an example of ADAMANT nodes list.

Log object is used to log current operations. You must implement error, info, warn an log methods. See an example. If no log provided, the library uses system console.

The library will use verbosity according to logLevel parameter: it can be none < error < warn < info < log.

Then refer to JS API:

// file checkerTransactions.js
const api = require('./api');
...

const txChat = (await api.get('uri', 'chats/get/?recipientId=' + Store.user.ADM.address + '&orderBy=timestamp:desc&fromHeight=' + (Store.lastHeight - 5))).transactions;

If you want to use WebSocket connections, initialize socket:

// file app.js
const api = require('./modules/api');
api.socket.initSocket({socket: config.socket, wsType: config.ws_type, onNewMessage: txParser, admAddress: Store.user.ADM.address});

Params for initSocket() example:

/** Socket connection is recommended for better user experience **/
"socket": true,

/** Choose socket connection, "ws" or "wss" depending on your server **/
"ws_type": "ws"

Function onNewMessage will be executed when new transaction appears for given admAddress with transaction object passed. In this example, txParser(transaction) will be called.

Clone this wiki locally