yarn add binance-websocket-api-node
Import the module and create a new client. Passing api keys is optional only if you don't plan on doing authenticated calls. Check the binance documentation here.
WebSocket api allows only spot trading
import webSocket from "ws";
import BinanceWebSocketApi from 'binance-websocket-api-node'
const client = new BinanceWebSocketApi({
webSocket: webSocket,
})
// Authenticated client, can make signed calls
const client2 = new BinanceWebSocketApi({
apiKey: 'xxx',
apiSecret: 'xxx',
webSocket: webSocket,
})
client.time().then(time => console.log(time))
Every method returns a Promise, making this library async await ready.
Following examples will use the await
form, which requires some configuration you will have to lookup.
Param | Type | Required | Info |
---|---|---|---|
apiKey | String | false | Required when making private calls |
apiSecret | String | false | Required when making private calls |
webSocket | Object | true | Required to communicate with server |
Test connectivity to the API.
console.log(await client.ping())
Test connectivity to the Rest API and get the current server time.
console.log(await client.time())
Output
1508478457643
Get the current exchange trading rules and symbol information. You can optionally pass a symbol to only retrieve info of this specific one.
console.log(await client.exchangeInfo())
Creates a new order. Doc
console.log(
await client.order({
symbol: "BTCUSDT",
side: "SELL",
type: "LIMIT",
timeInForce: "GTC",
price: "23416.10000000",
quantity: "0.00847000",
}),
)
Test order placement. This API does not return any output on success. Validates new order parameters and verifies your signature but does not send the order into the matching engine.
Creates a new OCO order. Doc
console.log(
await client.orderOco({
symbol: "BTCUSDT",
side: "SELL",
price: "23420.00000000",
quantity: "0.00650000",
stopPrice: "23410.00000000",
stopLimitPrice: "23405.00000000",
stopLimitTimeInForce: "GTC",
newOrderRespType: "RESULT",
}),
)
Check an order's status. Doc
console.log(
await client.getOrder({
symbol: 'BTCUSDT',
orderId: 12569099453,
}),
)
Retrieves a specific OCO based on provided optional parameters Doc
console.log(
await client.getOrderOco({
origClientOrderId: "08985fedd9ea2cf6b28996"
}),
)
Cancels an active order. Doc
console.log(
await client.cancelOrder({
symbol: 'BTCUSDT',
origClientOrderId: "4d96324ff9d44481926157",
}),
)
Cancel an entire Order List. Doc
console.log(
await client.cancelOrderOco({
symbol: "BTCUSDT",
orderListId: 1274512,
}),
)
Cancel all open orders on a symbol, including OCO orders. Doc
console.log(
await client.cancelOpenOrders({
symbol: 'BTCUSDT'
}),
)
Query execution status of all open orders. Doc
console.log(
await client.openOrders({
symbol: 'BTCUSDT',
}),
)
Query information about all your orders – active, canceled, filled – filtered by time range. Doc
console.log(
await client.allOrders({
symbol: "BTCUSDT",
startTime: 1660780800000,
endTime: 1660867200000,
limit: 5,
}),
)
Query information about all your OCOs, filtered by time range. Doc
console.log(
await client.allOrdersOCO({
startTime: 1660780800000,
endTime: 1660867200000,
limit: 5,
}),
)
Query information about your account. Doc
console.log(await client.accountInfo())
Query information about all your trades, filtered by time range. Doc
console.log(
await client.myTrades({
symbol: "BTCUSDT",
startTime: 1660780800000,
endTime: 1660867200000,
}),
)
Every websocket utility returns a function you can call to close the opened connection and avoid memory issues.
Live user data feed (Account Update, Balance Update, Order Update).
Requires authentication
const clean = await client.ws.user(msg => {
console.log(msg)
})