Skip to main content

WebSocket API Quick Reference

Quick reference for connecting to and using the Hyperliquid Order Book WebSocket API. For detailed documentation on each subscription type, see the individual pages.

Detailed Documentation#

SubscriptionDescriptionDocumentation
TradesReal-time trade executionsView Trades Docs →
L2 Order BookAggregated price levelsView L2 Docs →
L4 Order BookIndividual order visibilityView L4 Docs →

For an overview of all subscription types and use cases, see the Order Book Server Overview.

Connection#

wss://<your-instance>.dwellir.com/ws

Contact ben@dwellir.com for server credentials.

Quick Start#

import asyncio
import json
import websockets

async def main():
ws_url = "wss://your-instance.dwellir.com/ws"

async with websockets.connect(ws_url) as websocket:
# Subscribe to BTC trades
await websocket.send(json.dumps({
"method": "subscribe",
"subscription": {"type": "trades", "coin": "BTC"}
}))

async for message in websocket:
print(json.loads(message))

asyncio.run(main())

Subscription Types#

Trades#

{
"method": "subscribe",
"subscription": {
"type": "trades",
"coin": "BTC"
}
}

Full Trades Documentation →

L2 Order Book#

{
"method": "subscribe",
"subscription": {
"type": "l2Book",
"coin": "BTC",
"nSigFigs": 5,
"nLevels": 20
}
}
ParameterDefaultRangeDescription
nSigFigs52-5Price aggregation significant figures
nLevels201-100Number of price levels per side

Full L2 Documentation →

L4 Order Book#

{
"method": "subscribe",
"subscription": {
"type": "l4Book",
"coin": "BTC"
}
}

Full L4 Documentation →

Unsubscribe#

{
"method": "unsubscribe",
"subscription": {
"type": "trades",
"coin": "BTC"
}
}

Server Messages#

Subscription Confirmation#

{
"channel": "subscriptionResponse",
"data": {
"method": "subscribe",
"subscription": { "type": "trades", "coin": "BTC" }
}
}

Error Response#

{
"channel": "error",
"data": "Invalid subscription: coin not found"
}

Common Errors#

ErrorCause
"coin not found"Invalid trading pair symbol
"n_levels too high"Requested more than 100 levels
"Already subscribed"Duplicate subscription
"Order book not ready"Server initializing

Response Channels#

ChannelSubscription Type
tradesTrades stream
l2BookL2 order book
l4BookL4 order book
subscriptionResponseSubscription confirmations
errorError messages

Reconnection#

The server may restart to resync state. Implement automatic reconnection:

async def connect_with_retry():
delay = 1
while True:
try:
async with websockets.connect(ws_url) as ws:
delay = 1 # Reset on success
await handle_connection(ws)
except Exception:
await asyncio.sleep(delay)
delay = min(delay * 2, 60) # Exponential backoff

Resources#


For detailed documentation, visit the individual subscription pages: Trades | L2 Book | L4 Book