HTX Spot private websocket 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.
@siebly/htx-api instead of hand-written WebSocket plumbing.HTX/Spot/WebSockets/privateWs.ts.DefaultLogger, WebsocketClient, WS_KEY_MAP.subscribe()./* eslint-disable @typescript-eslint/no-unused-vars */import { DefaultLogger, LogParams, WebsocketClient, WS_KEY_MAP,} from '@siebly/htx-api'; function isRecord(value: unknown): value is Record<string, unknown> { return typeof value === 'object' && value !== null;} function isMutedTrace(params: LogParams): boolean { const [message, details] = params; if (message === 'Received PING event') { return true; } if (message === 'onWsMessage().emit(message)') { return true; } if ( typeof message === 'string' && message.startsWith('getFinalEmittable()->pre()') ) { return true; } if ( message === 'Sending upstream ws message: ' && isRecord(details) && typeof details.wsMessage === 'string' ) { // return false; return !details.wsMessage.includes('"pong"'); } return false;} const customLogger: DefaultLogger = { trace: (...params: LogParams): void => { // if (isMutedTrace(params)) { // return; // } // 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_KEY || '', secret: process.env.API_SECRET || '', }; 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('message', (data) => console.info('message:', JSON.stringify(data))) .on('response', (data) => console.info('response:', JSON.stringify(data))) .on('reconnecting', (data) => console.log('reconnecting:', data.wsKey)) .on('reconnected', (data) => console.log('reconnected:', data.wsKey)) .on('close', (data) => console.log('close:', data.wsKey)) .on('exception', (data) => console.error('exception:', data)); /** * Spot private websocket: wss://api-aws.huobi.pro/ws/v2 by default. * * The SDK signs and sends the auth request automatically before subscribing. */ client.subscribe( [ // not specifying "mode", Only update when account balance changed; 'accounts.update', // Specify "mode" as 0, Only update when account balance changed; 'accounts.update#0', // Specify "mode" as 1, Update when either account balance changed or available balance changed. 'accounts.update#1', // Specify "mode" as 2, Whenever account balance or available balance changed, it will be updated together. 'accounts.update#2', // Subscribe to order updates for all symbols (wildcard allowed) 'orders#*', // Subscribe to order updates for btcusdt only 'orders#btcusdt', // Subscribe to trade clearing for all symbols (wildcard allowed) (mode is optional) 'trade.clearing#*', // Subscribe to trade clearing for btcusdt only (mode is optional) 'trade.clearing#btcusdt', // Subscribe to trade clearing for all symbols, mode 0 (trade event only (default)) 'trade.clearing#*#0', // Subscribe to trade clearing for all symbols, mode 1 (trade & cancellation events) 'trade.clearing#*#1', ], WS_KEY_MAP.spotPrivateV2, );} start(); We use essential cookies and optional analytics. Read the Privacy Policy.
Essential cookies stay on. Toggle analytics if you want to share anonymous usage insights. You can revisit this anytime via Cookie Settings in the footer.