John R
John R

Reputation: 367

kucoin websocket API, how to "subscribe" to their public channel, they say no authorization required, but they ask for a token :(

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 explanations :(

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 explanation on how to get that token :(

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

Upvotes: 5

Views: 4932

Answers (3)

i was able to connect with this in nestjs in onModuleInit function

const tokenObj = await axios.post('https://api.kucoin.com/api/v1/bullet-public')

const ws = new WebSocket(`wss://ws-api-spot.kucoin.com/?token=${tokenObj.data.data.token}`);

ws.on('open', () => {
  console.log('gate is opened')
})

ws.on("message", (data) => {
    console.log(data.toString());
}); 

Upvotes: 0

John
John

Reputation: 186

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"
}

Upvotes: 9

edarblanco
edarblanco

Reputation: 11

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

Upvotes: 1

Related Questions