Skip to content

Private account updates ​

Use WebsocketClient to observe account changes. The Spot v2 executions channel delivers order status and execution updates. Other private channels cover account balances. Futures account updates use the SDK’s Derivatives WebSocket connection.

Observe Spot executions ​

Use the Spot credentials setup, with WebSocket access enabled and the order/trade query permissions required by the channel. This example only subscribes to account updates; it does not place orders.

Save this as private-stream.ts and run node --env-file=.env private-stream.ts with Node.js 24:

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

function requiredEnvVar(name: string) {
  const value = process.env[name];
  if (!value) {
    throw new Error(`Set ${name}.`);
  }
  return value;
}

const ws = new WebsocketClient({
  apiKey: requiredEnvVar('KRAKEN_API_KEY'),
  apiSecret: requiredEnvVar('KRAKEN_API_SECRET'),
});

ws.on('message', (message) => console.log(message));
ws.on('response', (response) =>
  console.log('Subscription response:', response),
);
ws.on('exception', (error) => console.error('WebSocket exception:', error));
ws.on('reconnecting', (event) => console.log('Reconnecting:', event));
ws.on('reconnected', (event) => console.log('Reconnected:', event));

// Authentication and the Spot WebSocket token are managed by the SDK.
ws.subscribe('executions', WS_KEY_MAP.spotPrivateV2);

const timer = setTimeout(stop, 30000);
function stop() {
  clearTimeout(timer);
  ws.closeAll();
}
process.once('SIGINT', stop);
process.once('SIGTERM', stop);

The SDK obtains and manages the Spot WebSocket authentication token. Do not fetch or inject a token manually for this subscription.

Interpret updates ​

The channel sends snapshots and subsequent updates. Apply these events to local order and fill state as they arrive. Read each event's execution type and order status to follow an order from acceptance through fills or cancellation. Subscription and authentication replies describe the connection, not an order's outcome.

Use the balances channel when your application also needs balance changes. Load any starting state not covered by the snapshots, then let private events keep it up to date. Do not query REST after every order. See account state.

The example logs all messages so you can inspect them before adding handlers. Follow the executions channel schema when adding typed handlers.

Recover after a gap ​

The client reconnects and resubscribes. After a disconnect, process the new snapshots and updates and use REST to repair any missing order, execution or balance state. Account for events received during recovery before relying on that state for new decisions. A reconnect event alone does not prove your application has accounted for every fill.

Production usage · WebsocketClient reference

Built by Siebly. Open source SDKs for exchange APIs.