binance contains this components
| Component | Info | Status | Document Link |
|---|---|---|---|
| Spot | Spot API | Completed | Spot |
| Futures | Futures API | Completed | Futures |
| Coin-M | Coin-M API | Completed | Coin-M |
| Options | European API | Completed | Options |
| BLVT Streams | BLVT Streams | Completed | BLVT |
| Websocket | Abstract Websocket | Completed | |
| Http | Abstract Http | Completed |
Spot: contains: Wallet, Sub-Account, Market Data, Spot, Margin, Savings, Staking, Mining, Futures, Futures Algo, Portfolio, BLVT, BSwap, Fiat, C2C, VIP Loans, Crypto Loans, Crypto Loans, Pay, Convert, Rebate, NFT, Binance Code (all available endpoints in binance spot doc)
Websocket & Http: can connect/request to any of binance ws/rest endpoints
Node ^18.0.0 and higher
(because it's using fetch API)
npm i @ixjb94/binance
import { Futures } from "@ixjb94/binance"Note: Everything isPromised so you need to do .then or await
- Rest (Public)
import { Futures } from "@ixjb94/binance"
const myFuture = new Futures({
isTestNet: true,
})
// exchange info
myFuture.exchangeInfo()
// candles data
myFuture.klines({
interval: "1m",
symbol: "BTCUSDT",
limit: 10,
})- Rest (Private)
import { Futures } from "@ixjb94/binance"
const myFuture = new Futures({
api_key: "MyApiKey",
api_secret: "MyApiSecret",
isTestNet: true,
})
// get account balance
myFuture.balance()
// place new order
myFuture.newOrder({
symbol: "BTCUSDT",
side: "BUY",
type: "MARKET",
quantity: 0.01,
})- Websocket (Public)
import { Futures } from "@ixjb94/binance"
const myFuture = new Futures({
isTestNet: true,
})
// subscribe to two market data
myFuture.ws.subscribe(["btcusdt@kline_1m", "ethusdt@kline_3m"], 1, "MyMarketData")
// listen for data coming from binance
myFuture.ws.addListener("MyMarketData", (socket) => {
socket.addEventListener("message", (event) => {
let data = event.data
data = JSON.parse(data)
console.log(data)
})
})- Websocket (Private)
import { Futures } from "@ixjb94/binance"
const myFuture = new Futures({
api_key: "MyApiKey",
api_secret: "MyApiSecret",
isTestNet: true,
})
async function Run() {
// 1- get the listenKey
const reqListenKey = await myFuture.newListenKey()
const listenKey = reqListenKey.listenKey
// 2- subscribe to User Data Stream
myFuture.ws.userStream(listenKey, "MyUserData")
// 3- Listen for data coming from binance
myFuture.ws.addListener("MyUserData", (socket) => {
socket.addEventListener("message", (event) => {
let data = event.data
data = JSON.parse(data)
console.log(data)
})
})
}
Run()| Starts | Example | Http Method |
|---|---|---|
| new | newOrder | POST |
| change | changeLeverage | POST | PUT |
| delete | deleteOrder | POST | DELETE |
| (nothing) | exchangeInfo | GET |
- ws data are either
BufferORRaw string
so you need toJSON.parsethem
example
import { Futures } from "@ixjb94/binance"
const myFuture = new Futures({
isTestNet: true,
})
myFuture.ws.subscribe(["btcusdt@kline_1m"], 1)
myFuture.ws.addListener("DATA", (socket) => {
// Buffer
socket.addListener("message", (data, siBinary) => {
console.log(data)
})
// Raw string data
socket.addEventListener("message", (event) => {
// Its raw
let data = event.data
// Now its parsed
data = JSON.parse(data)
})
})- You don't need to import Http OR Websocket directly
you can access them with.http.OR.ws.
example
import { Futures } from "@ixjb94/binance"
const myFuture = new Futures({
isTestNet: true,
})
// websocket example
myFuture.ws.subscribe(["btcusdt@kline_1m"], 1)
// http example
myFuture.http.publicGET("/fapi/v1/exchangeInfo")
