Thanks for putting this together, Lucas. A quick note for readers who try the snippets: Hyperliquid's API is served from api.hyperliquid.xyz (api.hyperliquid.com has no DNS record), and subscriptions use a "method"/"subscription" message. This version of the WebSocket part streams live BTC trades (I ran it this morning):
import json, threading, websocket
def on_message(ws, message):
data = json.loads(message)
if data.get("channel") == "trades":
for t in data["data"]:
print(t["coin"], t["side"], t["px"], t["sz"])
def on_open(ws):
ws.send(json.dumps({
"method": "subscribe",
"subscription": {"type": "trades", "coin": "BTC"}
}))
ws = websocket.WebSocketApp("wss://api.hyperliquid.xyz/ws",
on_open=on_open, on_message=on_message)
threading.Timer(10, ws.close).start()
ws.run_forever(ping_interval=50, ping_payload='{"method":"ping"}')
Orders work a little differently from exchanges with key and secret headers: Hyperliquid has no API key. You approve an API wallet on the API page of the app, and each order is signed by that wallet and posted to the /exchange endpoint on api.hyperliquid.xyz. The Signing page in the Hyperliquid docs covers the details.
For anyone building in Node.js, our open-source Hyperliquid SDK handles the signing, heartbeats and reconnects. The same trade stream there:
import { WebsocketClient } from '@siebly/hyperliquid-api';
const ws = new WebsocketClient({ connectionTimeout: 5_000 });
ws.on('message', (e) => {
if (e.channel === 'trades') {
for (const t of e.data) console.log(t.coin, t.side, t.px, t.sz);
}
});
await ws.subscribe({ type: 'trades', coin: 'BTC' });
More examples are in the Siebly Hyperliquid SDK docs on siebly.io.
Daisy, Siebly