Socket

Market data and your accounts, live, on one WebSocket.

Connect

Send your API key as Authorization: Bearer on the upgrade. Read and trade keys both work; no Origin needed.

Messages are JSON text frames. The first one from the server is hello.

wss://api.perpfoundry.com/api/ws
Authorization: Bearer pf_…
{ "type": "hello", "userId": "usr_…", "tradingPaused": false }

Subscribe

Send sub with a channel and a market or account; unsub with the same fields stops it. Subscribing twice has no effect. Accounts must be yours.

Keepalive: send ping, get { "type": "pong" }.

{ "op": "sub", "ch": "mark", "market": "hyperliquid:BTC" }
{ "op": "unsub", "ch": "mark", "market": "hyperliquid:BTC" }
{ "op": "ping" }

Your accounts

No market or account: one channel for all your accounts.

accounts

Every account you hold, as GET /api/accounts lists them. Then opened and closed as they happen, from the terminal, the engine or us. reason: breach, pass, reset or term_ended. Orders to a closed account are refused; the API never moves you to another.

{ "op": "sub", "ch": "accounts" }
{
  "ch": "accounts",
  "data": [
    {
      "accountId": "acc_3f1c…",
      "name": "Demo 10K",
      "accountType": "demo",
      "status": "active",
      "…": ""
    }
  ]
}

A reset: the old account closes, a new one opens.

{
  "ch": "accounts",
  "closed": {
    "accountId": "acc_3f1c…",
    "reason": "reset",
    "at": 1764150000000
  }
}
{
  "ch": "accounts",
  "opened": {
    "accountId": "acc_9b2e…",
    "name": "Competition 10K",
    "accountType": "competition",
    "status": "active",
    "competition": {
      "competitionId": "weekly-10k",
      "edition": 3,
      "endsAt": 1764547200000
    },
    "…": ""
  }
}

Market channels

Per market, by its id from GET /api/markets.

book

Book snapshots, at most about 3 a second. depth: levels a side, default and most 20. Subscribe again with another depth to switch.

{
  "op": "sub",
  "ch": "book",
  "market": "hyperliquid:BTC",
  "depth": 10
}
{
  "ch": "book",
  "market": "hyperliquid:BTC",
  "data": {
    "market": "hyperliquid:BTC",
    "seq": "81723",
    "at": 1764150000000,
    "bids": [
      {
        "px": "96380.0",
        "sz": "1.2"
      }
    ],
    "asks": [
      {
        "px": "96381.0",
        "sz": "0.8"
      }
    ]
  }
}

trades

Public trades on the venue.

{ "op": "sub", "ch": "trades", "market": "hyperliquid:BTC" }
{
  "ch": "trades",
  "market": "hyperliquid:BTC",
  "data": [
    {
      "px": "96380.0",
      "sz": "0.05",
      "side": "buy",
      "at": 1764150000000
    }
  ]
}

mark

Mark, oracle and mid price, the predicted funding rate per interval and open interest. Each funding settlement arrives as funding.

{ "op": "sub", "ch": "mark", "market": "hyperliquid:BTC" }
{
  "ch": "mark",
  "market": "hyperliquid:BTC",
  "data": {
    "markPx": "96380.5",
    "oraclePx": "96378.0",
    "midPx": "96380.5",
    "fundingRate": "0.0000125",
    "openInterest": "18250.4",
    "at": 1764150000000
  }
}

A settlement:

{
  "ch": "mark",
  "market": "hyperliquid:BTC",
  "funding": {
    "rate": "0.0000125",
    "settledAt": 1764151200000
  }
}

candles

The current candle as it changes. interval: 1m (default), 5m, 15m, 1h, 4h or 1d. On reset, reload history from REST.

{
  "op": "sub",
  "ch": "candles",
  "market": "hyperliquid:BTC",
  "interval": "5m"
}
{
  "ch": "candles",
  "market": "hyperliquid:BTC",
  "interval": "5m",
  "data": {
    "t": 1764150000000,
    "o": "96300.0",
    "h": "96410.0",
    "l": "96280.0",
    "c": "96380.0",
    "v": "42.1",
    "n": 318
  }
}
{
  "ch": "candles",
  "market": "hyperliquid:BTC",
  "interval": "5m",
  "reset": true
}

Gaps

If market data was missed, book, trades and mark send a gap. Book and candles resend; refetch anything else you keep.

{
  "ch": "trades",
  "market": "hyperliquid:BTC",
  "gap": "feed reconnected"
}

Account channels

