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.

The @siebly/kraken-api SDK ships two distinct WebSocket interfaces that cover every real-time use case on Kraken: a general-purpose streaming client for market data and account updates, and a command-oriented client for low-latency Spot order management over a persistent connection. Both share the same underlying connection lifecycle machinery — heartbeats, reconnect, re-authentication, and resubscription are all handled automatically.

Two WebSocket interfaces

WebsocketClient

Streaming subscriptions — subscribe to public market data (tickers, trades, order books, OHLC candles, instruments) and private account streams (executions, balances, positions) across Spot and Futures markets. Use WS_KEY_MAP to route each subscription to the correct endpoint.

WebsocketAPIClient

REST-like commands over WebSocket — send Spot trading commands (add_order, amend_order, cancel_order, etc.) over a persistent authenticated connection and await the response exactly like a REST call. Currently Spot only.

WS_KEY_MAP — connection keys explained

Every subscribe() call and every WebsocketAPIClient method requires a WsKey that tells the SDK which WebSocket endpoint to use. The SDK maintains a separate physical connection for each key, so you can have public and private connections open simultaneously without interference.
import { WS_KEY_MAP } from '@siebly/kraken-api';
KeyEndpoint URLPurpose
spotPublicV2wss://ws.kraken.com/v2Spot public streams: ticker, trade, book, ohlc, instrument
spotPrivateV2wss://ws-auth.kraken.com/v2Spot private streams: executions, balances
spotL3V2wss://ws-l3.kraken.com/v2Level 3 order book (dedicated endpoint)
spotBetaPublicV2wss://beta-ws.kraken.com/v2Spot beta public — early-access features
spotBetaPrivateV2wss://beta-ws-auth.kraken.com/v2Spot beta private
derivativesPublicV1wss://futures.kraken.com/ws/v1Futures public streams: ticker, ticker_lite, book, trade
derivativesPrivateV1wss://futures.kraken.com/ws/v1Futures private streams: open_orders, fills, balances, open_positions, and more
derivativesPublicV1 and derivativesPrivateV1 share the same upstream URL, but the SDK maintains two separate connections — one unauthenticated, one authenticated — for simpler lifecycle management and to avoid cross-contamination of authentication state.

Events reference

Set up event listeners immediately after constructing the client, before making any subscribe() calls. The SDK emits these events throughout the connection lifecycle:
EventWhen it firesRecommended action
openA WebSocket connection is successfully establishedLog for visibility; safe to subscribe
messageA streaming data update arrives (topic snapshot or update)Parse data.channel to route to your handler
responseAcknowledgement for subscribe / unsubscribe / auth requestsVerify success: true; log errors
reconnectingHeartbeat timeout detected; SDK is tearing down the stale connectionPause order management; do not send new commands
reconnectedNew connection opened, re-authenticated, and all topics resubscribedQuery REST API to reconcile any state drift
closeSocket closed (could be expected or unexpected)If unexpected, reconnecting will fire shortly after
authenticatedPrivate connection auth handshake completedLog for auditing; WS API is now ready
exceptionAn error or unexpected server message was receivedLog data in full; contains error detail
import { WebsocketClient } from '@siebly/kraken-api';

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('reconnecting', (data) => {
  console.warn('reconnecting — pause order management:', data.wsKey);
});

ws.on('reconnected', (data) => {
  console.log('reconnected — reconcile state via REST:', data.wsKey);
});

ws.on('close', (data) => {
  console.log('connection closed:', data);
});

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

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

Connection lifecycle

The SDK manages the full WebSocket lifecycle without any manual intervention required. Here is the sequence of events from first connection through an automatic recovery:
1

Connect

The first subscribe() call triggers a connection to the appropriate endpoint derived from the WsKey. Public connections open immediately; private connections proceed to the auth step.
2

Authenticate

For private WsKey values (spotPrivateV2, spotL3V2, spotBetaPrivateV2, derivativesPrivateV1), the SDK automatically handles the authentication handshake:
  • Spot private: fetches a WebSocket token via the REST API and injects it into each subscription payload.
  • Derivatives private: sends a challenge event with your API key, receives the challenge string, hashes and signs it, then transmits the signed challenge alongside each subscription.
3

Subscribe

Each topic passed to subscribe() is dispatched as a correctly structured request. Topics are cached internally so they can be resubscribed automatically on reconnect.
4

Heartbeat monitoring

The SDK sends periodic ping messages and expects pong replies within the configured pongTimeout. If no reply arrives in time, the SDK flags the connection as stale.
5

Reconnect

On a detected disconnect, the SDK emits reconnecting, tears down the stale socket, waits for reconnectTimeout, then opens a fresh connection — re-authenticating and resubscribing all cached topics automatically.
6

Reconnected

Once all topics are resubscribed, the SDK emits reconnected. This is the correct time to call your REST API for any account or market state that may have changed while the connection was down.

Instantiation examples

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

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

Next steps

Public Streams

Subscribe to live Spot and Futures market data — tickers, trades, order books, OHLC candles, and instruments.

Private Streams

Stream authenticated account data — order executions, balances, open positions, and Level 3 order book.

WS API Client

Place, amend, and cancel Spot orders over a persistent WebSocket connection with promise-based async/await.

Build docs developers (and LLMs) love