Hyperliquid JavaScript SDK example: rest-public.ts

Hyperliquid REST 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/Rest/rest-public.ts

What this example covers

  • Hyperliquid REST API JavaScript example.
  • Uses the Siebly Hyperliquid SDK package @siebly/hyperliquid-api instead of hand-written HTTP request plumbing.
  • Source path: Hyperliquid/Rest/rest-public.ts.
  • Imports SDK symbols including RestClient.
  • Calls SDK methods such as getExchangeStatus(), getAllMids(), getOrderBook(), nameToCoin(), resolveAsset(), getCandles(), getRecentTrades(), getPerpMetadata(), getSpotMetadata(), getPredictedFundingRates().

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.
  • Open the repository source when you need the latest committed version: GitHub source file.
import { RestClient } from '@siebly/hyperliquid-api'; // or// import { RestClient } from '@siebly/hyperliquid-api'; /** * Public Info calls do not need a private key. * * npx tsx examples/Rest/rest-public.ts */const client = new RestClient(); // Use testnet instead:// const client = new RestClient({ testnet: true }); async function main() {  try {    const status = await client.getExchangeStatus();    console.log('exchange status:', status);     const mids = await client.getAllMids();    console.log('BTC mid:', mids.BTC);    console.log('ETH mid:', mids.ETH);     // nSigFigs is optional aggregation. Some nSigFigs+mantissa combos 500 on    // Hyperliquid's side; start with nSigFigs alone.    const book = await client.getOrderBook({      coin: 'BTC',      nSigFigs: 5,    });    console.log('BTC best bid:', book?.levels[0]?.[0]);    console.log('BTC best ask:', book?.levels[1]?.[0]);     // Info remaps pair aliases like the official Python SDK: HYPE/USDC -> @107.    // PURR/USDC stays PURR/USDC because that is the universe name.    const [purr, hypeBook, hypeCoin] = await Promise.all([      client.getOrderBook({ coin: 'PURR/USDC' }),      client.getOrderBook({ coin: 'HYPE/USDC' }),      client.nameToCoin('HYPE/USDC'),    ]);    console.log('PURR book:', purr?.levels[0]?.[0]);    console.log('HYPE info coin:', hypeCoin);    console.log('HYPE book:', hypeBook?.levels[0]?.[0]);     const asset = await client.resolveAsset('HYPE/USDC');    console.log('resolved HYPE/USDC:', {      assetId: asset.assetId,      canonicalName: asset.canonicalName,      kind: asset.kind,      szDecimals: asset.szDecimals,    });     const candles = await client.getCandles({      coin: 'BTC',      interval: '1m',      startTime: Date.now() - 5 * 60_000,    });    console.log('BTC candles:', candles.length, candles.at(-1));     const trades = await client.getRecentTrades({ coin: 'BTC' });    console.log(      'BTC recent trades:',      Array.isArray(trades) ? trades.length : trades,      Array.isArray(trades) ? trades[0] : undefined,    );     const [perpMeta, spotMeta] = await Promise.all([      client.getPerpMetadata(),      client.getSpotMetadata(),    ]);    console.log(      'perp universe size:',      perpMeta.universe.length,      'spot universe size:',      spotMeta.universe.length,    );    console.log(      'sample perps:',      perpMeta.universe.slice(0, 5).map((row) => row.name),    );     const funding = await client.getPredictedFundingRates();    console.log(      'predicted funding entries:',      Array.isArray(funding) ? funding.length : funding,    );  } catch (e) {    console.error('request failed: ', e);  }} main(); 

Subscribe on Substack

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