Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@bitmartexchange/bitmart-node-sdk-api

bitmartexchange248MIT2.4.0

BitMart Exchange official Nodejs client for the BitMart Cloud API.

bitmart, API

readme

Logo

BitMart-Node-SDK-API

npm package Node version License: MIT

BitMart Exchange official Nodejs client for the BitMart Cloud API.

Feature

  • Provides exchange quick trading API
  • Easier withdrawal
  • Efficiency, higher speeds, and lower latencies
  • Priority in development and maintenance
  • Dedicated and responsive technical support
  • Provide webSocket apis calls
  • Supported APIs:
    • /spot/*
    • /contract/*
    • /account/*
    • Spot WebSocket Market Stream
    • Spot User Data Stream
    • Futures User Data Stream
    • Futures WebSocket Market Stream
  • Examples

Installation

npm install @bitmartexchange/bitmart-node-sdk-api

Documentation

API Documentation

Example

Spot Market API Example

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const bitmartSpotAPI = new Bitmart.BitmartSpotAPI()

// Get Currency List
bitmartSpotAPI.getCurrencies()
  .then(response => bitmartSpotAPI.logger.log(response.data))
  .catch(error => bitmartSpotAPI.logger.log(error))

// Get List of Trading Pairs
bitmartSpotAPI.getSymbols()
  .then(response => bitmartSpotAPI.logger.log(response.data))
  .catch(error => bitmartSpotAPI.logger.log(error))


// Get Ticker of All Pairs 
bitmartSpotAPI.getV3Tickers()
  .then(response => bitmartSpotAPI.logger.log(response.data))
  .catch(error => bitmartSpotAPI.logger.log(error))

// Get Ticker of a Trading Pair
bitmartSpotAPI.getV3Ticker('BTC_USDT')
  .then(response => bitmartSpotAPI.logger.log(response.data))
  .catch(error => bitmartSpotAPI.logger.log(error))

Spot Trade API Example

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const bitmartSpotAPI = new Bitmart.BitmartSpotAPI({
    apiKey: 'your api key',
    apiSecret: 'your secret key',
    apiMemo: 'your api memo',
})

bitmartSpotAPI.newSpotOrder('BTC_USDT', 'sell', 'limit', {
    size: 10000,
    price: "500000"
}).then(response => bitmartSpotAPI.logger.log(response.data))
    .catch(error => {
        if (error.response) {
            bitmartSpotAPI.logger.log(error.response.data);
        } else if (error.request) {
            bitmartSpotAPI.logger.log(error.request);
        } else {
            bitmartSpotAPI.logger.log('Error', error.message);
        }
    });

Please find examples/spot folder to check for more endpoints.


Spot WebSocket Public Channel Example

const { Console } = require('console')
const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')

const callbacks = {
  open: (client) => {
    // 【Public】Ticker Channel
    client.send('{"op": "subscribe", "args": ["spot/ticker:BTC_USDT"]}')

    // 【Public】KLine Channel
    client.send('{"op": "subscribe", "args": ["spot/kline1m:BTC_USDT"]}')

    // 【Public】Depth Channel
    client.send('{"op": "subscribe", "args": ["spot/depth5:BTC_USDT"]}')

    // 【Public】Trade Channel
    client.send('{"op": "subscribe", "args": ["spot/trade:BTC_USDT"]}')

  },
  close: () => console.info('...Disconnected with server'),
  pong: () => console.info('recv:pong from server'),
  ping: () => console.info('recv:ping from server'),
  message: data => console.info('recv:' + data)
}

const bitmartSpotWebsocket = new Bitmart.BitmartSpotWebsocket(
    'wss://ws-manager-compress.bitmart.com/api?protocol=1.1', { 
    callbacks: callbacks 
})

Spot WebSocket Private Channel Example

const { Console } = require('console')
const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')

const callbacks = {
  open: (client) => {
    client.login()

    // 【Private】Order Progress
    client.send('{"op": "subscribe", "args": ["spot/user/order:BTC_USDT"]}')

  },
  close: () => console.info('...Disconnected with Websocket server'),
  pong: () => console.info('recv:pong from server'),
  ping: () => console.info('recv:ping from server'),
  message: data => console.info('recv:' + data)
}

const bitmartSpotWebsocket = new Bitmart.BitmartSpotWebsocket(
    'wss://ws-manager-compress.bitmart.com/user?protocol=1.1', { 
    callbacks: callbacks,
    apiKey: 'your api key',
    apiSecret: 'your api secret',
    apiMemo: 'your api memo'
})

Futures Market API Example

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const bitmartFuturesAPI = new Bitmart.BitmartFuturesAPI()

// Get Market Depth
bitmartFuturesAPI.getDepth('BTCUSDT')
  .then(response => bitmartFuturesAPI.logger.log(response.data))
  .catch(error => bitmartFuturesAPI.logger.log(error))


// Get Futures Open Interest
bitmartFuturesAPI.getOpenInterest('BTCUSDT')
  .then(response => bitmartFuturesAPI.logger.log(response.data))
  .catch(error => bitmartFuturesAPI.logger.log(error))

// Get Current Funding Rate
bitmartFuturesAPI.getCurrentFundingRate('BTCUSDT')
  .then(response => bitmartFuturesAPI.logger.log(response.data))
  .catch(error => bitmartFuturesAPI.logger.log(error))

Futures Trade API Example

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const bitmartFuturesAPI = new Bitmart.BitmartFuturesAPI({
    apiKey: 'your api key',
    apiSecret: 'your secret',
    apiMemo: 'your memo',
})

bitmartFuturesAPI.newFuturesOrder({
    symbol: "ETHUSDT",
    client_order_id: "BM12344444",
    side: 4,
    mode: 1,
    type: "limit",
    leverage: "1",
    open_type: "isolated",
    size: 10,
    price: "2000"
}).then(response => bitmartFuturesAPI.logger.log(response.data))
    .catch(error => {
        if (error.response) {
            bitmartFuturesAPI.logger.log(error.response.data);
        } else if (error.request) {
            bitmartFuturesAPI.logger.log(error.request);
        } else {
            bitmartFuturesAPI.logger.log('Error', error.message);
        }
    });

Please find examples/futures folder to check for more endpoints.


Futures WebSocket Public Channel Example

const { Console } = require('console')
const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')

const callbacks = {
  open: (client) => {
    // 【Public】Ticker Channel
    client.send('{"action":"subscribe","args":["futures/ticker"]}')

    // 【Public】Depth Channel
    client.send('{"action":"subscribe","args":["futures/depth20:BTCUSDT"]}')

    // 【Public】Trade Channel
    client.send('{"action":"subscribe","args":["futures/trade:BTCUSDT"]}')

    // 【Public】Kline Channel
    client.send('{"action":"subscribe","args":["futures/klineBin1m:BTCUSDT"]}')

  },
  close: () => console.info('...Disconnected with Websocket server'),
  pong: () => console.info('recv:pong from server'),
  ping: () => console.info('recv:ping from server'),
  message: data => console.info('recv:' + data)
}

const bitmartFuturesWebsocket = new Bitmart.BitmartFuturesWebsocket(
    'wss://openapi-ws-v2.bitmart.com/api?protocol=1.1', { 
    callbacks: callbacks 
})

Futures WebSocket Private Channel Example

const { Console } = require('console')
const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')

const callbacks = {
  open: (client) => {
    client.login()

    // 【Private】Assets Channel
    client.send('{"action": "subscribe","args":["futures/asset:USDT", "futures/asset:BTC"]}')

    // 【Private】Position Channel
    client.send('{"action": "subscribe","args":["futures/position"]}')

    // 【Private】Order Channel
    client.send('{"action": "subscribe","args": ["futures/order"]}')

  },
  close: () => console.info('...Disconnected with Websocket server'),
  pong: () => console.info('recv:pong from server'),
  ping: () => console.info('recv:ping from server'),
  message: data => console.info('recv:' + data)
}

const bitmartFuturesWebsocket = new Bitmart.BitmartFuturesWebsocket(
    'wss://openapi-ws-v2.bitmart.com/user?protocol=1.1', { 
    callbacks: callbacks,
    apiKey: 'your api key',
    apiSecret: 'your api secret',
    apiMemo: 'your api memo'
})

Custom Logger Integration

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const fs = require('fs')
const { Console } = require('console')


// make sure the logs/ folder is created beforehand
const output = fs.createWriteStream('./logs/stdout.log')
const errorOutput = fs.createWriteStream('./logs/stderr.log')

const logger = new Console({ stdout: output, stderr: errorOutput })
const bitmartSpotAPI = new Bitmart.BitmartSpotAPI({logger: logger})

bitmartSpotAPI.getTickerDetail('BTC_USDT')
  .then(response => bitmartSpotAPI.logger.log(response.data))
  .catch(error => bitmartSpotAPI.logger.log(error))

Extra Options

Authentication

How to set API KEY?

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const bitmartSpotAPI = new Bitmart.BitmartSpotAPI({
    apiKey: 'your api key',
    apiSecret: 'your secret key',
    apiMemo: 'your api memo',
})

Timeout

Set HTTP connection timeout and read timeout.

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const bitmartSpotAPI = new Bitmart.BitmartSpotAPI({
    timeout: 2000 // Milliseconds, the default value is 5000, which means 5 seconds
})

bitmartSpotAPI.getV3Tickers()
  .then(response => bitmartSpotAPI.logger.log(response.data))
  .catch(error => bitmartSpotAPI.logger.log(error))

Logging

The default logger defined in the package is Node.js Console class. Its output is sent to process.stdout and process.stderr, same as the global console.

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const fs = require('fs')
const { Console } = require('console')

// make sure the logs/ folder is created before hand
const output = fs.createWriteStream('./logs/stdout.log')
const errorOutput = fs.createWriteStream('./logs/stderr.log')
const logger = new Console({ stdout: output, stderr: errorOutput })

const bitmartSpotAPI = new Bitmart.BitmartSpotAPI({
    logger: logger
})

bitmartSpotAPI.getV3Tickers()
  .then(response => bitmartSpotAPI.logger.log(response.data))
  .catch(error => bitmartSpotAPI.logger.log(error))

Domain

How to set API domain name? The domain name parameter is optional, the default domain name is https://api-cloud.bitmart.com.

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const bitmartSpotAPI = new Bitmart.BitmartSpotAPI({
    baseURL: 'https://api-cloud.bitmart.com'
})

bitmartSpotAPI.getV3Tickers()
  .then(response => bitmartSpotAPI.logger.log(response.data))
  .catch(error => bitmartSpotAPI.logger.log(error))

Custom request headers

You can add your own request header information here, but please do not fill in X-BM-KEY, X-BM-SIGN, X-BM-TIMESTAMP

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const bitmartSpotAPI = new Bitmart.BitmartSpotAPI({
    headers: {'Your-Custom-Header':'xxxxxxxxxx'}
})

bitmartSpotAPI.getV3Tickers()
  .then(response => bitmartSpotAPI.logger.log(response.data))
  .catch(error => bitmartSpotAPI.logger.log(error))

Response Metadata

The bitmart API server provides the endpoint rate limit usage in the header of each response. This information can be obtained from the headers property. x-bm-ratelimit-remaining indicates the number of times the current window has been used, x-bm-ratelimit-limit indicates the maximum number of times the current window can be used, and x-bm-ratelimit-reset indicates the current window time.

Example:
x-bm-ratelimit-mode: IP
x-bm-ratelimit-remaining: 10
x-bm-ratelimit-limit: 600
x-bm-ratelimit-reset: 60

This means that this IP can call the endpoint 600 times within 60 seconds, and has called 10 times so far.

const Bitmart = require('@bitmartexchange/bitmart-node-sdk-api')
const bitmartSpotAPI = new Bitmart.BitmartSpotAPI()

bitmartSpotAPI.getV3Tickers()
  .then(response => bitmartSpotAPI.logger.log(
    response.headers['x-bm-ratelimit-mode'],
    response.headers['x-bm-ratelimit-remaining'],
    response.headers['x-bm-ratelimit-limit'],
    response.headers['x-bm-ratelimit-reset'],
  ))
  .catch(error => bitmartSpotAPI.logger.log(error))

changelog

Changelog

v2.4.0 Release

New Features

  • New

    • /contract/public/markprice-kline Get MarkPrice K-line
    • /contract/private/modify-limit-order Applicable for modifying Futurs limit orders(SIGNED)
    • /contract/private/cancel-all-after Applicable for canceling all Futurs orders timed(SIGNED)
    • /contract/private/get-position-mode Get Position Mode (KEYED)
    • /contract/private/set-position-mode Set Position Mode (SIGNED)
    • /contract/private/position-v2 Get Current Position V2 (KEYED)
    • /contract/public/leverage-bracket Get Current Leverage Risk Limit
    • /contract/public/market-trade Query the latest trade data
    • /account/v1/withdraw/address/list Query Withdraw Address List (KEYED)
  • Update

    • /contract/private/submit-order Placing Futurs orders(SIGNED): Add new request field stp_mode
    • /contract/private/transaction-history Get Transaction History (KEYED): Add new request field account
    • /contract/private/trades Get Order Trade (KEYED): Add new request field account, Request field symbol changed to optional
    • /contract/private/position-risk Get Current Position Risk Details(KEYED): Add new request field account
    • /contract/private/position Get Current Position (KEYED): Add new request field account
    • /contract/private/order-history Get Order History (KEYED): Add new request field account , order_id, client_order_id
    • /contract/private/order Get Order Detail (KEYED): Add new request field account
    • /account/v1/currencies Get Currencies: Add new request field currencies
    • /account/v1/wallet Get Account Balance (KEYED): Add new request field needUsdValuation
    • /account/v2/deposit-withdraw/history Get Deposit And Withdraw History (KEYED): Add new request field startTime, endTime
    • /spot/v4/batch_orders New Batch Order(v4) (SIGNED): Add new request field stpMode
    • /spot/v2/submit_order New Order(v2) (SIGNED): Add new request field stpMode

v2.3.0 Release

New Features

  • New
    • /contract/private/submit-trail-order Submit Trail Order (SIGNED)
    • /contract/private/cancel-trail-order Cancel Trail Order (SIGNED)
    • /contract/public/funding-rate-history Query Funding Rate History
    • /contract/public/transaction-history Query Transaction History (KEYED)
  • Update
    • /contract/private/submit-order Submit Order (SIGNED): Remove the Request Parameters related replacing trail orders
    • /contract/private/modify-plan-order Modify Plan Order (SIGNED): Remove the Request Parameters client_order_id

v2.2.1 Release

Improvements

  • The domain name wss://openapi-ws.bitmart.com will not provide Futures 1.0 Websocket services. Please use the domain name wss://openapi-ws-v2.bitmart.com to access Futures 2.0 Websocket services

v2.2.0 Release

New Features

  • New
    • /contract/private/trade-fee-rate Support querying trade fee rate(KEYED)

      Improvements

    • The default url of the client BitmartFuturesAPI is set to https://api-cloud-v2.bitmart.com
    • The ping/pong mechanism of the spot websocket has been modified to support ping text

v2.1.0 Release

New Features

  • New
    • /contract/private/submit-tp-sl-order
    • /contract/private/modify-plan-order
    • /contract/private/modify-preset-plan-order
    • /contract/private/modify-tp-sl-order
  • Updated
    • /contract/private/cancel-order Add new request field client_order_id
    • /contract/private/cancel-plan-order Add new request field client_order_id
    • /contract/private/current-plan-order Add new request field plan_type
    • Func cancelFuturesOrder rename to cancelOrder in FuturesTrade class
    • Func cancelAllFuturesOrder rename to cancelAllOrder in FuturesTrade class

v2.0.0 Release

New Features

  • New
    • /spot/v4/batch_orders
    • /spot/v4/cancel_orders
    • /spot/v4/cancel_all
    • /contract/private/current-plan-order
    • /contract/private/position-risk
  • Updated
    • /account/v1/withdraw/apply
  • Removed
    • /spot/v2/ticker
    • /spot/v1/ticker_detail
    • /spot/v1/steps
    • /spot/v1/symbols/kline
    • /spot/v1/symbols/book
    • /spot/v1/symbols/trades
    • /spot/v2/batch_orders
    • /spot/v1/cancel_orders

      Improvements

  • Support custom request headers

    Bug Fixes


v1.0.1 Release

New Features

  • New endpoints for API Spot Market
    • /spot/quotation/v3/tickers Get Ticker of All Pairs (V3) /spot/quotation/v3/ticker Get Ticker of a Trading Pair(V3) /spot/quotation/v3/lite-klines Get Latest K-Line (V3) /spot/quotation/v3/klines Get History K-Line (V3) /spot/quotation/v3/books Get Depth(V3) /spot/quotation/v3/trades Get Recent Trades(V3)
  • New endpoints for API Futures Trading
    • /contract/private/submit-leverageSubmit Leverage (SIGNED)

v1.0.0 Release

New Features

  • Spot API & Websocket
  • Futures API & Websocket