WebSocket API requests
WebsocketAPIClient sends trading commands and returns a promise for each response. It wraps WebsocketClient, opening and authenticating the appropriate trading connection as needed. Subscription streams and trading commands use different connections.
Submit a Spot order
This exported function submits a live order only when called. Validate the account and instrument constraints, and persist the client order ID before calling it:
import { WebsocketAPIClient } from '@siebly/htx-api';
// Call only after confirming the live order parameters and persisting the ID.
export async function placeSpotOrderOverWebSocket(
apiKey: string,
apiSecret: string,
accountId: string,
price: string,
amount: string,
clientOrderId: string,
) {
const api = new WebsocketAPIClient({
apiKey,
apiSecret,
attachEventListeners: false,
});
api
.getWSClient()
.on('exception', () =>
console.error(
'WebSocket API exception. Reconcile the order before retrying.',
),
);
try {
return await api.submitSpotOrder({
'account-id': accountId,
symbol: 'btcusdt',
type: 'buy-limit-maker',
amount,
price,
source: 'spot-api',
'client-order-id': clientOrderId,
});
} finally {
api.getWSClient().closeAll();
}
}The one-shot example closes its client in finally. In a long-running service, reuse the wrapper and close it during application shutdown instead. Closing the connection does not cancel an accepted order.
Choose the command family
Spot methods include submitSpotOrder(), submitSpotBatchOrders() and cancelSpotOrders(). Derivatives methods distinguish linear swaps, delivery contracts and coin-margined swaps. V5 methods such as placeLinearSwapOrder() have a different request contract from legacy submitLinearSwapOrder().
Use WebsocketAPIClient for supported wrappers, or sendWSAPIRequest(wsKey, operation, params) for typed low-level requests. The SDK handles signatures and request correlation; do not copy REST headers or handcraft login messages for these wrappers.
Handle replies and disconnects
Command replies retain the exchange envelope. Inspect the operation's response type and per-item results for batches. A disconnect rejects in-flight promises even if the server may already have accepted a command.
After an uncertain order or cancellation, reconcile private events and REST order/fill reads before retrying. Automatic reconnection and reauthentication do not make a rejected promise safe to replay. See Order recovery.