
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
bitmart-api
Advanced tools
Complete & robust Node.js SDK for BitMart's REST APIs and WebSockets, with TypeScript declarations.
[!TIP] Upcoming change: As part of the Siebly.io brand, this SDK will soon be hosted under the Siebly.io GitHub organisation. The migration is seamless and requires no user changes.
Complete JavaScript, TypeScript & Node.js SDK for BitMart REST APIs & WebSockets:
reconnected event when dropped connection is restored.npm install --save bitmart-api
Refer to the examples folder for implementation demos, including:
Create API credentials on BitMart's website if you plan to use private endpoints or place trades.
Most methods accept JS objects. These can be populated using parameters specified by BitMart's API documentation, or check the type definition in each class within this repository.
There are two main REST API clients depending on the market you're trading:
RestClient - for spot trading, margin, and general account operationsFuturesClientV2 - dedicated client for USD-M futures trading (uses V2 domain)Use the RestClient for spot trading, margin trading, and general account operations.
import { RestClient } from 'bitmart-api';
// or if you prefer require:
// const { RestClient } = require('bitmart-api');
const API_KEY = 'yourAPIKeyHere';
const API_SECRET = 'yourAPISecretHere';
const API_MEMO = 'yourAPIMemoHere';
const client = new RestClient({
apiKey: API_KEY,
apiSecret: API_SECRET,
apiMemo: API_MEMO,
});
// For public endpoints, API credentials are optional
// const client = new RestClient();
// Get account balances
client
.getAccountBalancesV1()
.then((result) => {
console.log('Account balances: ', result);
})
.catch((err) => {
console.error('Error: ', err);
});
// Submit a spot order
client
.submitSpotOrderV2({
symbol: 'BTC_USDT',
side: 'buy',
type: 'limit',
size: '0.001',
price: '30000',
})
.then((result) => {
console.log('Order submitted: ', result);
})
.catch((err) => {
console.error('Order error: ', err);
});
// Get spot candlestick data
client
.getSpotHistoryKlineV3({
symbol: 'BTC_USDT',
step: 60, // 1 minute
from: Math.floor(Date.now() / 1000) - 3600, // 1 hour ago
to: Math.floor(Date.now() / 1000),
})
.then((result) => {
console.log('Klines: ', result);
})
.catch((err) => {
console.error('Klines error: ', err);
});
Use the FuturesClientV2 for USD-M futures trading. This client connects to BitMart's V2 futures domain.
import { FuturesClientV2 } from 'bitmart-api';
// or if you prefer require:
// const { FuturesClientV2 } = require('bitmart-api');
const futuresClient = new FuturesClientV2({
apiKey: API_KEY,
apiSecret: API_SECRET,
apiMemo: API_MEMO,
});
// Enable demo/simulated trading environment (optional)
const demoFuturesClient = new FuturesClientV2({
apiKey: API_KEY,
apiSecret: API_SECRET,
apiMemo: API_MEMO,
demoTrading: true, // Connect to simulated trading environment
});
// Get futures account assets
try {
const balances = await futuresClient.getFuturesAccountAssets();
console.log('Futures balances: ', JSON.stringify(balances, null, 2));
} catch (error) {
console.error('Error getting balances: ', error);
}
// Submit a futures order
futuresClient
.submitFuturesOrder({
symbol: 'BTCUSDT',
side: 'buy',
type: 'limit',
size: '0.001',
price: '30000',
leverage: '10',
})
.then((result) => {
console.log('Futures order submitted: ', result);
})
.catch((err) => {
console.error('Futures order error: ', err);
});
All WebSocket functionality is supported via the unified WebsocketClient. This client handles both spot and futures WebSocket streams, with automatic connection management and reconnection.
Key WebSocket features:
For public data streams, API credentials are not required:
import { WebsocketClient } from 'bitmart-api';
// or if you prefer require:
// const { WebsocketClient } = require('bitmart-api');
// Create WebSocket client for public streams
const wsClient = new WebsocketClient();
// Set up event handlers
wsClient.on('open', (data) => {
console.log('WebSocket connected: ', data?.wsKey);
});
wsClient.on('update', (data) => {
console.log('Data received: ', JSON.stringify(data, null, 2));
});
wsClient.on('reconnected', (data) => {
console.log('WebSocket reconnected: ', data);
});
wsClient.on('exception', (data) => {
console.error('WebSocket error: ', data);
});
// Subscribe to public data streams
// Spot market ticker
wsClient.subscribe('spot/ticker:BTC_USDT', 'spot');
// Spot market depth
wsClient.subscribe('spot/depth20:BTC_USDT', 'spot');
// Futures market ticker
wsClient.subscribe('futures/ticker', 'futures');
// Futures market depth
wsClient.subscribe('futures/depth20:BTCUSDT', 'futures');
// Futures trades
wsClient.subscribe('futures/trade:BTCUSDT', 'futures');
// Multiple futures kline subscriptions
wsClient.subscribe(
[
'futures/klineBin1m:BTCUSDT',
'futures/klineBin1m:ETHUSDT',
'futures/klineBin5m:BTCUSDT',
'futures/klineBin1h:ETHUSDT',
],
'futures',
);
For private account data streams, API credentials are required:
import { WebsocketClient } from 'bitmart-api';
// Create WebSocket client with API credentials for private streams
const wsClient = new WebsocketClient({
apiKey: 'yourAPIKeyHere',
apiSecret: 'yourAPISecretHere',
apiMemo: 'yourAPIMemoHere',
});
// Enable demo/simulated trading environment for V2 Futures WebSocket (optional)
const demoWsClient = new WebsocketClient({
apiKey: 'yourAPIKeyHere',
apiSecret: 'yourAPISecretHere',
apiMemo: 'yourAPIMemoHere',
demoTrading: true, // Connect to simulated trading environment (V2 Futures only)
});
// Set up event handlers
wsClient.on('open', (data) => {
console.log('Private WebSocket connected: ', data?.wsKey);
});
wsClient.on('update', (data) => {
console.log('Private data received: ', JSON.stringify(data, null, 2));
});
wsClient.on('authenticated', (data) => {
console.log('WebSocket authenticated: ', data);
});
wsClient.on('response', (data) => {
console.log('WebSocket response: ', data);
});
wsClient.on('exception', (data) => {
console.error('WebSocket error: ', data);
});
// Subscribe to private data streams
// Spot account orders
wsClient.subscribe('spot/user/order:BTC_USDT', 'spot');
// Spot account balance updates
wsClient.subscribe('spot/user/balance:USDT', 'spot');
// Futures account orders
wsClient.subscribe('futures/user/order:BTCUSDT', 'futures');
// Futures account positions
wsClient.subscribe('futures/user/position:BTCUSDT', 'futures');
For more comprehensive examples, including custom logging and error handling, check the examples folder.
BitMart provides a simulated trading environment for testing futures trading strategies. Enable demo mode by adding demoTrading: true to your client configuration:
REST API (Futures V2 only):
const demoClient = new FuturesClientV2({
apiKey: API_KEY,
apiSecret: API_SECRET,
apiMemo: API_MEMO,
demoTrading: true, // Uses https://demo-api-cloud-v2.bitmart.com
});
WebSocket (V2 Futures only):
const demoWsClient = new WebsocketClient({
apiKey: API_KEY,
apiSecret: API_SECRET,
apiMemo: API_MEMO,
demoTrading: true, // Uses wss://openapi-wsdemo-v2.bitmart.com
});
Note: Demo environment is only available for V2 Futures. Spot trading and V1 Futures will continue using production endpoints. The same API keys work for both production and demo environments.
The receive window parameter determines how long an API request is valid. This can be configured at two levels:
// Set global receive window during client initialization
const client = new RestClient({
apiKey: API_KEY,
apiSecret: API_SECRET,
apiMemo: API_MEMO,
recvWindow: 10000, // 10 seconds global default
});
// Override receive window for specific method calls
client.getAccountBalancesV1({ recvWindow: 5000 }); // 5 seconds for this call
Authentication involves HMAC signing on requests using API credentials. Internally, this SDK uses the Web Crypto API for browser compatibility. The REST client also supports injecting a custom sign function if you wish to use an alternative (such as Node.js's native & faster createHmac).
Refer to the fasterHmacSign.ts example for a complete demonstration.
Build a bundle using webpack:
npm installnpm run buildnpm run packThe bundle can be found in dist/. Altough usage should be largely consistent, smaller differences will exist. Documentation is still TODO.
This SDK includes a bundled llms.txt file in the root of the repository. If you're developing with LLMs, use the included llms.txt with your LLM - it will significantly improve the LLMs understanding of how to correctly use this SDK.
This file contains AI optimised structure of all the functions in this package, and their parameters for easier use with any learning models or artificial intelligence.
Check out our JavaScript/TypeScript/Node.js SDKs & Projects:
This connector is fully compatible with both TypeScript and pure JavaScript projects, while the connector is written in TypeScript. A pure JavaScript version can be built using npm run build, which is also the version published to npm.
The version on npm is the output from the build command and can be used in projects without TypeScript (although TypeScript is definitely recommended).
Note: The build will output both ESM and CJS, although node should automatically import the correct entrypoint for your environment.
Have my projects helped you? Share the love, there are many ways you can show your thanks:
0xA3Bda8BecaB4DCdA539Dc16F9C54a592553Be06C Contributions are encouraged, I will review any incoming pull requests. See the issues tab for todo items.
FAQs
Complete & robust Node.js SDK for BitMart's REST APIs and WebSockets, with TypeScript declarations.
We found that bitmart-api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.