2

The question is about kucoin websocket public channel (not trades) just last trades

I just want a live feed of trades like other crypto exchanges...

but when I want to connect to "wss://ws-api-futures.kucoin.com/endpoint" I get WebSocketError: Received unexpected status code (401 Unauthorized)

the documentation https://docs.kucoin.com/futures/#create-connection lack explications :(

normally with other exchanges I can just do this in javascript

bybit_market_ws = new WebSocket("wss://stream.bybit.com/spot/quote/ws/v2");
bybit_market_ws.onmessage = event => bybit_trades(event.data);
bybit_market_ws.onopen = event => bybit_market_ws.send(JSON.stringify({"topic":"trade","params":{"symbol":"BTCUSDT","binary":false},"event":"sub"}));
function bybit_trades (jsonx) { console.log(JSON.parse(jsonx)); }

so how can I do that with kucoin websocket ?

according to the documentation i would need a "public token"...

but there is no explication on how to get that token :(

does someone knows how I would retrieve the last trades via websocket (public) channel ?

John R
  • 277
  • 3
  • 10

2 Answers2

4

Note that the following steps may be changed when the API is updated. All information can be found at https://docs.kucoin.com/#apply-connect-token

  1. Get the public token
    Send a empty http POST (GET will not work) message to https://api.kucoin.com/api/v1/bullet-public.
    Response:
{
    "code": "200000",
    "data": {
        "token": "2neAiuYvAU61ZD...",
        "instanceServers": [
            {
                "endpoint": "wss://ws-api.kucoin.com/endpoint",
                "encrypt": true,
                "protocol": "websocket",
                "pingInterval": 18000,
                "pingTimeout": 10000
            }
        ]
    }
}

  1. Connect to the Websocket
    With the data of the repsonse above:
    websocket: endpoint + "?token=" + token
    Example: wss://ws-api.kucoin.com/endpoint?token=2neAiu....

  2. Get all supported trading pairs
    send a http GET message to https://api.kucoin.com/api/v1/symbols

{
    "code": "200000",
    "data": [
        {
            "symbol": "REQ-ETH",
            "name": "REQ-ETH",
            "baseCurrency": "REQ",
            "quoteCurrency": "ETH",
            ...
        },
        {
            "symbol": "BTC-USDC",
            "name": "BTC-USDC",
            "baseCurrency": "BTC",
            "quoteCurrency": "USDC",
            ...
        },
        ...
  1. Get trading data
    When the websocket connection is established send a http POST message:
{
    "type": "subscribe", //subscribe or unsubscribe
    "topic": "/market/ticker:BTC-USDT,BTC-USDC"
}
John
  • 136
  • 5
1

maybe this answer will not please you at all, but i will try, most of the people who work from the API in KuCoin do it with python, in fact the SDK for Nodejs is out of date, your best bet is to ask in the telegram channel https://t.me/KuCoin_API, there are KuCoin engineers who always help, although most of them use python, there is also the academy channel https://t.me/kucoin_learning, where there are examples, in short I can only mention references because I was also where you are, and the best I could do was that and review the SDk code and from there intuit and create my own adjustments

PD: the datafeed.js file is your best option, check it out https://github.com/Kucoin/kucoin-futures-node-sdk/blob/master/src/lib/datafeed.js

edarblanco
  • 11
  • 4