Skip to main content
When you need state updates faster than 15-second polling allows, open a WebSocket. The server pushes a fresh reading for each asset every time the engine recomputes it (not only when the state changes), plus a heartbeat every 5 seconds. Endpoint: wss://api.pegana.xyz/v1/ws

Open a connection

Prefer a typed helper? The first-party SDKs wrap this feed and handle the subscribe/heartbeat framing for you — TypeScript PegFeed / connectPegFeed (@peganahq/sdk-ts) and Python peg_feed() (pegana-sdk). Works from the browser too (globalThis.WebSocket), Node, and Bun. No auth, no key. Origin header is checked against an allowlist — https://pegana.xyz, https://www.pegana.xyz, http://localhost:3000 by default. Add more origins via the CORS_EXTRA_ORIGINS env var on self-hosted instances.

Message shapes

All messages are JSON, single-line per frame. The op field discriminates.

Server → client: state update

Pushed on every engine recompute for the asset, typically several times a minute per asset, whether or not the state changed. Most frames repeat the previous state; to react only to transitions, compare payload.state with the last value you saw for that asset. The envelope carries the asset symbol; payload carries the current state, the smoothed discount (raw sign: 1 − market / intrinsic, positive = below fair value), the two underlying values (intrinsic_usd, market_usd), and updated_at. There is no previous_state or confidence field on this frame — fetch the prior state from your own last-seen value, and use /v1/assets/{symbol}/state for the confidence score. Two optional details on update frames:
  • market_observed_at is a nullable string in the same millisecond-precision UTC format as updated_at (YYYY-MM-DDTHH:MM:SS.mmmZ). It appears only when the server has the dark-market gate enabled, and tells you when the market price was last observed.
  • state_reason explains an UNKNOWN state; see the table below.
market_reference_divergence, depth_below_floor and premium_hold_timeout come from guards that are off by default. market_unavailable can appear without any guard when the market feed has gone quiet, and the engine can also emit it when its dark-market guard (off by default) is switched on. Treat any state_reason you do not recognise as “state unknown, reason new”. Values use the same wire format as REST: decimals are strings with trailing zeros trimmed, discount has the same 6-decimal precision /v1/assets serves, and timestamps are RFC 3339 UTC with millisecond precision. The stream covers every asset the engine computes, which can include assets that are not listed on GET /v1/assets (for example xSOL, which is computed but kept off the peg surface). If you only care about listed assets, filter frames against the symbols you got from GET /v1/assets.

Server → client: heartbeat

Every 5 seconds. Use this to detect connection death — if you go 15s without any frame (update or heartbeat), drop and reconnect.

Server → client: error

Returned on bad input. Connection stays open.

Client → server: ping

Server responds with { "op": "pong" }. Optional — heartbeats already keep the connection alive on most NAT/proxy configurations.

Client → server: subscribe / unsubscribe

In v1 these do not filter anything: the server broadcasts updates for every asset to every connection. Filter client-side by inspecting msg.asset. Per-asset subscription is planned for v2. What the server answers today, so your parser is not surprised:
  • subscribe is acknowledged with an update frame whose asset is the literal string "system" and whose payload has no state:
    Skip frames with asset === "system" (or without payload.state) before treating a frame as a peg reading, otherwise you will record a phantom asset named system.
  • unsubscribe is answered with { "op": "pong" }.
  • A frame that is not valid JSON or has an unknown op gets an error frame ("msg": "malformed: …"); the connection stays open.

Connection limits

  • Per-IP cap: 20 concurrent connections. Beyond that, handshakes return 429.
  • Handshake budget: 30 accepted handshakes per minute per address. Exceeding it returns 429 with ws_rate_limited and a Retry-After header. A connection rejected by a concurrency cap does not use this budget.
  • Client messages: at most 4 KiB per message and frame. A subscribe request accepts at most 256 asset names; a larger list receives an error frame.
  • Capacity: above 10,000 open sockets in total the server answers new handshakes with 503; retry with backoff.
  • Slow readers: a client that cannot receive an outbound frame within 10 seconds is disconnected.
  • Idle timeout: if the server receives nothing from your client for 120 seconds, it sends a WebSocket protocol ping and closes the connection if no frame arrives within the next 30 seconds. Standard clients (browser WebSocket, ws, websockets) answer protocol pings automatically, so they stay connected indefinitely; a hand-rolled client must reply to the ping or send its own { "op": "ping" } at least every two minutes.
  • Shared IPs: IPv6 addresses are grouped by their /64 prefix. The cap is per source address or IPv6 /64, so every open Pegana dashboard tab, script and notebook behind the same NAT or office network counts toward the same 20. If you get 429 while developing, close other tabs or connections on that network first.

Reconnect strategy

Recommended client logic:

What about historical data?

The WebSocket is stream-only. For history, use /v1/assets/{symbol}/history. A common pattern is to fetch the last 24h via REST on connect, then maintain real-time via WebSocket.

Next

REST API

Poll state, history and audit receipts.

MCP for agents

Native MCP tool for Claude, Cursor, Cline.