Skip to content

Subscribe to account order updates ​

Use WebsocketClient to receive orderUpdates and userFills for a Hyperliquid account. These subscriptions work on Mainnet and Testnet, use the managed account address and do not require a private key or signature.

Connect to the account stream ​

This example uses Mainnet. To use Testnet, set testnet: true and use the Testnet account address. Use Node.js 24 with the @siebly/hyperliquid-api npm package installed. Set the account address in a local .env file:

dotenv
HYPERLIQUID_ACCOUNT_ADDRESS=0x1111111111111111111111111111111111111111 # Use your managed account address

Save this as account-stream.ts:

ts
import { WebsocketClient } from '@siebly/hyperliquid-api';

async function main() {
  const user = process.env.HYPERLIQUID_ACCOUNT_ADDRESS;
  if (!user || !/^0x[\da-fA-F]{40}$/.test(user)) {
    throw new Error(
      'Set HYPERLIQUID_ACCOUNT_ADDRESS to the managed account address.',
    );
  }
  const ws = new WebsocketClient({});

  let stopped = false;
  const timer = setTimeout(stop, 60_000);
  function stop() {
    if (stopped) {
      return;
    }
    stopped = true;
    clearTimeout(timer);
    ws.closeAll();
    process.removeListener('SIGINT', stop);
    process.removeListener('SIGTERM', stop);
  }
  process.once('SIGINT', stop);
  process.once('SIGTERM', stop);

  // Install listeners before subscribing so early messages are not missed.
  ws.on('exception', (error) => {
    console.error('Account stream exception:', error);
  });
  ws.on('reconnecting', (event) => {
    console.log('Account stream reconnecting:', event);
  });
  ws.on('reconnected', (event) => {
    console.log('Account stream reconnected:', event);
  });
  ws.on('response', (event) => {
    console.log('Subscription response:', event);
  });
  ws.on('message', (event) => {
    if (stopped) {
      return;
    }
    if (event.channel === 'orderUpdates') {
      for (const update of event.data) {
        console.log(
          'Order:',
          update.order.oid,
          update.order.coin,
          update.status,
        );
      }
    } else if (event.channel === 'userFills' && !event.data.isSnapshot) {
      for (const fill of event.data.fills) {
        console.log(
          'Fill:',
          fill.oid,
          fill.coin,
          'price:',
          fill.px,
          'size:',
          fill.sz,
        );
      }
    } else {
      console.log('Account update:', event);
    }
  });
  try {
    await ws.subscribe([
      { type: 'orderUpdates', user },
      { type: 'userFills', user },
    ]);
  } catch (error) {
    // Topics remain cached for the SDK's automatic reconnect attempts.
    console.error('Subscription setup failed:', error);
  } finally {
    // A stop can occur while subscription setup is pending.
    if (stopped) {
      ws.closeAll();
    }
  }
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Run it in a terminal:

sh
node --env-file=.env account-stream.ts

The response listener logs server subscription acknowledgements for orderUpdates and userFills. A resolved subscribe() promise confirms local sending, not server acceptance. Run the order example in another terminal to produce updates.

Read updates ​

orderUpdates contains an array of order changes. Each entry includes the order ID, coin, status and status timestamp. Use order.oid to correlate an update with the order ID returned when placing the order.

userFills contains fills and an optional isSnapshot flag. The example skips the initial snapshot when printing new fills. In a persistent application, use the snapshot to initialize state and deduplicate later fills using their trade identifiers. Order-status updates and fills describe different events.

Load existing orders and any state not covered by the stream snapshots at startup. Account for events received during that read, then use WebSocket updates to maintain local state. Do not follow every submission or cancellation with a REST query. A quiet account may produce no new messages.

These two subscriptions cover orders and fills. Add the account-state subscriptions needed for your balances or positions; a fill event alone does not provide a complete account view. See account state.

For perpetual positions, the clearinghouseState subscription provides account state including assetPositions. Compare the previous and current position size (szi) for the account, DEX and coin to trigger follow-up logic, such as placing or adjusting SL/TP orders when a position opens or increases. Use a throttled debounce with a maximum wait so partial-fill bursts produce fewer API calls without postponing the work indefinitely. An account-state message does not necessarily represent a new fill.

See the subscription payloads for the complete event fields.

Disconnect and recover ​

The SDK automatically reconnects and restores cached subscriptions. The example logs reconnecting, reconnected and exception events without stopping recovery. It closes after 60 seconds or on Ctrl+C/SIGTERM and clears its timer and signal listeners.

After a connection gap, use REST to reconcile order and fill history with streamed updates. reconnected reports a restored connection, not a replay of missed events. Closing the stream does not cancel orders.

WebSocket subscriptions · Authentication · Production usage

Built by Siebly. Open source SDKs for exchange APIs.