Connecting to API

https://learninghub.kx.com/forums/topic/connecting-to-api

Hi,

I have few questions when it comes to connection to an API. is it possible to connect to public api for market data (binance or so on) using kdb q? how can i do it

can I acheive this using .Q.hg or other methodslike web socket

https://developers.binance.com/docs/binance-spot-api-docs/faqs

this is the market data how can i connect to it use those data. Can i load the data save it to disk as table or other.

your support is highly appreciated thank you.


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"

You can see an example of streaming data from www.coinapi.io over Websockets here:


  • https://github.com/rianoc/qCoinAPI


For REST APIs the best library to use is kurl:


  • https://code.kx.com/insights/1.10/core/kurl/quickstart.html