1.0.10 • Published 1 month ago

binance-websocket-api-node v1.0.10

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

binance-websocket-api-node

Installation

yarn add binance-websocket-api-node

Getting started

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.

Note

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.

Table of Contents

Init

ParamTypeRequiredInfo
apiKeyStringfalseRequired when making private calls
apiSecretStringfalseRequired when making private calls
webSocketObjecttrueRequired to communicate with server

Public REST Endpoints

ping

Test connectivity to the API.

console.log(await client.ping())

time

Test connectivity to the Rest API and get the current server time.

console.log(await client.time())
1508478457643

exchangeInfo

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())

Authenticated Endpoints

order

Creates a new order. Doc

console.log(
  await client.order({
    symbol: "BTCUSDT",
    side: "SELL",
    type: "LIMIT",
    timeInForce: "GTC",
    price: "23416.10000000",
    quantity: "0.00847000",
  }),
)

orderTest

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.

orderOco

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",
  }),
)

getOrder

Check an order's status. Doc

console.log(
  await client.getOrder({
    symbol: 'BTCUSDT',
    orderId: 12569099453,
  }),
)

getOrderOco

Retrieves a specific OCO based on provided optional parameters Doc

console.log(
  await client.getOrderOco({
    origClientOrderId: "08985fedd9ea2cf6b28996"
  }),
)

cancelOrder

Cancels an active order. Doc

console.log(
  await client.cancelOrder({
    symbol: 'BTCUSDT',
    origClientOrderId: "4d96324ff9d44481926157",
  }),
)

cancelOrderOco

Cancel an entire Order List. Doc

console.log(
  await client.cancelOrderOco({
    symbol: "BTCUSDT",
    orderListId: 1274512,
  }),
)

cancelOpenOrders

Cancel all open orders on a symbol, including OCO orders. Doc

console.log(
  await client.cancelOpenOrders({
    symbol: 'BTCUSDT'
  }),
)

openOrders

Query execution status of all open orders. Doc

console.log(
  await client.openOrders({
    symbol: 'BTCUSDT',
  }),
)

allOrders

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,
  }),
)

allOrdersOCO

Query information about all your OCOs, filtered by time range. Doc

console.log(
  await client.allOrdersOCO({
    startTime: 1660780800000,
    endTime: 1660867200000,
    limit: 5,
  }),
)

accountInfo

Query information about your account. Doc

console.log(await client.accountInfo())

myTrades

Query information about all your trades, filtered by time range. Doc

console.log(
  await client.myTrades({
    symbol: "BTCUSDT",
    startTime: 1660780800000,
    endTime: 1660867200000,
  }),
)

WebSockets

Every websocket utility returns a function you can call to close the opened connection and avoid memory issues.

user

Live user data feed (Account Update, Balance Update, Order Update).

Requires authentication

const clean = await client.ws.user(msg => {
  console.log(msg)
})
1.0.10

1 month ago

1.0.9

1 month ago

1.0.8

2 months ago

1.0.7

9 months ago

1.0.6

10 months ago

1.0.5

11 months ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.0

1 year ago