Per account, by its accountId from GET /api/accounts. Every message carries seq: it reflects every account event up to that sequence.

account

Balances, status and whether trading is paused. A snapshot first, then each change.

{ "op": "sub", "ch": "account", "accountId": "acc_3f1c…" }
{
  "ch": "account",
  "accountId": "acc_3f1c…",
  "seq": 42,
  "data": {
    "accountId": "acc_3f1c…",
    "status": "active",
    "balances": {
      "balance": {
        "amount": "10000",
        "currency": "SIM_USD"
      },
      "equity": {
        "amount": "10012.5",
        "currency": "SIM_USD"
      },
      "marginUsed": {
        "amount": "1927.6",
        "currency": "SIM_USD"
      },
      "at": 1764150000000
    },
    "tradingPaused": false
  }
}

orders

Every working order first, then orders as they change. Orders as in the REST reference.

{ "op": "sub", "ch": "orders", "accountId": "acc_3f1c…" }
{
  "ch": "orders",
  "accountId": "acc_3f1c…",
  "seq": 42,
  "data": [
    {
      "orderId": "ord_…",
      "clientOrderId": "grid-7",
      "market": "hyperliquid:BTC",
      "side": "buy",
      "type": "limit",
      "size": "0.25",
      "limitPx": "96000",
      "status": "open",
      "…": ""
    }
  ]
}

positions

All open positions, at mark, on each change. Positions as in the REST reference.

{ "op": "sub", "ch": "positions", "accountId": "acc_3f1c…" }
{
  "ch": "positions",
  "accountId": "acc_3f1c…",
  "seq": 42,
  "data": [
    {
      "market": "hyperliquid:BTC",
      "size": "0.25",
      "entryPx": "96000",
      "markPx": "96380.5",
      "…": ""
    }
  ]
}

rules

Rule measures, as in the REST reference. On a breach or a pass, an outcome.

{ "op": "sub", "ch": "rules", "accountId": "acc_3f1c…" }
{
  "ch": "rules",
  "accountId": "acc_3f1c…",
  "seq": 42,
  "data": {
    "accountId": "acc_3f1c…",
    "status": "active",
    "rules": [
      {
        "ruleId": "max_loss",
        "status": "ok",
        "headroom": "612.5",
        "…": ""
      }
    ],
    "…": ""
  }
}
{
  "ch": "rules",
  "accountId": "acc_3f1c…",
  "seq": 57,
  "outcome": {
    "kind": "breach",
    "breach": {
      "ruleId": "daily_loss",
      "…": ""
    },
    "at": 1764150000000
  }
}

events

Every stored account event, in order, each with its seq. Send after to resume (below); without it, events from now on.

{
  "op": "sub",
  "ch": "events",
  "accountId": "acc_3f1c…",
  "after": 41
}
{
  "ch": "events",
  "accountId": "acc_3f1c…",
  "event": {
    "seq": 42,
    "type": "fill.recorded",
    "at": 1764150000000,
    "payload": {
      "fill": {
        "…": ""
      }
    }
  }
}

Everything up to this sequence has been sent; live events follow:

{ "ch": "events", "accountId": "acc_3f1c…", "caughtUp": 42 }

Resume

After a drop, subscribe to events with after set to the last seq you handled. You get each missed event once, then caughtUp, then live events. All events are kept, so any earlier sequence works.

An after past the account's last event is refused with out_of_range; seq is the last event.

{
  "op": "sub",
  "ch": "events",
  "accountId": "acc_3f1c…",
  "after": 41
}
{
  "type": "error",
  "code": "out_of_range",
  "message": "Nothing after 90: the account's last event is 57. Subscribe without after to start from now.",
  "ch": "events",
  "accountId": "acc_3f1c…",
  "seq": 57
}

Other messages

trading: the engine went away or came back. While paused, orders are refused with trading_paused.

error: a message was refused. code is from the error codes; ch, market and accountId say which subscription.

{ "type": "trading", "paused": true }
{
  "type": "error",
  "code": "not_found",
  "message": "No such market.",
  "ch": "book",
  "market": "hyperliquid:NOPE"
}

Limits

Messages you send
20 a second per connection; more get a rate_limited error. Past 100 in a second, the socket closes (1008).
Subscriptions
200 per connection.
Sockets
5 open per API key; a 6th is refused (1008).
Message size
16 KB.
Slow readers
A connection more than 4 MB behind is closed (1013).
Compression
permessage-deflate.

Close codes

1008
No key or session, a revoked key, signed out, too many messages or too many sockets.
1013
Too slow: reconnect and resume.