DEV Community


Posted on

Using Binance APIs


As someone who worked with data for entire of my career, I always love to learn how to build a useful live data dashboard. One of the problems for build a live data dashboard comes with live data. Most live data are behind a hefty price wall and not really accessible to most people. However, Binance has a wonderful public Websocket API and it is really useful for anyone to learn!

In this blog, I am going to share my experience in learning websocket. This is the link to the final product.

Websocket API

The websocket API we are going to use is the miniticker, which we can receive the latest closing price. According to the document, this is the payload

    "e": "24hrMiniTicker",  // Event type
    "E": 123456789,         // Event time
    "s": "BNBBTC",          // Symbol
    "c": "0.0025",          // Close price
    "o": "0.0010",          // Open price
    "h": "0.0025",          // High price
    "l": "0.0010",          // Low price
    "v": "10000",           // Total traded base asset volume
    "q": "18"               // Total traded quote asset volume
Enter fullscreen mode Exit fullscreen mode

In order to receive the miniTicker socket stream, we would need to subscribe to it. What it means is that, we need to send below json to websocket url, say if we want to subscribe to BTCUSDT and ETHUSDT

    "method": "SUBSCRIBE",
    "id": 1
Enter fullscreen mode Exit fullscreen mode

Finally, the market data websocket url is wss:// Now, we are good to go 😎

Trying the websocket API

Most morden web browser support websocket, so we just need to open the developer console to try

const ws = new WebSocket('wss://');
ws.onmessage = function (e) { console.log( };
Enter fullscreen mode Exit fullscreen mode

we should see log messages like below

Enter fullscreen mode Exit fullscreen mode


Top comments (1)

scofieldidehen profile image
Scofield Idehen


``` to bring your code to life

print("hello world)

Enter fullscreen mode Exit fullscreen mode