Skip to content

Library Initialization

martiliones edited this page Dec 17, 2023 · 13 revisions

You can install the library using npm or any other NodeJS package manager. See Readme for reference.

npm install adamant-api

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

// api.js
import { AdamantApi } from 'adamant-api'
import { customLogger } from './logger.js'

const nodes = [
  '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'
]

const api = new AdamantApi({
  /**
   * List of nodes you would like to connect.
   */
  nodes, // Required.

  /**
   * By default, adamant-api will consider node as unavailable if it
   * doesn't respond within 4 seconds. You can control the time by
   * setting this option in milliseconds. Set it to 0 for infinite wait.
   */
  timeout: 10_000, // default is `5000` (5 seconds)
  /**
   * How much retries should library do before giving up request.
   */
  maxRetries: 5, // default is `3`
  /**
   * Whenever the API should check nodes at startup and every 5 minutes.
   */
  checkHealthAtStartup: false, // default is `true`

  /**
   * 'none' (-1) < 'error' (0) < 'warn' (1) < 'info' (2) < 'log' (3).
   */
  logLevel: 'info', // default is `3` ('log')
  /**
   * Custom logger, MUST implement `error`, `warn`, `info` and `log` methods.
   */
  logger: customLogger // default is `console`
})

api.onReady(() => {
  console.log('ADAMANT API is ready to use!')
})

export { api }

Then refer to the API module in your other modules:

// blocksChecker.js
import { api } from './api.js'

const { data } = await api.getBlocks()
console.log(data)

Check out WebSocket connections page if you want to learn more about WebSocket connections in adamant-api.

Clone this wiki locally