KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Connecting to API Reply To: Connecting to API

  • jlucid

    Member
    August 14, 2024 at 12:11 pm

    You can use this library by Jonathon McMurrray.
    Just load the ws-client_0.2.2.q file into a q session.
    This example below shows how to request Top of book messages for BTCUSDT

    https://github.com/jonathonmcmurray/ws.q

    Note, that because you need a secure connection (for wss), you need to have set the following environmental variables first. Including creation of your SSL certs.

    $export SSL_KEY_FILE=/path/to/server-key.pem

    $export SSL_CERT_FILE=/path/to/server-crt.pem

    $export SSL_VERIFY_SERVER=NO

    $rlwrap q ws-client_0.2.2.q -E 2

    q)func:{[x] show x}

    q).ws.open[$wss://stream.binance.com:443/ws/btcusdt@bookTicker";func]
    {\”u\”:50341952152,\”s\”:\”BTCUSDT\”,\”b\”:\”61344.99000000\”,\”B\”:\”7.60825000\”,\”a\”:\”61345.00000000\”,\”A\”:\”0.04012000\”}”

    You can parse the messages using .j.k

    q).j.k “{\”u\”:50341952152,\”s\”:\”BTCUSDT\”,\”b\”:\”61344.99000000\”,\”B\”:\”7.60825000\”,\”a\”:\”61345.00000000\”,\”A\”:\”0.04012000\”}”

    u| 5.034195e+10

    s| “BTCUSDT”

    b| “61344.99000000”

    B| “7.60825000”

    a| “61345.00000000”

    A| “0.04012000”