Handling messages from Binance Websocket streams

User Data Stream

To subscribe I query listen key:

POST /api/v3/userDataStream

connect to websocket wss://stream.binance.com:9443/stream and send the listen key with the following message :

{
    "id": 1,
    "method": "SUBSCRIBE",
    "params": [
        "<my listen key>"
    ]
}

When Binance accepts the subscription it sends:

{
    "id": 1,
    "result": null
}

Then when I create an order I receive:

{
    "data": {
        "C": "",
        "E": 1657908946556,
        "F": "0.00000000",
        "I": 24679170241,
        "L": "0.00000000",
        "M": false,
        "N": null,
        "O": 1657908946556,
        "P": "0.00000000",
        "Q": "0.00000000",
        "S": "BUY",
        "T": 1657908946556,
        "X": "NEW",
        "Y": "0.00000000",
        "Z": "0.00000000",
        "c": "cd1e7249-c3d9-4e9e-af4c-19be47f674a1",
        "e": "executionReport",
        "f": "GTC",
        "g": -1,
        "i": 11619060599,
        "l": "0.00000000",
        "m": false,
        "n": "0",
        "o": "LIMIT",
        "p": "20521.34000000",
        "q": "0.00060000",
        "r": "NONE",
        "s": "BTCUSDT",
        "t": -1,
        "w": true,
        "x": "NEW",
        "z": "0.00000000"
    },
    "stream": "<my listen key>"
}

and

{
    "data": {
        "B": [
            {
                "a": "BTC",
                "f": "0.00046913",
                "l": "0.00000000"
            },
            {
                "a": "BNB",
                "f": "0.04822143",
                "l": "0.00000000"
            },
            {
                "a": "USDT",
                "f": "89.01995974",
                "l": "703.69807400"
            }
        ],
        "E": 1657908946556,
        "e": "outboundAccountPosition",
        "u": 1657908946556
    },
    "stream": "<my listen key>"
}

Market Stream

To subscribe I send:

{
    "id": 1,
    "method": "SUBSCRIBE",
    "params": [
        "btcusdt@aggTrade",
        "xrpusdt@aggTrade",
        "1inchbtc@aggTrade",
        ...
    ]
}

And receive trades like this:

{
    "data": {
        "E": 1657910133927,
        "M": true,
        "T": 1657910133927,
        "a": 1257346680,
        "e": "aggTrade",
        "f": 1471456796,
        "l": 1471456796,
        "m": true,
        "p": "20838.64000000",
        "q": "0.00200000",
        "s": "BTCUSDT"
    },
    "stream": "btcusdt@aggTrade"
}

Leave a Reply

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