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.
@siebly/htx-api instead of hand-written WebSocket plumbing.HTX/Spot/WebSockets/wsAPI.RAW.ts.DefaultLogger, WebsocketClient, WS_KEY_MAP.connectWSAPI(), sendWSAPIRequest(), generateNewOrderID().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(); 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.