Sample Binance API queries

Getting All Isolated Margin Symbols in PHP:

$api_key = "*****";
$secret = "*****";

$opt = [
    "http" => [
        "method" => "GET",
        "header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\nX-MBX-APIKEY: {$api_key}\r\n"
    ]
];
$context = stream_context_create($opt);
$params['timestamp'] = number_format(microtime(true)*1000,0,'.','');
$query = http_build_query($params, '', '&');
$signature = hash_hmac('sha256', $query, $secret);
$endpoint = "https://api.binance.com/sapi/v1/margin/isolated/allPairs?{$query}&signature={$signature}";

$res = file_get_contents($endpoint, false, $context);
echo $res;

the response is:

[
    {
        "symbol": "ADABTC",
        "base": "ADA",
        "quote": "BTC",
        "isMarginTrade": true,
        "isBuyAllowed": true,
        "isSellAllowed": true
    },
    {
        "symbol": "ADABUSD",
        "base": "ADA",
        "quote": "BUSD",
        "isMarginTrade": true,
        "isBuyAllowed": true,
        "isSellAllowed": true
    },
    ...
]

Getting real-time trades in JavaScript:

const WebSocket = require('ws');

const ws = new WebSocket("wss://stream.binance.com:9443/stream");

ws.on('open', async () =>
{
    const msg =
    {
        method: "SUBSCRIBE",
        params: ["xrpusdt@trade", "ltcusdt@trade"],
        id: 1
    };
    
    ws.send(JSON.stringify(msg));
});

ws.on('message', async message =>
{
    console.log(message);
});

or alternatively

const WebSocket = require('ws');

const ws = new WebSocket("wss://stream.binance.com:9443/stream?streams=btcusdt@trade/ethusdt@trade");

ws.on('message', async message =>
{
    console.log(message);
});

the response is:

{"result":null,"id":1}
{"stream":"xrpusdt@trade","data":{"e":"trade","E":1602256866029,"s":"XRPUSDT","t":73837901,"p":"0.25108000","q":"7383.00000000","b":926377010,"a":926377004,"T":1602256866027,"m":false,"M":true}}
...

Exchange information query in a browser: https://api.binance.com/api/v3/exchangeInfo

{
    "timezone": "UTC",
    "serverTime": 1602329180305,
    "rateLimits": [
        {
            "rateLimitType": "REQUEST_WEIGHT",
            "interval": "MINUTE",
            "intervalNum": 1,
            "limit": 1200
        },
        {
            "rateLimitType": "ORDERS",
            "interval": "SECOND",
            "intervalNum": 10,
            "limit": 100
        },
        {
            "rateLimitType": "ORDERS",
            "interval": "DAY",
            "intervalNum": 1,
            "limit": 200000
        }
    ],
    "exchangeFilters": [],
    "symbols": [
        {
            "symbol": "ETHBTC",
            "status": "TRADING",
            "baseAsset": "ETH",
            "baseAssetPrecision": 8,
            "quoteAsset": "BTC",
            "quotePrecision": 8,
            "quoteAssetPrecision": 8,
            "baseCommissionPrecision": 8,
            "quoteCommissionPrecision": 8,
            "orderTypes": [ "LIMIT", "LIMIT_MAKER", "MARKET", "STOP_LOSS_LIMIT", "TAKE_PROFIT_LIMIT" ],
            "icebergAllowed": true,
            "ocoAllowed": true,
            "quoteOrderQtyMarketAllowed": true,
            "isSpotTradingAllowed": true,
            "isMarginTradingAllowed": true,
            "filters": [
                {
                    "filterType": "PRICE_FILTER",
                    "minPrice": "0.00000100",
                    "maxPrice": "100000.00000000",
                    "tickSize": "0.00000100"
                },
                {
                    "filterType": "PERCENT_PRICE",
                    "multiplierUp": "5",
                    "multiplierDown": "0.2",
                    "avgPriceMins": 5
                },
                {
                    "filterType": "LOT_SIZE",
                    "minQty": "0.00100000",
                    "maxQty": "100000.00000000",
                    "stepSize": "0.00100000"
                },
                {
                    "filterType": "MIN_NOTIONAL",
                    "minNotional": "0.00010000",
                    "applyToMarket": true,
                    "avgPriceMins": 5
                },
                {
                    "filterType": "ICEBERG_PARTS",
                    "limit": 10
                },
                {
                    "filterType": "MARKET_LOT_SIZE",
                    "minQty": "0.00000000",
                    "maxQty": "7479.60184990",
                    "stepSize": "0.00000000"
                },
                {
                    "filterType": "MAX_NUM_ORDERS",
                    "maxNumOrders": 200
                },
                {
                    "filterType": "MAX_NUM_ALGO_ORDERS",
                    "maxNumAlgoOrders": 5
                }
            ],
            "permissions": [ "SPOT", "MARGIN" ]
        },
        ...

6/18/2021 I got a strange notification:

Links:

3 Responses to Sample Binance API queries

  1. dmitriano says:

    After testing, if I first connect to websocket URL wss://stream.binance.com:9443/stream, then send subscription messages. I found that I can’t subscribe too many streams as you experimented

    11:48
    Dear user, let me feedback your findings to our relevant team to see any improvement opportunities. By alternative, could you connect to websocket streams by combinations like wss://stream.binance.com:443/stream?streams=/

    11:51
    Through this way, I could successfully subscribe to roughly 900 streams

    ***************

    By the way, I got a response from our relevant team. Kindly note that you could append streams subscriptions after another message. Let’s say you could firstly book 10 stream by sending Json message, then send another 10 streams by another message

    To be safe, we suggest you could shorten the length of Json message. If you encounter the same errors, you may reduce the length of messages further

    You could subscribe more than 500, but you may have to send more than one subscription message by json format

    I send 500 two times, one by one, right?

    12:25
    but not 3 times, right?

    Yes

Leave a Reply to dmitriano Cancel reply

Your email address will not be published. Required fields are marked *