# binance-websocket-api-node

> Binance websocket api using node js

Latest version **1.0.11** (published 2024-07-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install binance-websocket-api-node
pnpm add binance-websocket-api-node
yarn add binance-websocket-api-node
bun add binance-websocket-api-node
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.11 |
| Published | 2024-07-30 |
| First published | 2023-05-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 2 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Hiren Reshamwala |
| Maintainers | hiren.reshamwala |
| Keywords | binance, api, websocket, client |

## Links

- npm: https://www.npmjs.com/package/binance-websocket-api-node
- Repository: https://github.com/hirenreshamwala/binance-websocket-api-node
- Homepage: https://github.com/hirenreshamwala/binance-websocket-api-node#readme
- Issues: https://github.com/hirenreshamwala/binance-websocket-api-node/issues
- npm.io page: https://npm.io/package/binance-websocket-api-node

## Dependencies (1)

- [reconnecting-websocket](https://npm.io/package/reconnecting-websocket.md) ^4.4.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.11 (latest) — 2024-07-30
- 1.0.10 — 2024-03-27
- 1.0.9 — 2024-03-27
- 1.0.8 — 2024-03-22
- 1.0.7 — 2023-08-03
- 1.0.6 — 2023-07-10
- 1.0.5 — 2023-06-02
- 1.0.4 — 2023-05-08
- 1.0.3 — 2023-05-06
- 1.0.2 — 2023-05-05
- 1.0.0 — 2023-05-05

## README

# 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](https://binance-docs.github.io/apidocs/websocket_api/en/#change-log).

## Note

WebSocket api allows only spot trading

```js
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) ready.
Following examples will use the `await` form, which requires some configuration you will have to lookup.

### Table of Contents
- [Init](#init)
- [Public Endpoints](#public-rest-endpoints)
  - [ping](#ping)
  - [time](#time)
  - [exchangeInfo](#exchangeinfo)
- [Authenticated Endpoints](#authenticated-endpoints)
  - [order](#order)
  - [orderTest](#ordertest)
  - [orderOco](#orderoco)
  - [getOrder](#getorder)
  - [getOrderOco](#getorderoco)
  - [cancelOrder](#cancelorder)
  - [cancelOrderOco](#cancelorderoco)
  - [cancelOpenOrders](#cancelOpenOrders)
  - [cancelAndReplaceOrder](#cancelAndReplaceOrder)
  - [openOrders](#openorders)
  - [openOrdersOco](#openOrdersOco)
  - [allOrders](#allorders)
  - [allOrdersOCO](#allordersoco)  
  - [accountInfo](#accountinfo)
  - [myTrades](#mytrades)
  - [getOrderRateLimits](#getOrderRateLimits)
  - [myPreventedMatches](#myPreventedMatches)
- [Websockets](#websockets)
  - [user](#user)

### Init

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


### Public REST Endpoints

#### ping

Test connectivity to the API.

```js
console.log(await client.ping())
```

#### time

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

```js
console.log(await client.time())
```

<details>
<summary>Output</summary>

```js
1508478457643
```

</details>

#### exchangeInfo

Get the current exchange trading rules and symbol information. You can optionally
pass a symbol to only retrieve info of this specific one.

```js
console.log(await client.exchangeInfo())
```

### Authenticated Endpoints


#### order

Creates a new order. [Doc](https://binance-docs.github.io/apidocs/websocket_api/en/#place-new-order-trade)

```js
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](https://binance-docs.github.io/apidocs/websocket_api/en/#place-new-oco-trade)

```js
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](https://binance-docs.github.io/apidocs/websocket_api/en/#query-order-user_data)

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

#### getOrderOco

Retrieves a specific OCO based on provided optional parameters [Doc](https://binance-docs.github.io/apidocs/websocket_api/en/#query-oco-user_data)

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

#### cancelOrder

Cancels an active order. [Doc](https://binance-docs.github.io/apidocs/websocket_api/en/#cancel-order-trade)

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

#### cancelOrderOco

Cancel an entire Order List. [Doc](https://binance-docs.github.io/apidocs/websocket_api/en/#cancel-oco-trade)

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

#### cancelOpenOrders

Cancel all open orders on a symbol, including OCO orders.  [Doc](https://binance-docs.github.io/apidocs/websocket_api/en/#cancel-open-orders-trade)

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

#### openOrders 

Query execution status of all open orders. [Doc](https://binance-docs.github.io/apidocs/websocket_api/en/#current-open-orders-user_data)

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

#### allOrders

Query information about all your orders – active, canceled, filled – filtered by time range. [Doc](https://binance-docs.github.io/apidocs/websocket_api/en/#account-order-history-user_data)

```js
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](https://binance-docs.github.io/apidocs/websocket_api/en/#account-oco-history-user_data)

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

#### accountInfo

Query information about your account. [Doc](https://binance-docs.github.io/apidocs/websocket_api/en/#account-information-user_data)

```js
console.log(await client.accountInfo())
```

#### myTrades

Query information about all your trades, filtered by time range. [Doc](https://binance-docs.github.io/apidocs/websocket_api/en/#account-trade-history-user_data)

```js
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**

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

---
_Source: https://npm.io/package/binance-websocket-api-node · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
