Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sieblyio/kraken-api/llms.txt

Use this file to discover all available pages before exploring further.

Public streams require no API keys. The WebsocketClient connects to the correct endpoint automatically based on the WsKey you pass to subscribe(). Every subscription is persistent — if the connection drops, the SDK reconnects and resubscribes without any code on your side.

Setup

import { WebsocketClient, WS_KEY_MAP } from '@siebly/kraken-api';

// No API keys needed for public market data
const ws = new WebsocketClient();

ws.on('open', (data) => console.log('connected:', data.wsKey));
ws.on('message', (data) => console.log('data:', JSON.stringify(data)));
ws.on('response', (data) => console.log('server reply:', JSON.stringify(data)));
ws.on('reconnected', (data) => console.log('reconnected:', data.wsKey));
ws.on('exception', (data) => console.error('exception:', data));

Spot public topics

Spot public streams connect via WS_KEY_MAP.spotPublicV2 (wss://ws.kraken.com/v2). The available topics are ticker, trade, book, ohlc, and instrument.
Stream best-bid-offer price updates for one or more symbols. By default, snapshots are sent on connect and updates fire on every trade or BBO change.
ws.subscribe(
  {
    topic: 'ticker',
    payload: {
      symbol: ['BTC/USD', 'ETH/USD'],
      // event_trigger: 'bbo',    // fire only on best-bid-offer changes
      // event_trigger: 'trades', // fire only on trades
      // snapshot: true,          // default: true
    },
  },
  WS_KEY_MAP.spotPublicV2,
);

Batch subscription

Instead of calling subscribe() multiple times, pass an array of topic requests in a single call. All topics must share the same WsKey.
import { WebsocketClient, WS_KEY_MAP, WSTopicRequest } from '@siebly/kraken-api';

const ws = new WebsocketClient();

const tickerRequest: WSTopicRequest = {
  topic: 'ticker',
  payload: { symbol: ['BTC/USD', 'ETH/USD'] },
};

const tradeRequest: WSTopicRequest = {
  topic: 'trade',
  payload: { symbol: ['BTC/USD'] },
};

const instrumentRequest: WSTopicRequest = {
  topic: 'instrument',
  payload: {
    symbol: ['BTC/USD'],
    include_tokenized_assets: true,
  },
};

// Subscribe to all three topics in one call
ws.subscribe(
  [tickerRequest, tradeRequest, instrumentRequest],
  WS_KEY_MAP.spotPublicV2,
);

Complete Spot public example

import { WebsocketClient, WS_KEY_MAP } from '@siebly/kraken-api';

const ws = new WebsocketClient();

ws.on('open', (data) => {
  console.log('connected:', data.wsKey);
});

ws.on('message', (data) => {
  // All streaming topic updates arrive here
  console.log('data received:', JSON.stringify(data));
});

ws.on('response', (data) => {
  // Subscribe/unsubscribe acknowledgements from the server
  console.log('server reply:', JSON.stringify(data));
});

ws.on('reconnecting', (data) => {
  console.warn('reconnecting:', data.wsKey);
});

ws.on('reconnected', (data) => {
  console.log('reconnected:', data.wsKey);
});

ws.on('exception', (data) => {
  console.error('exception:', data);
});

// Subscribe to ticker and order book for BTC/USD
ws.subscribe(
  [
    {
      topic: 'ticker',
      payload: { symbol: ['BTC/USD'] },
    },
    {
      topic: 'book',
      payload: { symbol: ['BTC/USD'], depth: 10 },
    },
  ],
  WS_KEY_MAP.spotPublicV2,
);

Futures public topics

Futures (derivatives) public streams use WS_KEY_MAP.derivativesPublicV1 and connect to wss://futures.kraken.com/ws/v1. The available topics are ticker, ticker_lite, book, and trade. Futures payloads use product_ids instead of symbol.
Full ticker data for perpetual futures contracts.
ws.subscribe(
  {
    topic: 'ticker',
    payload: {
      product_ids: ['PI_XBTUSD', 'PI_ETHUSD'],
    },
  },
  WS_KEY_MAP.derivativesPublicV1,
);

Complete Futures public example

import {
  WebsocketClient,
  WS_KEY_MAP,
  WSTopicRequest,
} from '@siebly/kraken-api';
import { WSDerivativesTopic } from '@siebly/kraken-api';

const ws = new WebsocketClient();

ws.on('open', (data) => {
  console.log('connected:', data.wsKey);
});

ws.on('message', (data) => {
  console.log('data received:', JSON.stringify(data));
});

ws.on('response', (data) => {
  console.log('server reply:', JSON.stringify(data));
});

ws.on('reconnected', (data) => {
  console.log('reconnected:', data.wsKey);
});

ws.on('exception', (data) => {
  console.error('exception:', data);
});

// Subscribe to futures trades and order book
const tradeRequest: WSTopicRequest<WSDerivativesTopic> = {
  topic: 'trade',
  payload: { product_ids: ['PI_XBTUSD', 'PI_ETHUSD'] },
};

const bookRequest: WSTopicRequest<WSDerivativesTopic> = {
  topic: 'book',
  payload: { product_ids: ['PI_XBTUSD'] },
};

ws.subscribe(tradeRequest, WS_KEY_MAP.derivativesPublicV1);
ws.subscribe(bookRequest, WS_KEY_MAP.derivativesPublicV1);

Testnet / demo environment

The Kraken Derivatives environment supports a testnet (demo) mode. Set testnet: true in the constructor and the SDK routes connections to wss://demo-futures.kraken.com/ws/v1 automatically. Spot does not currently have a separate testnet WebSocket endpoint.
import { WebsocketClient, WS_KEY_MAP } from '@siebly/kraken-api';

const ws = new WebsocketClient({
  testnet: true, // Routes derivatives connections to demo-futures.kraken.com
});

ws.subscribe(
  {
    topic: 'ticker',
    payload: { product_ids: ['PI_XBTUSD'] },
  },
  WS_KEY_MAP.derivativesPublicV1,
);
The testnet flag only affects derivativesPublicV1 and derivativesPrivateV1 connections. It has no effect on Spot WebSocket connections.

Unsubscribing

Call ws.unsubscribe() with the same request shape used when subscribing. The topic is removed from the internal cache so it will not be resubscribed after a reconnect.
// Unsubscribe from a single topic
ws.unsubscribe(
  {
    topic: 'ticker',
    payload: { symbol: ['BTC/USD'] },
  },
  WS_KEY_MAP.spotPublicV2,
);

// Unsubscribe from multiple topics at once
ws.unsubscribe(
  [
    { topic: 'trade', payload: { symbol: ['BTC/USD'] } },
    { topic: 'book', payload: { symbol: ['BTC/USD'], depth: 10 } },
  ],
  WS_KEY_MAP.spotPublicV2,
);

Topic reference

MarketTopicWsKeyNotes
SpottickerspotPublicV2Best-bid-offer prices
SpottradespotPublicV2Individual trade events
SpotbookspotPublicV2Level 2 order book; configurable depth
SpotohlcspotPublicV2Candlestick data; configurable interval
SpotinstrumentspotPublicV2Instrument metadata
FuturestickerderivativesPublicV1Full ticker
Futuresticker_litederivativesPublicV1Lightweight ticker
FuturesbookderivativesPublicV1Level 2 order book
FuturestradederivativesPublicV1Trade events

Build docs developers (and LLMs) love