Hyperliquid JavaScript SDK example: ws-public.ts

Hyperliquid websocket public JavaScript example. Read the source code, review its SDK calls, and browse related exchange integration examples.

This example uses @siebly/hyperliquid-api, the JavaScript and TypeScript SDK for Hyperliquid. Install it with npm install @siebly/hyperliquid-api.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/Hyperliquid/Websocket/ws-public.ts

What this example covers

  • Hyperliquid WebSocket stream JavaScript example.
  • Uses the Siebly Hyperliquid SDK package @siebly/hyperliquid-api instead of hand-written WebSocket plumbing.
  • Source path: Hyperliquid/Websocket/ws-public.ts.
  • Imports SDK symbols including DefaultLogger, WebsocketClient.
  • Calls SDK methods such as on(), subscribe(), nameToCoin().

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 WebSocket examples, keep reconnect, resubscribe, heartbeat, and event-handler behavior explicit in your service.
  • Open the repository source when you need the latest committed version: GitHub source file.
import { DefaultLogger, WebsocketClient } from '@siebly/hyperliquid-api'; // or// import { DefaultLogger, WebsocketClient } from '@siebly/hyperliquid-api'; /** * Public market-data subscriptions. * * Listen for data with on('message') and acknowledgements with on('response'). * Every event includes the connection's wsKey. * * npx tsx examples/Websocket/ws-public.ts */const logger = {  ...DefaultLogger,  // Uncomment to log connection lifecycle events:  trace: (...params: unknown[]) => console.log(new Date(), 'trace', ...params),}; const wsClient = new WebsocketClient(  {    // testnet: true,  },  logger,); wsClient.on('open', (data) => {  console.log('ws connection opened:', data.wsKey);}); wsClient.on('reconnecting', ({ wsKey }) => {  console.log('ws automatically reconnecting.... ', wsKey);}); wsClient.on('reconnected', (data) => {  console.log('ws has reconnected ', data?.wsKey);}); wsClient.on('exception', ({ event, data, wsKey }) => {  console.error('ws exception:', wsKey, event ?? data);}); wsClient.on('close', ({ wsKey }) => {  console.log('ws closed:', wsKey);}); wsClient.on('response', (event) => {  console.log('ws response:', event.wsKey, event.channel, event.data);}); wsClient.on('message', (event) => {  switch (event.channel) {    case 'allMids':      console.log('allMids BTC:', event.data.mids.BTC);      break;    case 'l2Book':      console.log(        'Order book:',        event.wsKey,        event.data.coin,        event.data.levels,      );      break;    case 'trades':      console.log('Trades:', event.wsKey, event.data);      break;    default:      console.log(        `Market update (unhandled in switch case: "${event.channel}"):`,        event.wsKey,        event.channel,        event.data,      );  }}); async function main() {  try {    await wsClient.subscribe([      { type: 'allMids' },      { type: 'l2Book', coin: 'ETH' },      { type: 'trades', coin: 'BTC' },      { type: 'candle', coin: 'ETH', interval: '1m' },      { type: 'bbo', coin: 'ETH' },      { type: 'activeAssetCtx', coin: 'BTC' },      // Native Spot universe names pass through without metadata requests.      { type: 'trades', coin: 'PURR/USDC' },    ]);     // For a readable alias, resolve it explicitly before subscribing:    // const coin = await wsClient.nameToCoin('HYPE/USDC');    // await wsClient.subscribe({ type: 'trades', coin });  } catch (error) {    console.error('subscribe failed:', error);  }} main(); 

Subscribe on Substack

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