Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BINANCE API - How to get Account info with User Data Stream

I'm using Node and the ws npm package to work with WebSockets. Got the listenKey as stated in the docs (below), but I'm unable to get my account info using User Data Stream. I'd prefer to use a stream to read my most current account info (balances, etc) since using the Rest API to do it incurs a penalty (WEIGHT: 5) each time.

I've tried doing ws.send('outboundAccountInfo') but no joy.

DOCS: https://github.com/binance-exchange/binance-official-api-docs/blob/master/user-data-stream.md

Full code example - does not return any data:

import request from 'request'
import WebSocket from 'ws'

import { API_KEY } from '../../assets/secrets'


const DATA_STREAM_ENDPOINT = 'wss://stream.binance.com:9443/ws'
const BINANCE_API_ROOT = 'https://api.binance.com'
const LISTEN_KEY_ENDPOINT = `${BINANCE_API_ROOT}/api/v1/userDataStream`

const fetchAccountWebsocketData = async() => { 
  const listenKey = await fetchListenKey()

  console.log('-> ', listenKey) // valid key is returned

  let ws

  try {
    ws = await openWebSocket(`${DATA_STREAM_ENDPOINT}/${listenKey}`)
  } catch (err) {
    throw(`ERROR - fetchAccountWebsocketData: ${err}`)
  }

  // Nothing returns from either
  ws.on('message', data => console.log(data))
  ws.on('outboundAccountInfo', accountData => console.log(accountData))
}

const openWebSocket = endpoint => {
  const p = new Promise((resolve, reject) => {
    const ws = new WebSocket(endpoint)

    console.log('\n-->> New Account Websocket')

    ws.on('open', () => {
      console.log('\n-->> Websocket Account open...')
      resolve(ws)
    }, err => { 
      console.log('fetchAccountWebsocketData error:', err)
      reject(err) 
    })
  })

  p.catch(err => console.log(`ERROR - fetchAccountWebsocketData: ${err}`))
  return p
}

const fetchListenKey = () => {
  const p = new Promise((resolve, reject) => {
    const options = {
      url: LISTEN_KEY_ENDPOINT, 
      headers: {'X-MBX-APIKEY': API_KEY}
    }

    request.post(options, (err, httpResponse, body) => {
      if (err) 
        return reject(err)

      resolve(JSON.parse(body).listenKey)
    })
  })

  p.catch(err => console.log(`ERROR - fetchListenKey: ${err}`))
  return p
}

export default fetchAccountWebsocketData
like image 645
Ben Avatar asked Mar 04 '18 23:03

Ben


People also ask

What is a user data stream?

Streaming data includes a wide variety of data such as log files generated by customers using your mobile or web applications, ecommerce purchases, in-game player activity, information from social networks, financial trading floors, or geospatial services, and telemetry from connected devices or instrumentation in data ...

What is listenKey in Binance?

General WSS information The base API endpoint is: https://api.binance.com. A User Data Stream listenKey is valid for 60 minutes after creation. Doing a PUT on an active listenKey will extend its validity for 60 minutes. Doing a DELETE on an active listenKey will close the stream and invalidate the listenKey .

Does Binance have a REST API?

Data can be pulled from Binance and interact with external applications using two interfaces: the RESTful API that sends and receives data via HTTP queries and the WebSocket for streaming account updates and market data.


1 Answers

Was stuggling too .... for hours !!!

https://www.reddit.com/r/BinanceExchange/comments/a902cq/user_data_streams_has_anyone_used_it_successfully/

The binance user data stream doesn't return anything when you connect to it, only when something changes in your account. Try running your code, then go to binance and place an order in the book, you should see some data show up*

like image 59
Nikita Yo LAHOLA Avatar answered Sep 19 '22 17:09

Nikita Yo LAHOLA