Reputation: 367
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
Reputation: 1
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
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
{
"code": "200000",
"data": {
"token": "2neAiuYvAU61ZD...",
"instanceServers": [
{
"endpoint": "wss://ws-api.kucoin.com/endpoint",
"encrypt": true,
"protocol": "websocket",
"pingInterval": 18000,
"pingTimeout": 10000
}
]
}
}
Connect to the Websocket
With the data of the repsonse above:
websocket: endpoint + "?token=" + token
Example: wss://ws-api.kucoin.com/endpoint?token=2neAiu....
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",
...
},
...
{
"type": "subscribe", //subscribe or unsubscribe
"topic": "/market/ticker:BTC-USDT,BTC-USDC"
}
Upvotes: 9
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