Skip to content

Library Initialization

adamant-al edited this page May 27, 2021 · 13 revisions

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

Initialize the library, passing list of ADAMANT nodes and log object. It's better to initialize API library in a separate module api.js, and then refer to it.

// file api.js
const nodesList = [
  "http://localhost:36666",
  "https://endless.adamant.im",
  "https://clown.adamant.im",
  "http://23.226.231.225:36666",
  "http://88.198.156.44:36666",
  "https://lake.adamant.im"
];
module.exports = require('adamant-api')({ node: nodesList, logLevel: 'info' }, log);

List of ADAMANT nodes node is an array including nodes you want to connect.

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

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

Then refer to JS API in other modules:

// file blockChecker.js
const api = require('./api');
api.get('blocks').then(response => {
  console.log(response.data)
})

If you want to use WebSocket connections, initialize socket:

// file app.js
const api = require('./api');
api.socket.initSocket({ socket: 'true', wsType: 'ws', onNewMessage: txParser, admAddress: 'U6386412615727665758' });

Set socket to true to activate socket connections. Param wsType is one of "ws" or "wss", depending on your host.

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