BitMart JavaScript SDK
Installation and integration guidance for the BitMart SDK in JavaScript.
JavaScript SDK Usage
Use framework-neutral JavaScript request and event flows across Node.js-compatible runtimes.
In JavaScript, use the package directly from readable examples. TypeScript remains useful for larger integrations, but the same client patterns work in plain JavaScript.
What This SDK Covers
- Complete BitMart REST API integrations for exchange-specific workflows.
- Robust BitMart WebSocket support for market and account stream handling.
- JavaScript-friendly usage patterns for production integration work.
- Adaptable JavaScript examples & guides under /examples for implementation reference.
- Framework-neutral JavaScript snippets that stay approachable in all JavaScript runtimes.
Install Package
npm install bitmart-api
# or
pnpm install bitmart-api
yarn add bitmart-api
Quickstart REST API Walkthrough
Easily start calling Bitmart's spot REST APIs in JavaScript.
- Install the Bitmart JavaScript SDK via NPM:
npm install bitmart-api. - Import the RestClient class (REST API wrapper for Bitmart APIs).
- Create an authenticated RestClient instance with your API credentials (
apiKey,apiSecret,apiMemo). - Call REST API methods as functions and await the promise containing the response.
In this example, we:
- Prepare an authenticated REST client using environment variables.
- Submit a spot market sell order on
BTC_USDTusingsubmitSpotOrderV2. - Log the full API response to the console.
Commented examples are also included in the script to show how to prepare market and limit buy order payloads.
For a full map of available REST API methods, check out the endpoint reference below.
Quickstart REST API Example
import { RestClient } from 'bitmart-api';
const account = {
key: process.env.API_KEY || 'apiKeyHere',
secret: process.env.API_SECRET || 'apiSecretHere',
memo: process.env.API_MEMO || 'apiMemoHere',
};
async function start() {
const client = new RestClient({
apiKey: account.key,
apiSecret: account.secret,
apiMemo: account.memo,
});
try {
// const usdValue = 6;
// const price = 52000;
// const qty = usdValue / price;
// const limitBuyOrder = {
// symbol: 'BTC_USDT',
// side: 'buy',
// type: 'limit',
// size: String(qty),
// price: String(price),
// };
// const res = await client.submitSpotOrder({
// symbol: 'BTC_USDT',
// side: 'buy',
// type: 'market',
// size: String(qty),
// });
const res = await client.submitSpotOrderV2({
symbol: 'BTC_USDT',
side: 'sell',
type: 'market',
size: String(0.00011),
});
console.log('res ', JSON.stringify(res, null, 2));
} catch (e) {
console.error('Req error: ', e);
}
}
start();
Quickstart WebSocket Walkthrough
Connecting to Bitmart's private spot WebSocket streams is straightforward with the WebsocketClient.
- Install the Bitmart JavaScript SDK via NPM:
npm install bitmart-api. - Import the WebsocketClient and (optionally) a custom logger.
- Create an authenticated WebsocketClient instance with your API credentials (
apiKey,apiSecret,apiMemo). - Configure event handlers for key events such as
open,update,response,reconnect,reconnected,close,authenticated, andexception. - Subscribe to private spot topics and process incoming updates in real time.
In this example, we:
- Open an authenticated private WebSocket connection for spot.
- Subscribe to order progress updates for
BTC_USDT. - Demonstrate an additional (commented) private balance update topic.
This setup allows you to consume live account-level trading events without polling REST endpoints.
Quickstart WebSocket Example
import { LogParams, WebsocketClient } from 'bitmart-api';
const account = {
key: process.env.API_KEY || 'apiKeyHere',
secret: process.env.API_SECRET || 'apiSecretHere',
memo: process.env.API_MEMO || 'apiMemoHere',
};
const customLogger = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
trace: (...params: LogParams): void => {
console.log('trace', ...params);
},
info: (...params: LogParams): void => {
console.log('info', ...params);
},
error: (...params: LogParams): void => {
console.error('error', ...params);
},
};
async function start() {
const client = new WebsocketClient(
{
apiKey: account.key,
apiSecret: account.secret,
apiMemo: account.memo,
},
customLogger,
);
client.on('open', (data) => {
console.log('connected ', data?.wsKey);
});
// Data received
client.on('update', (data) => {
console.info('data received: ', JSON.stringify(data));
});
// Something happened, attempting to reconenct
client.on('reconnect', (data) => {
console.log('reconnect: ', data);
});
// Reconnect successful
client.on('reconnected', (data) => {
console.log('reconnected: ', data);
});
// Connection closed. If unexpected, expect reconnect -> reconnected.
client.on('close', (data) => {
console.error('close: ', data);
});
// Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"
client.on('response', (data) => {
console.info('response: ', data);
});
client.on('exception', (data) => {
console.error('exception: ', data);
});
client.on('authenticated', (data) => {
console.error('authenticated: ', data);
});
try {
// order progress
client.subscribe('spot/user/order:BTC_USDT', 'spot');
// balance updates
// client.subscribe('spot/user/balance:BALANCE_UPDATE', 'spot');
} catch (e) {
console.error('Req error: ', e);
}
}
start();
Endpoint Function Reference
Endpoint maps
Each REST client is a JavaScript class, which provides functions individually mapped to each endpoint available in the exchange's API offering.
The following table shows all methods available in each REST client, whether the method requires authentication (automatically handled if API keys are provided), as well as the exact endpoint each method is connected to.
This can be used to easily find which method to call, once you have found which endpoint you're looking to use.
All REST clients are in the src folder. For usage examples, make sure to check the examples folder.
List of clients:
If anything is missing or wrong, please open an issue or let us know in our Node.js Traders telegram group!
How to use table
Table consists of 4 parts:
- Function name
- AUTH
- HTTP Method
- Endpoint
Function name is the name of the function that can be called through the SDK. Check examples folder in the repo for more help on how to use them!
AUTH is a boolean value that indicates if the function requires authentication - which means you need to pass your API key and secret to the SDK.
HTTP Method shows HTTP method that the function uses to call the endpoint. Sometimes endpoints can have same URL, but different HTTP method so you can use this column to differentiate between them.
Endpoint is the URL that the function uses to call the endpoint. Best way to find exact function you need for the endpoint is to search for URL in this table and find corresponding function name.
RestClient.ts
This table includes all endpoints from the official Exchange API docs and corresponding SDK functions for each endpoint that are found in RestClient.ts.
| Function | AUTH | HTTP Method | Endpoint |
|---|---|---|---|
| getSystemTime() | GET | system/time | |
| getSystemStatus() | GET | system/service | |
| getSpotCurrenciesV1() | GET | spot/v1/currencies | |
| getSpotTradingPairsV1() | GET | spot/v1/symbols | |
| getSpotTradingPairDetailsV1() | GET | spot/v1/symbols/details | |
| getSpotTickersV3() | GET | spot/quotation/v3/tickers | |
| getSpotTickerV3() | GET | spot/quotation/v3/ticker | |
| getSpotLatestKlineV3() | GET | spot/quotation/v3/lite-klines | |
| getSpotHistoryKlineV3() | GET | spot/quotation/v3/klines | |
| getSpotOrderBookDepthV3() | GET | spot/quotation/v3/books | |
| getSpotRecentTrades() | GET | spot/quotation/v3/trades | |
| getSpotTickersV2() | GET | spot/v2/ticker | |
| getSpotTickerV1() | GET | spot/v1/ticker_detail | |
| getSpotKLineStepsV1() | GET | spot/v1/steps | |
| getSpotKlinesV1() | GET | spot/v1/symbols/kline | |
| getSpotOrderBookDepthV1() | GET | spot/v1/symbols/book | |
| getAccountBalancesV1() | 🔐 | GET | account/v1/wallet |
| getAccountCurrenciesV1() | GET | account/v1/currencies | |
| getSpotWalletBalanceV1() | 🔐 | GET | spot/v1/wallet |
| getAccountDepositAddressV1() | 🔐 | GET | account/v1/deposit/address |
| getAccountWithdrawQuotaV1() | 🔐 | GET | account/v1/withdraw/charge |
| submitWithdrawalV1() | 🔐 | POST | account/v1/withdraw/apply |
| getWithdrawAddressList() | 🔐 | GET | account/v1/withdraw/address/list |
| getDepositWithdrawHistoryV2() | 🔐 | GET | account/v2/deposit-withdraw/history |
| getDepositWithdrawDetailV1() | 🔐 | GET | account/v1/deposit-withdraw/detail |
| getMarginAccountDetailsV1() | 🔐 | GET | spot/v1/margin/isolated/account |
| submitMarginAssetTransferV1() | 🔐 | POST | spot/v1/margin/isolated/transfer |
| getBasicSpotFeeRateV1() | 🔐 | GET | spot/v1/user_fee |
| getActualSpotTradeFeeRateV1() | 🔐 | GET | spot/v1/trade_fee |
| submitSpotOrderV2() | 🔐 | POST | spot/v2/submit_order |
| submitMarginOrderV1() | 🔐 | POST | spot/v1/margin/submit_order |
| submitSpotBatchOrdersV2() | 🔐 | POST | spot/v2/batch_orders |
| cancelSpotOrderV3() | 🔐 | POST | spot/v3/cancel_order |
| submitSpotBatchOrdersV4() | 🔐 | POST | spot/v4/batch_orders |
| cancelSpotBatchOrdersV4() | 🔐 | POST | spot/v4/cancel_orders |
| cancelAllSpotOrders() | 🔐 | POST | spot/v4/cancel_all |
| cancelSpotOrdersV1() | 🔐 | POST | spot/v1/cancel_orders |
| getSpotOrderByIdV4() | 🔐 | POST | spot/v4/query/order |
| getSpotOrderByClientOrderIdV4() | 🔐 | POST | spot/v4/query/client-order |
| getSpotOpenOrdersV4() | 🔐 | POST | spot/v4/query/open-orders |
| getSpotHistoricOrdersV4() | 🔐 | POST | spot/v4/query/history-orders |
| getSpotAccountTradesV4() | 🔐 | POST | spot/v4/query/trades |
| getSpotAccountOrderTradesV4() | 🔐 | POST | spot/v4/query/order-trades |
| submitSpotAlgoOrderV4() | 🔐 | POST | spot/v4/algo/submit_order |
| cancelSpotAlgoOrderV4() | 🔐 | POST | spot/v4/algo/cancel_order |
| cancelAllSpotAlgoOrdersV4() | 🔐 | POST | spot/v4/algo/cancel_all |
| getSpotAlgoOrderByIdV4() | 🔐 | POST | spot/v4/query/algo/order |
| getSpotAlgoOrderByClientOrderIdV4() | 🔐 | POST | spot/v4/query/algo/client-order |
| getSpotAlgoOpenOrdersV4() | 🔐 | POST | spot/v4/query/algo/open-orders |
| getSpotAlgoHistoryOrdersV4() | 🔐 | POST | spot/v4/query/algo/history-orders |
| marginBorrowV1() | 🔐 | POST | spot/v1/margin/isolated/borrow |
| marginRepayV1() | 🔐 | POST | spot/v1/margin/isolated/repay |
| getMarginBorrowRecordV1() | 🔐 | GET | spot/v1/margin/isolated/borrow_record |
| getMarginRepayRecordV1() | 🔐 | GET | spot/v1/margin/isolated/repay_record |
| getMarginBorrowingRatesV1() | 🔐 | GET | spot/v1/margin/isolated/pairs |
| submitMainTransferSubToMainV1() | 🔐 | POST | account/sub-account/main/v1/sub-to-main |
| submitSubTransferSubToMainV1() | 🔐 | POST | account/sub-account/sub/v1/sub-to-main |
| submitMainTransferMainToSubV1() | 🔐 | POST | account/sub-account/main/v1/main-to-sub |
| submitMainTransferSubToSubV1() | 🔐 | POST | account/sub-account/main/v1/sub-to-sub |
| submitSubTransferSubToSubV1() | 🔐 | POST | account/sub-account/sub/v1/sub-to-sub |
| getSubTransfersV1() | 🔐 | GET | account/sub-account/main/v1/transfer-list |
| getAccountSubTransfersV1() | 🔐 | GET | account/sub-account/v1/transfer-history |
| getSubSpotWalletBalancesV1() | 🔐 | GET | account/sub-account/main/v1/wallet |
| getSubAccountsV1() | 🔐 | GET | account/sub-account/main/v1/subaccount-list |
| getFuturesContractDetails() | GET | contract/public/details | |
| getFuturesContractDepth() | GET | contract/public/depth | |
| getFuturesOpenInterest() | GET | contract/public/open-interest | |
| getFuturesFundingRate() | GET | contract/public/funding-rate | |
| getFuturesKlines() | GET | contract/public/kline | |
| getFuturesAccountAssets() | 🔐 | GET | contract/private/assets-detail |
| getFuturesAccountOrder() | 🔐 | GET | contract/private/order |
| getFuturesAccountOrderHistory() | 🔐 | GET | contract/private/order-history |
| getFuturesAccountOpenOrders() | 🔐 | GET | contract/private/get-open-orders |
| getFuturesAccountPlanOrders() | 🔐 | GET | contract/private/current-plan-order |
| getFuturesAccountPositions() | 🔐 | GET | contract/private/position |
| getPositionRiskDetails() | 🔐 | GET | contract/private/position-risk |
| getFuturesAccountTrades() | 🔐 | GET | contract/private/trades |
| getFuturesTransfers() | 🔐 | GET | account/v1/transfer-contract-list |
| submitFuturesOrder() | 🔐 | POST | contract/private/submit-order |
| cancelFuturesOrder() | 🔐 | POST | contract/private/cancel-order |
| cancelAllFuturesOrders() | 🔐 | POST | contract/private/cancel-orders |
| submitFuturesPlanOrder() | 🔐 | POST | contract/private/submit-plan-order |
| cancelFuturesPlanOrder() | 🔐 | POST | contract/private/cancel-plan-order |
| submitFuturesTransfer() | 🔐 | POST | account/v1/transfer-contract |
| setFuturesLeverage() | 🔐 | POST | contract/private/submit-leverage |
| submitFuturesSubToMainTransferFromMain() | 🔐 | POST | account/contract/sub-account/main/v1/sub-to-main |
| submitFuturesMainToSubTransferFromMain() | 🔐 | POST | account/contract/sub-account/main/v1/main-to-sub |
| submitFuturesSubToMainSubFromSub() | 🔐 | POST | account/contract/sub-account/sub/v1/sub-to-main |
| getFuturesSubWallet() | 🔐 | GET | account/contract/sub-account/main/v1/wallet |
| getFuturesSubTransfers() | 🔐 | GET | account/contract/sub-account/main/v1/transfer-list |
| getFuturesSubTransferHistory() | 🔐 | GET | account/contract/sub-account/v1/transfer-history |
| getFuturesAffiliateRebates() | 🔐 | GET | contract/private/affiliate/rebate-list |
| getFuturesAffiliateTrades() | 🔐 | GET | contract/private/affiliate/trade-list |
| getBrokerRebate() | 🔐 | GET | spot/v1/broker/rebate |
FuturesClientV2.ts
This table includes all endpoints from the official Exchange API docs and corresponding SDK functions for each endpoint that are found in FuturesClientV2.ts.
| Function | AUTH | HTTP Method | Endpoint |
|---|---|---|---|
| getSystemTime() | GET | system/time | |
| getSystemStatus() | GET | system/service | |
| getFuturesContractDetails() | GET | contract/public/details | |
| getFuturesContractDepth() | GET | contract/public/depth | |
| getFuturesMarketTrade() | GET | contract/public/market-trade | |
| getFuturesOpenInterest() | GET | contract/public/open-interest | |
| getFuturesFundingRate() | GET | contract/public/funding-rate | |
| getFuturesKlines() | GET | contract/public/kline | |
| getFuturesMarkPriceKlines() | GET | contract/public/markprice-kline | |
| getFuturesFundingRateHistory() | GET | contract/public/funding-rate-history | |
| getFuturesLeverageBracket() | GET | contract/public/leverage-bracket | |
| getFuturesAccountAssets() | 🔐 | GET | contract/private/assets-detail |
| getFuturesTradeFeeRate() | 🔐 | GET | contract/private/trade-fee-rate |
| getFuturesAccountOrder() | 🔐 | GET | contract/private/order |
| getFuturesAccountOrderHistory() | 🔐 | GET | contract/private/order-history |
| getFuturesAccountOpenOrders() | 🔐 | GET | contract/private/get-open-orders |
| getFuturesAccountPlanOrders() | 🔐 | GET | contract/private/current-plan-order |
| getFuturesAccountPositions() | 🔐 | GET | contract/private/position |
| getFuturesAccountPositionsV2() | 🔐 | GET | contract/private/position-v2 |
| getPositionRiskDetails() | 🔐 | GET | contract/private/position-risk |
| getFuturesAccountTrades() | 🔐 | GET | contract/private/trades |
| getFuturesAccountTransactionHistory() | 🔐 | GET | contract/private/transaction-history |
| getFuturesAutoRepayment() | 🔐 | GET | contract/private/auto_repayment |
| getFuturesCrossCollateralInterestLog() | 🔐 | GET | contract/private/cross_collateral/interest_log |
| getFuturesTransfers() | 🔐 | GET | account/v1/transfer-contract-list |
| submitFuturesOrder() | 🔐 | POST | contract/private/submit-order |
| updateFuturesLimitOrder() | 🔐 | POST | contract/private/modify-limit-order |
| cancelFuturesOrder() | 🔐 | POST | contract/private/cancel-order |
| cancelAllFuturesOrders() | 🔐 | POST | contract/private/cancel-orders |
| cancelAllFuturesOrdersAfter() | 🔐 | POST | contract/private/cancel-all-after |
| submitFuturesPlanOrder() | 🔐 | POST | contract/private/submit-plan-order |
| cancelFuturesPlanOrder() | 🔐 | POST | contract/private/cancel-plan-order |
| submitFuturesTransfer() | 🔐 | POST | account/v1/transfer-contract |
| setFuturesLeverage() | 🔐 | POST | contract/private/submit-leverage |
| submitFuturesTPSLOrder() | 🔐 | POST | contract/private/submit-tp-sl-order |
| updateFuturesPlanOrder() | 🔐 | POST | contract/private/modify-plan-order |
| updateFuturesPresetPlanOrder() | 🔐 | POST | contract/private/modify-preset-plan-order |
| updateFuturesTPSLOrder() | 🔐 | POST | contract/private/modify-tp-sl-order |
| submitFuturesTrailOrder() | 🔐 | POST | contract/private/submit-trail-order |
| cancelFuturesTrailOrder() | 🔐 | POST | contract/private/cancel-trail-order |
| setPositionMode() | 🔐 | POST | contract/private/set-position-mode |
| getPositionMode() | 🔐 | GET | contract/private/get-position-mode |
| submitFuturesSubToMainTransferFromMain() | 🔐 | POST | account/contract/sub-account/main/v1/sub-to-main |
| submitFuturesMainToSubTransferFromMain() | 🔐 | POST | account/contract/sub-account/main/v1/main-to-sub |
| submitFuturesSubToMainSubFromSub() | 🔐 | POST | account/contract/sub-account/sub/v1/sub-to-main |
| getFuturesSubWallet() | 🔐 | GET | account/contract/sub-account/main/v1/wallet |
| getFuturesSubTransfers() | 🔐 | GET | account/contract/sub-account/main/v1/transfer-list |
| getFuturesSubTransferHistory() | 🔐 | GET | account/contract/sub-account/v1/transfer-history |
| getFuturesAffiliateRebates() | 🔐 | GET | contract/private/affiliate/rebate-list |
| getFuturesAffiliateTrades() | 🔐 | GET | contract/private/affiliate/trade-list |
| getFuturesAffiliateRebateUser() | 🔐 | GET | contract/private/affiliate/rebate-user |
| getFuturesAffiliateRebateInviteUser() | 🔐 | GET | contract/private/affiliate/rebate-inviteUser |
| getFuturesAffiliateRebateApi() | 🔐 | GET | contract/private/affiliate/rebate-api |
| getFuturesAffiliateDepositWithdrawalList() | 🔐 | GET | contract/private/affiliate/deposit-withdrawal-list |
| submitFuturesSimulatedClaim() | 🔐 | POST | contract/private/claim |
Source: View endpoint map source
BitMart JavaScript FAQ
What does the BitMart JavaScript SDK cover?
BitMart supports Spot, Margin, Futures, and WebSockets workflows. The JavaScript guide covers the main REST and WebSocket integration patterns.
How do I authenticate private BitMart API calls in JavaScript?
Install bitmart-api from npm & pass API credentials into the SDK client options, as shown in the BitMart JavaScript examples above. The SDK handles the exchange-specific signing requirements for private requests.
Does the BitMart JavaScript SDK help with WebSocket connection management?
Yes. Use the SDK WebSocket client for subscriptions, reconnect handling, and stream lifecycle management instead of building raw socket flows yourself.
Where should I start on the BitMart JavaScript page: REST or WebSocket?
Start with the REST quick start for installation, authentication, and request and response flows. Move to the WebSocket example when you need streaming market or account updates.
Direct Example Files
Open the example files below if you want the clearest general-purpose starting point for REST API, WebSocket, and authentication workflows in plain JavaScript.
- BitMart REST API example file (Bitmart/Rest/Spot/spot-submit-order.ts)
- BitMart WebSocket example file (Bitmart/Websocket/ws-spot-private.ts)