← All articles

Hyperliquid live prices in Node.js: replace the deprecated allMids with fastAssetCtxs

Hyperliquid live prices for every market now come from the fastAssetCtxs WebSocket subscription. Hyperliquid’s subscriptions reference lists allMids as “deprecated, use fastAssetCtxs”, and an allMids subscription now pushes about every 12 seconds. The code below streams mark and mid prices for every coin in Node.js and TypeScript with the WebsocketClient from @siebly/hyperliquid-api, keeps them in a cache, and runs unchanged in a browser bundle.

Everything here is public data and needs no key.

Versions: @siebly/hyperliquid-api 1.0.5, Node.js 24.

allMids and fastAssetCtxs side by side

allMids fastAssetCtxs
Status Deprecated The replacement
Fields Mid price only markPx and midPx
Coverage One perp dex per subscription (dex option); spot only with the first dex Every perp dex, HIP-3 markets and spot in one stream
Messages A full map each time, about every 12 s A snapshot, then changes about once a second
On the wire Plain JSON Base64 text holding raw DEFLATE data

The getAllMids() REST call is a separate info request and is not marked deprecated. The deprecation applies to the WebSocket subscription.

What the Hyperliquid live prices stream carries

Each fastAssetCtxs message is an object keyed by coin. Every value has up to two fields: markPx, the mark price, and midPx, the book mid.

  • The first message is a snapshot of every coin, about 2,100 keys: main perps such as BTC, spot pairs such as @107 and PURR/USDC, outcome coins starting with #, and HIP-3 markets with a dex prefix such as xyz:NVDA.
  • Later messages carry only what changed, usually about 110 to 300 coins. A coin can arrive with only midPx or only markPx; the missing field has not changed.
  • midPx can be null, which means the book has no mid. In one snapshot that applies to 886 of 2,131 coins: mostly spot pairs, plus some HIP-3 markets, outcome coins and main perps. markPx is still present for them.

Because diffs leave out unchanged coins and fields, code that shows Hyperliquid live prices needs a cache and has to merge each message into it.

Stream Hyperliquid live prices in Node.js

npm install @siebly/hyperliquid-api
npm install -D typescript @types/node

prices.ts subscribes once and merges every message into a Map:

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

type Price = { markPx?: string; midPx?: string | null };

// One cache for every coin: perps, spot pairs (@107), outcome coins (#...) and HIP-3 markets (xyz:NVDA).
export const prices = new Map<string, Price>();

export const ws = new WebsocketClient({ connectionTimeout: 10_000 });

ws.on('message', (event) => {
  if (event.channel !== 'fastAssetCtxs') return;

  // event.data is already decoded: a plain object keyed by coin.
  // The first message lists every coin; later ones carry only what changed.
  for (const [coin, ctx] of Object.entries(event.data)) {
    prices.set(coin, { ...prices.get(coin), ...ctx });
  }
});

ws.on('reconnected', () => console.log('Reconnected: a fresh snapshot follows'));
ws.on('exception', (err) => console.error('WebSocket error', err));

// If the first connection fails, subscribe() rejects, but the topic stays
// cached and the client keeps retrying, so the stream starts once it connects.
ws.subscribe({ type: 'fastAssetCtxs' }).catch((err) => console.error('First connect failed', err));

Checking event.channel narrows event.data to the SDK’s FastAssetCtxsEvent type, an index of { markPx?: string; midPx?: string | null }, so the merge type-checks under tsc --strict with no casts. The spread keeps any cached field that a diff leaves out.

watch.ts reads the cache every five seconds:

import { prices } from './prices.ts';

const coins = ['BTC', 'ETH', 'xyz:NVDA'];

setInterval(() => {
  for (const coin of coins) {
    const p = prices.get(coin);
    // midPx is null when the book has no mid; markPx is still usable.
    const mid = p?.midPx ?? 'no mid';
    console.log(`${coin.padEnd(9)} mark ${p?.markPx ?? '-'}  mid ${mid}`);
  }
  console.log(`${prices.size} coins cached`);
}, 5_000);
BTC       mark 59624.5  mid 59623.5
ETH       mark 1663.1  mid 1663.15
xyz:NVDA  mark 149.11  mid 149.125
2145 coins cached
BTC       mark 59631.0  mid 59636.5
ETH       mark 1663.2  mid 1663.15
xyz:NVDA  mark 149.11  mid 149.125
2145 coins cached

The files run directly with node src/watch.ts on Node.js 24. Prices are strings, as Hyperliquid sends them, so convert with Number() only where arithmetic needs it.

To list the coins whose book has no mid at the moment:

import { prices, ws } from './prices.ts';

// Wait for the snapshot, then list the coins whose book has no mid right now.
setTimeout(() => {
  const noMid = [...prices].filter(([, p]) => p.midPx === null).map(([coin]) => coin);
  console.log(`${noMid.length} of ${prices.size} coins have no mid, e.g.`, noMid.slice(0, 5));
  ws.closeAll();
}, 5_000);
886 of 2131 coins have no mid, e.g. [ '@1107', 'hyna:ETH', 'cash:GOLD', '@1175', '@865' ]

What the SDK handles: decoding and reconnects

On the wire, the data field of a fastAssetCtxs frame is a base64 string, not JSON. Hyperliquid’s reference describes it as a raw DEFLATE stream with no zlib or gzip wrapper. A frame looks like this (truncated):

{"channel":"fastAssetCtxs","data":"lb1bc2s5kh38X6pfHfxwv9STKIlHYpVEqknpHJ16688Ohydq..."}

The WebsocketClient decodes and inflates that string before it emits the event, so event.data is already the plain object shown above. If a payload cannot be decoded, the client emits an exception event instead of a message.

The client also keeps the subscription across connection drops. After a drop it emits reconnecting, then reconnected, sends the subscription again, and Hyperliquid answers with a fresh full snapshot followed by diffs. The cache needs no special handling: the new snapshot overwrites every coin it lists. Clear the Map in the reconnected handler if coins that disappear from the snapshot should disappear from the cache too.

The WebSocket guide covers the client’s events and options in full.

The same code in a browser

Hyperliquid live prices reach a browser page the same way: the WebsocketClient works through a bundler such as Vite with the same import, the same subscription and the same merge. The browser receives the compressed frames too, and the client inflates them there as well. Keep private keys out of browser code; this stream needs none.

For one coin’s full order book rather than its mark and mid, see Hyperliquid l2Book in JavaScript: slow snapshots, fast mode and l2 diffs.

Missing prices, null mids and timeouts

  • A coin’s midPx is missing from a message. It has not changed. Keep the cached value.
  • midPx is null. The book has no mid. Fall back to markPx or show no price.
  • A coin is missing from the cache. Check the key format: spot pairs use @ indexes and HIP-3 markets carry the dex prefix, as in xyz:NVDA.
  • The first connection times out. connectionTimeout sets the connect limit in milliseconds (1,000 by default in 1.0.5); the example raises it to 10,000 for slower links.

Further reading

@siebly/hyperliquid-api is one of Siebly’s JavaScript SDKs, with over 4.8 million npm downloads across 10+ SDKs.