Kraken Spot Trading via WebsocketAPIClient (WS API)
Use WebsocketAPIClient to place and manage Spot orders over a persistent WebSocket connection using promise-based async/await — lower latency than REST.
Use this file to discover all available pages before exploring further.
WebsocketAPIClient is a thin, ergonomic wrapper around WebsocketClient that exposes Kraken’s Spot WebSocket API as a set of awaitable async methods. Instead of opening a new HTTP connection for every order operation, WebsocketAPIClient maintains a single persistent authenticated WebSocket connection and routes each command through it — returning a resolved promise when the server replies.The interface deliberately feels like a REST client: call a method, await the result, inspect the response. The underlying transport is WebSocket, which means significantly lower latency and reduced overhead compared with REST, without any of the raw WebSocket plumbing in your code.
Instantiate WebsocketAPIClient with your Spot API credentials. The underlying WebsocketClient connection is managed internally and made accessible via getWSClient().
import { WebsocketAPIClient } from '@siebly/kraken-api';const wsApi = new WebsocketAPIClient({ apiKey: process.env.API_SPOT_KEY!, apiSecret: process.env.API_SPOT_SECRET!,});
By default (attachEventListeners: true), WebsocketAPIClient attaches console-logging event handlers for open, reconnecting, reconnected, authenticated, and exception. These are useful during development but you may want to replace them in production with your own handlers.
const wsApi = new WebsocketAPIClient( { apiKey: process.env.API_SPOT_KEY!, apiSecret: process.env.API_SPOT_SECRET!, attachEventListeners: false, // Disable default console logging },);// Now attach your own handlers via the underlying WebsocketClientwsApi.getWSClient().on('open', (data) => { console.log('ws api connected:', data.wsKey);});wsApi.getWSClient().on('authenticated', (data) => { console.log('ws api authenticated:', data.wsKey);});wsApi.getWSClient().on('reconnecting', ({ wsKey }) => { console.warn('ws api reconnecting:', wsKey); // Pause order activity during reconnect});wsApi.getWSClient().on('reconnected', (data) => { console.log('ws api reconnected:', data.wsKey); // Reconcile order state via REST API});wsApi.getWSClient().on('exception', (data) => { console.error('ws api exception:', JSON.stringify(data));});
getWSClient() returns the underlying WebsocketClient instance. Use it to attach event listeners, or to call subscribe() if you want to combine WS API trading with topic subscriptions on the same client instance.
The SDK connects and authenticates automatically on the first command. If you want to eliminate first-request latency, call connectWSAPI() before issuing orders:
import { WS_KEY_MAP } from '@siebly/kraken-api';// Pre-warm the connection and auth before your first orderawait wsApi.connectWSAPI(WS_KEY_MAP.spotPrivateV2);// Now the first order goes out on an already-authenticated connectionconst result = await wsApi.submitSpotOrder({ ... });
Submit a new Spot order. Supports limit, market, iceberg, stop-loss, stop-loss-limit, take-profit, take-profit-limit, trailing-stop, trailing-stop-limit, and settle-position order types, as well as conditional (bracket) orders and trigger-based orders.
Amend an existing open order by order_id or cl_ord_id. Only the fields you provide are changed; omitted fields retain their original values.
// Amend by client order IDconst response = await wsApi.amendSpotOrder({ cl_ord_id: '2c6be801-1f53-4f79-a0bb-4ea1c95dfae9', limit_price: 10000, order_qty: 1.2,});// Amend by exchange order ID with post-only flag and deadlineconst response2 = await wsApi.amendSpotOrder({ order_id: 'OAIYAU-LGI3M-PFM5VW', order_qty: 1.2, limit_price: 1100.3, deadline: '2025-11-19T09:53:59.050Z', post_only: true,});
Edit an existing order using the legacy edit_order method. This replaces the entire order. For better performance and more granular control, prefer amendSpotOrder().
Cancel one or more orders by passing their IDs in an array. Supports exchange order IDs (order_id), client order IDs (cl_ord_id), or user references (order_userref).
Set a timer in seconds. If the timer expires before it is refreshed or cancelled, all open orders are automatically cancelled. Call with timeout: 0 to disarm the timer.
// Arm the dead man's switch: cancel all orders in 100 seconds if not refreshedconst response = await wsApi.cancelAllSpotOrdersAfter({ timeout: 100 });// Disarm: set timeout to 0await wsApi.cancelAllSpotOrdersAfter({ timeout: 0 });
The dead man’s switch timer is reset each time you call cancelAllSpotOrdersAfter(). If your system crashes without disarming the timer, all open orders will be cancelled automatically. This is intentional behaviour — use it as a safety net for automated trading systems.
Use validate: true in submitSpotOrder() or batchSubmitSpotOrders() to verify your request shape against the exchange without placing a live order. Switch validate to false — or omit it — when you are ready to go live.
Combine WebsocketAPIClient with a WebsocketClient subscription on executions (via getWSClient().subscribe(...)) to both place orders and receive fill confirmations on the same underlying connection.