Kraken Spot JavaScript SDK example: wsAPI.ts

Kraken Spot websocket API JavaScript example for the Siebly Kraken SDK, covering exchange REST API and WebSocket integration, setup, and production SDK docs.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/Kraken/Spot/WebSockets/wsAPI.ts

What this example covers

  • Kraken WebSocket stream JavaScript example.
  • Uses the Siebly Kraken SDK package @siebly/kraken-api instead of hand-written WebSocket plumbing.
  • Source path: Kraken/Spot/WebSockets/wsAPI.ts.
  • Example category: Spot.
  • Imports SDK symbols including DefaultLogger, WebsocketAPIClient.
  • Calls SDK methods such as submitSpotOrder(), amendSpotOrder(), cancelSpotOrder(), cancelAllSpotOrders(), cancelAllSpotOrdersAfter(), batchSubmitSpotOrders(), batchCancelSpotOrders().

How to use this example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • For WebSocket examples, keep reconnect, resubscribe, heartbeat, and event-handler behavior explicit in your service.
  • Open the repository source when you need the latest committed version: GitHub source file.
// eslint-disable-next-line @typescript-eslint/no-unused-varsimport {  DefaultLogger,  LogParams,  WebsocketAPIClient,} from '@siebly/kraken-api'; const customLogger: DefaultLogger = {  // eslint-disable-next-line @typescript-eslint/no-unused-vars  trace: (...params: LogParams): void => {    // console.log('trace', ...params);  },  info: (...params: LogParams): void => {    console.log('info', ...params);  },  error: (...params: LogParams): void => {    console.error('error', ...params);  },}; async function start() {  const account = {    key: process.env.API_SPOT_KEY || 'keyHere',    secret: process.env.API_SPOT_SECRET || 'secretHere',  };   /**   * The WebsocketClient is the core class to manage WebSocket subscriptions. Give it the topics you want to subscribe to, and it will handle the rest:   * - Connection management (connect, disconnect, reconnect)   * - Authentication for private topics   * - Subscription management (subscribe, unsubscribe, resubscribe on reconnect)   * - Message handling (dispatch messages to appropriate handlers)   *   * All you need to do is provide the topics you want to subscribe to when calling `subscribe()`, and the client will take care of the rest.   *   * Here we create a WebsocketClient instance with API key/secret for private topic subscriptions.   *   * In terms of product groups such as Spot, Derivatives, etc., the WebsocketClient understand the product group from the WsKey you provide when subscribing. For example, using `WS_KEY_MAP.spotPrivateV2` indicates that the subscription is for Spot private topics, as shown below.   *   * Refer to WS_KEY_MAP in the source code for all available WsKey options.   */  const client = new WebsocketAPIClient(    {      apiKey: account.key,      apiSecret: account.secret,    },    customLogger,  );   /**   * The below examples demonstrate how you can subscribe to private topics.   *   * Note: while the documentation specifies "token" as a required parameter, the SDK will automatically:   * - fetch the token using your API key/secret,   * - manage token caching/refreshing,   * - include the token in the request for you.   *   * So you do NOT need to manually fetch or provide the token when subscribing to private topics.   *   * Do note that all of these include the "spotPrivateV2" WsKey reference. This tells the WebsocketClient to use the private "wss://ws-auth.kraken.com/v2" endpoint for these private subscription requests.   */   try {    const addOrderResponse = await client.submitSpotOrder({      order_type: 'limit',      side: 'buy',      limit_price: 26500.4,      order_userref: 100054,      order_qty: 1.2,      symbol: 'BTC/USD',    });    console.log('addOrderResponse: ', addOrderResponse);     const addOrderConditionalResponse = await client.submitSpotOrder({      order_type: 'limit',      side: 'buy',      order_qty: 1.2,      symbol: 'BTC/USD',      limit_price: 28440,      conditional: {        order_type: 'stop-loss-limit',        trigger_price: 28410,        limit_price: 28400,      },    });    console.log('addOrderConditionalResponse: ', addOrderConditionalResponse);     const addOrderTriggersResponse = await client.submitSpotOrder({      order_type: 'stop-loss',      side: 'sell',      order_qty: 100,      symbol: 'MATIC/USD',      triggers: {        reference: 'last',        price: 10.0,        price_type: 'pct',      },    });    console.log('addOrderTriggersResponse: ', addOrderTriggersResponse);     const amendOrderResponse = await client.amendSpotOrder({      cl_ord_id: '2c6be801-1f53-4f79-a0bb-4ea1c95dfae9',      limit_price: 10000,      order_qty: 1.2,    });    console.log('amendOrderResponse: ', amendOrderResponse);     const amendOrderPostOnlyResponse = await client.amendSpotOrder({      order_id: 'OAIYAU-LGI3M-PFM5VW',      order_qty: 1.2,      limit_price: 1100.3,      deadline: '2025-11-19T09:53:59.050Z',      post_only: true,    });    console.log('amendOrderPostOnlyResponse: ', amendOrderPostOnlyResponse);     const cancelOrderResponse = await client.cancelSpotOrder({      order_id: ['OM5CRX-N2HAL-GFGWE9', 'OLUMT4-UTEGU-ZYM7E9'],    });    console.log('cancelOrderResponse: ', cancelOrderResponse);     const cancelAllResponse = await client.cancelAllSpotOrders();    console.log('cancelAllResponse: ', cancelAllResponse);     const cancelAllOrdersAfterResponse = await client.cancelAllSpotOrdersAfter({      timeout: 100,    });    console.log('cancelAllOrdersAfterResponse: ', cancelAllOrdersAfterResponse);     const batchAddResponse = await client.batchSubmitSpotOrders({      deadline: '2025-11-19T09:53:59.050Z',      orders: [        {          limit_price: 1010.1,          order_qty: 1.2,          order_type: 'limit',          order_userref: 1,          side: 'buy',        },        {          limit_price: 1100.3,          order_qty: 1.2,          order_type: 'limit',          order_userref: 2,          side: 'sell',          stp_type: 'cancel_both',        },      ],      symbol: 'BTC/USD',      validate: false,    });    console.log('batchAddResponse: ', batchAddResponse);     const batchCancelResponse = await client.batchCancelSpotOrders({      orders: ['OM5CRX-N2HAL-GFGWE9', 'OLUMT4-UTEGU-ZYM7E9'],    });    console.log('batchCancelResponse: ', batchCancelResponse);  } catch (e) {    console.error('Req error: ', e);  }} start(); 

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.