HTX Spot Private JavaScript SDK example: orderManagement.ts

HTX Spot Private order management 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/Private/orderManagement.ts

What this example covers

  • HTX exchange API JavaScript example.
  • Uses the Siebly HTX SDK package @siebly/htx-api instead of hand-written HTTP request plumbing.
  • Source path: HTX/Spot/Private/orderManagement.ts.
  • Example category: Spot Private.
  • Imports SDK symbols including SpotClient.
  • Calls SDK methods such as getOpenOrders(), getOrderByClientId(), getOrderHistory(), getOrderHistory48h(), getMatchResults(), cancelOrderById(), cancelOrderByClientId(), cancelAllOrders(), setCancelAllAfter().

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 private or order-management examples, keep credentials in environment variables or a secret manager. Use read-only keys where possible, and check the execution mode reference before any exchange write path can run.
  • Open the repository source when you need the latest committed version: GitHub source file.
/* eslint-disable @typescript-eslint/no-unused-vars */import { SpotClient } from '@siebly/htx-api'; // This example shows how to call HTX Spot API endpoints for ORDER MANAGEMENT. const client = new SpotClient({  apiKey: process.env.API_SPOT_KEY || 'insertApiKeyHere',  apiSecret: process.env.API_SPOT_SECRET || 'insertApiSecretHere',}); async function getOpenOrders() {  try {    const openOrders = await client.getOpenOrders({ symbol: 'btcusdt' });    console.log('Open Orders: ', JSON.stringify(openOrders, null, 2));  } catch (e) {    console.error('Get open orders error: ', e);  }} async function getOpenOrdersByClientId() {  try {    const order = await client.getOrderByClientId({      clientOrderId: 'your-client-order-id-here',    });    console.log('Order by Client ID: ', JSON.stringify(order, null, 2));  } catch (e) {    console.error('Get order by client id error: ', e);  }} async function getOrderHistory() {  try {    const orderHistory = await client.getOrderHistory({      symbol: 'btcusdt',      states: 'filled,canceled,partial-canceled',      size: 50,    });    console.log('Order History: ', JSON.stringify(orderHistory, null, 2));  } catch (e) {    console.error('Get order history error: ', e);  }} async function getOrderHistory48h() {  try {    const orderHistory = await client.getOrderHistory48h({      symbol: 'btcusdt',      size: 50,    });    console.log('Order History (48h): ', JSON.stringify(orderHistory, null, 2));  } catch (e) {    console.error('Get order history 48h error: ', e);  }} async function getMatchResults() {  try {    const matchResults = await client.getMatchResults({      symbol: 'btcusdt',      size: 50,    });    console.log('Match Results: ', JSON.stringify(matchResults, null, 2));  } catch (e) {    console.error('Get match results error: ', e);  }} async function cancelOrderById() {  try {    const cancelResult = await client.cancelOrderById({      orderId: '1620028655831163',      symbol: 'btcusdt',    });    console.log('Cancel Order Result: ', JSON.stringify(cancelResult, null, 2));  } catch (e) {    console.error('Cancel order by id error: ', e);  }} async function cancelOrderByClientId() {  try {    const cancelResult = await client.cancelOrderByClientId({      'client-order-id': 'your-client-order-id-here',    });    console.log(      'Cancel by Client ID Result: ',      JSON.stringify(cancelResult, null, 2),    );  } catch (e) {    console.error('Cancel order by client id error: ', e);  }} async function cancelAllOrders() {  try {    const cancelAllResult = await client.cancelAllOrders({ symbol: 'btcusdt' });    console.log(      'Cancel All Orders Result: ',      JSON.stringify(cancelAllResult, null, 2),    );  } catch (e) {    console.error('Cancel all orders error: ', e);  }} async function setCancelAllAfter() {  try {    const cancelAfterResult = await client.setCancelAllAfter({ timeout: 120 });    console.log(      'Cancel All After Result: ',      JSON.stringify(cancelAfterResult, null, 2),    );  } catch (e) {    console.error('Set cancel all after error: ', e);  }} // Uncomment the function you want to test: // getOpenOrders();// getOpenOrdersByClientId();// getOrderHistory();// getOrderHistory48h();// etMatchResults();// cancelOrderById();// cancelOrderByClientId();// cancelAllOrders();// setCancelAllAfter(); 

Subscribe on Substack

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