跳到主要内容

Subscribe / Unsubscribe

Type: NONE

WS {"type":"subscribe","channel":"<channel>"}
WS {"type":"unsubscribe","channel":"<channel>"}

Status: Implemented.

Open one socket to wss://api-avax.<sparky-domain>/ws and send as many subscribe messages as you need; there is no per-connection stream limit enforced today.

Request

{ "type": "subscribe", "channel": "trades:BTCUSDT" }
{ "type": "subscribe", "channel": "orderbook:BTCUSDT" }
{ "type": "subscribe", "channel": "ticker:BTCUSDT" }
{ "type": "subscribe", "channel": "kline:BTCUSDT:1m" }

Response

{ "type": "subscribed", "channel": "trades:BTCUSDT" }

Unsubscribe:

{ "type": "unsubscribe", "channel": "trades:BTCUSDT" }
{ "type": "unsubscribed", "channel": "trades:BTCUSDT" }

Errors

codeCause
INVALID_CHANNELChannel prefix / name not in the known list (unknown channels are rejected, not silently accepted).
AUTH_REQUIREDPrivate channel requested before a successful auth.
FORBIDDENuser_*:<address> / private:<address> channel whose address is not the authenticated user.

Keepalive

{ "type": "ping" }{ "type": "pong" }

Snapshot fetches triggered by a subscribe run asynchronously, so a burst of subscribes does not block the connection.

Example (JavaScript)

const ws = new WebSocket('wss://api-avax.<sparky-domain>/ws');
ws.onopen = () => {
ws.send(JSON.stringify({ type: 'subscribe', channel: 'trades:BTCUSDT' }));
ws.send(JSON.stringify({ type: 'subscribe', channel: 'orderbook:BTCUSDT' }));
};
ws.onmessage = (e) => {
const msg = JSON.parse(e.data);
if (msg.type === 'trade') console.log('fill', msg.price, msg.amount, msg.side);
if (msg.type === 'orderbook') console.log('book', msg.bids[0], msg.asks[0]);
};