Node.js / JavaScript quickstart
Read a public BTC price with @siebly/hyperliquid-api. No API key or TypeScript compiler is needed. Prices are BTC mid prices.
Use Node.js 24 and npm. If you prefer to start from a finished project, view the complete Hyperliquid Node.js example on GitHub.
Create the project
mkdir hyperliquid-node-quickstart
cd hyperliquid-node-quickstart
npm init -y
npm install @siebly/hyperliquid-apinpm init creates package.json; npm install adds the current SDK release. You do not need to edit the package file for these examples.
Read a price
For a new project, create price.mjs with import. If your project uses require, choose price.cjs instead. Create one file; both request the price directly.
import { RestClient } from '@siebly/hyperliquid-api';
const client = new RestClient();
const mids = await client.getAllMids();
const price = mids.BTC;
if (!price) {
throw new Error('No BTC price was returned.');
}
console.log('BTC REST price:', price);const { RestClient } = require('@siebly/hyperliquid-api');
async function main() {
const client = new RestClient();
const mids = await client.getAllMids();
const price = mids.BTC;
if (!price) {
throw new Error('No BTC price was returned.');
}
console.log('BTC REST price:', price);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Run the file you created:
node price.mjsnode price.cjsThe result is a price string, for example:
BTC REST price: 65000.5Stream a price (optional)
For a new project, create stream.mjs with import. If your project uses require, choose stream.cjs instead. Create one stream file; it closes the client after the first price or a failure.
import { WebsocketClient } from '@siebly/hyperliquid-api';
import process from 'node:process';
async function main() {
const ws = new WebsocketClient();
let timeout;
let stopped = false;
try {
const firstPrice = new Promise((resolve, reject) => {
timeout = setTimeout(() => {
reject(new Error('No BTC update within 30 seconds.'));
}, 30_000);
ws.on('message', (message) => {
if (message.channel !== 'allMids') {
return;
}
const price = message.data?.mids?.BTC;
if (price) {
resolve(String(price));
}
});
ws.on('exception', reject);
const subscription = ws.subscribe({ type: 'allMids' });
subscription.catch(reject).finally(() => {
if (stopped) {
ws.closeAll();
}
});
});
console.log('BTC WebSocket price:', await firstPrice);
} finally {
stopped = true;
clearTimeout(timeout);
ws.closeAll();
console.log('WebSocket closed.');
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});const { WebsocketClient } = require('@siebly/hyperliquid-api');
const process = require('node:process');
async function main() {
const ws = new WebsocketClient();
let timeout;
let stopped = false;
try {
const firstPrice = new Promise((resolve, reject) => {
timeout = setTimeout(() => {
reject(new Error('No BTC update within 30 seconds.'));
}, 30_000);
ws.on('message', (message) => {
if (message.channel !== 'allMids') {
return;
}
const price = message.data?.mids?.BTC;
if (price) {
resolve(String(price));
}
});
ws.on('exception', reject);
const subscription = ws.subscribe({ type: 'allMids' });
subscription.catch(reject).finally(() => {
if (stopped) {
ws.closeAll();
}
});
});
console.log('BTC WebSocket price:', await firstPrice);
} finally {
stopped = true;
clearTimeout(timeout);
ws.closeAll();
console.log('WebSocket closed.');
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Run the matching file:
node stream.mjsnode stream.cjsThe script prints the first update, closes its WebSocket and exits. SDK connection logs may also appear:
BTC WebSocket price: 65001.2
WebSocket closed.Troubleshooting
- Use
.mjsforimportand.cjsforrequire. These Node.js module rules let you choose either format without configuring"type"inpackage.json. - If the SDK cannot be resolved, run the install command from the directory containing your program and
package.json. - If a request fails or no update arrives within 30 seconds, check HTTPS and WSS access to the exchange from your network and region.
- See Runtime compatibility for tested versions and WebSocket subscriptions for continuous subscriptions.