Private account updates
Subscribe to private Spot updates through spotPrivateV2. The SDK authenticates the connection using its configured API key and secret. Keep these clients on your server.
Subscribe to accounts, orders and trades
import { WebsocketClient } from '@siebly/htx-api';
const apiKey = process.env.HTX_API_KEY;
const apiSecret = process.env.HTX_API_SECRET;
if (!apiKey || !apiSecret) {
throw new Error('Set HTX_API_KEY and HTX_API_SECRET.');
}
const ws = new WebsocketClient({ apiKey, apiSecret });
ws.on('message', (message) => {
if (Array.isArray(message)) {
return;
}
console.log(
'Private update channel:',
message.ch,
'connection:',
message.wsKey,
);
// Apply the channel payload to private application state. Do not publish it.
});
ws.on('authenticated', ({ wsKey }) => console.log('Authenticated:', wsKey));
ws.on('exception', () =>
console.error('Private stream exception. Check credentials and permissions.'),
);
ws.subscribe(
['accounts.update#2', 'orders#btcusdt', 'trade.clearing#btcusdt#0'],
'spotPrivateV2',
);
process.once('SIGINT', () => ws.closeAll());
process.once('SIGTERM', () => ws.closeAll());The example subscribes to account changes, Bitcoin/USDT orders and trade clearing. These are different event types; store their payloads separately.
accounts.update#2 selects combined account/available-balance updates. orders#btcusdt and trade.clearing#btcusdt#0 select symbol-specific order and execution events. Use the Spot WebSocket reference for the current channel fields and mode behavior.
Recover after a gap
- Mark affected state stale on
reconnectingor unexpected closure. - After authentication and resubscription, buffer new private events while reading balances, open orders and recent fills over REST.
- Reconcile with your persisted order IDs and execution identifiers. Paginate the history reads and account for their retention windows.
- Apply buffered events without duplicating executions, then allow trading decisions to use the recovered state.
Automatic resubscription does not guarantee replay of missed events. A reopened socket or authentication acknowledgment does not mean balances and orders have been reconciled.
Derivatives accounts
Use the connection and topics for your derivatives account mode. linearSwapPrivate, coinDeliveryPrivate, coinSwapPrivate and derivativesPrivateV5 are distinct choices. Do not send Spot orders#... topics to a derivatives connection or mix legacy and unified account state.