HTX Spot JavaScript SDK example: wsAPI.RAW.ts

HTX Spot websocket API RAW JavaScript example for the Siebly HTX 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/HTX/Spot/WebSockets/wsAPI.RAW.ts

What this example covers

  • HTX WebSocket stream JavaScript example.
  • Uses the Siebly HTX SDK package @siebly/htx-api instead of hand-written WebSocket plumbing.
  • Source path: HTX/Spot/WebSockets/wsAPI.RAW.ts.
  • Example category: Spot.
  • Imports SDK symbols including DefaultLogger, WebsocketClient, WS_KEY_MAP.
  • Calls SDK methods such as connectWSAPI(), sendWSAPIRequest(), generateNewOrderID().

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.
import {  DefaultLogger,  LogParams,  WebsocketClient,  WS_KEY_MAP,} from '@siebly/htx-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',  };   /**   * WebsocketClient can send low-level WS API request/response commands with   * sendWSAPIRequest(). Use WebsocketAPIClient if you prefer typed wrapper   * methods for the same trading commands.   */  const client = new WebsocketClient(    {      apiKey: account.key,      apiSecret: account.secret,    },    customLogger,  );   client    .on('open', (data) => console.log('open:', data.wsKey))    .on('authenticated', (data) => console.log('authenticated:', data.wsKey))    .on('response', (data) => console.info('response:', JSON.stringify(data)))    .on('exception', (data) => console.error('exception:', data));   /**   * Optional: connect/authenticate before the first request.   */  // await client.connectWSAPI(WS_KEY_MAP.spotTrade);   const runLiveTradingExamples = false;   if (!runLiveTradingExamples) {    console.log('Set runLiveTradingExamples=true after reviewing the params.');    return;  }   const submitOrderResponse = await client.sendWSAPIRequest(    WS_KEY_MAP.spotTrade,    'create-order',    {      'account-id': 123456,      symbol: 'btcusdt',      type: 'buy-limit',      amount: '0.001',      price: '20000',      source: 'spot-api',      'client-order-id': client.generateNewOrderID(),    },  );  console.log('create-order:', submitOrderResponse);   const cancelResponse = await client.sendWSAPIRequest(    WS_KEY_MAP.spotTrade,    'cancel',    {      'order-ids': ['123456789'],    },  );  console.log('cancel:', cancelResponse);} start(); 

Subscribe on Substack

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