WebSocket Protocols
WebSocket protocol reference for the Scalar trading and market-data feeds.
Two WebSocket endpoints. Every frame is a single JSON text frame, internally tagged by a snake_case type field. Errors on both channels carry one payload:
{
"type": "error",
"code": "invalid_request",
"message": "human-readable prose; branch on code, never message",
"ref_type": "order_insert"
}code is the stable machine-readable ErrorCode vocabulary, shared with REST; message is prose and may change. ref_type (trading WS only) names the inbound frame the error answers, when known.
Trading WebSocket (GET /ws)
Authenticated order entry and private event pushes, one user per connection. The client must send authenticate within the auth deadline; frames sent before it are refused. A connection may arm a dead-man's-switch (set_dead_mans_switch): any inbound frame resets the timer, and if it fires the server closes with code 1008. If the connection cannot keep up with its private feed it is closed with code 1013.
Transport: inbound frame and message sizes are bounded by the server's ws_max_frame_bytes / ws_max_message_bytes configuration (default 8192), not a fixed protocol value. Large server replies (notably cancel_all_ack) are chunked (more: true, 512 cancelled entries per frame).
Client → Server
type | Purpose |
|---|---|
authenticate | Prove ownership of user with a SIWE bearer |
order_insert | Place a resting/IOC order |
order_cancel | Cancel one order by client_tag |
cancel_all | Sweep the user's resting orders by scope |
set_dead_mans_switch | Arm/disarm the dead-man's-switch |
get_account | Fetch balance + margin |
subscribe_account | Stream balance + margin updates |
unsubscribe_account | Stop account updates |
get_position | Fetch one instrument position |
get_products | List products (optional product_ptr) |
heartbeat | Keep-alive / reset DMS |
authenticate
Direction: Client → Server
Proves user with a SIWE bearer token (base64 of [EIP-4361 message, signature]) — the same 24h credential as REST Authorization: Bearer.
{
"type": "authenticate",
"user": "0xf9b04d191dadfa68b2dd10e8359f5e5f48aa8b41",
"token": "base64(eip4361, signature)"
}order_insert
Direction: Client → Server
Places an order for the session user. Same fields as REST POST /orders, including the valid_till minimum-horizon rule: a valid_till inside the venue's valid_till_buffer comes back as order_reject, never order_ack.
client_tag is the packed order nonce and is bound into the EIP-712 signature — it must be the same value you signed. Unix-ms timestamp in the high 64 bits, 32 reserved bits that must stay zero, and a user-unique value in the low 32 bits. Generate its timestamp immediately before signing; it must be within 5 seconds of server time.
The signature is EIP-712 over the deployed protocol contract's domain — name ScalarProtocol, version 1, this chain, this venue's protocol address — because the contract re-verifies that same signature when the order settles. A signature over any other domain name is accepted here and then rejected on-chain.
{
"type": "order_insert",
"client_tag": "0x1",
"price": 5.25,
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"quantity": 100,
"side": "bid",
"valid_till": 1735689600,
"signature": "0xabcd",
"time_in_force": "GTC"
}order_cancel
Direction: Client → Server
Cancels one resting order. instrument is required.
{
"type": "order_cancel",
"client_tag": "0x1",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000"
}cancel_all
Direction: Client → Server
Sweeps the user's resting orders. instrument required for scope: instrument, product_ptr for scope: product.
{
"type": "cancel_all",
"scope": "all",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"product_ptr": null
}set_dead_mans_switch
Direction: Client → Server
Arms (or re-arms) the dead-man's-switch. timeout_ms: 0 disarms it.
{
"type": "set_dead_mans_switch",
"timeout_ms": 30000
}get_orders
Direction: Client → Server
Lists resting orders; omit instrument for all.
{
"type": "get_orders",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000"
}get_account
Direction: Client → Server
Fetches the session user's balance and margin.
{
"type": "get_account"
}subscribe_account
Direction: Client → Server
Starts account updates. The server sends an initial account snapshot, then account_update after each balance change.
{
"type": "subscribe_account"
}unsubscribe_account
Direction: Client → Server
Stops account updates.
{
"type": "unsubscribe_account"
}get_position
Direction: Client → Server
Fetches the session user's position in one instrument.
{
"type": "get_position",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000"
}get_products
Direction: Client → Server
Lists products; omit product_ptr for all. Reply is always a list.
{
"type": "get_products",
"product_ptr": "0x046b6d51871422655107632c891e0cfaa0e07769"
}heartbeat
Direction: Client → Server
Keep-alive; also resets the dead-man's-switch timer.
{
"type": "heartbeat"
}Server → Client
type | Purpose |
|---|---|
authenticated | authenticate succeeded |
order_ack / order_reject | Order accepted / refused |
cancel_ack / cancel_reject | Cancel accepted / refused |
cancel_all_ack / cancel_all_reject | Sweep result (chunked) |
dead_mans_switch_ack | DMS armed |
heartbeat_ack | Keep-alive reply |
fill | A fill on one of the user's orders |
order_cancelled | A resting order left the book |
account_subscribed / account_unsubscribed | Account subscription state |
orders / account / account_update / position / products | Query or subscription reply |
error | Any failure |
authenticated
Direction: Server → Client
Emitted once authenticate validates.
{
"type": "authenticated",
"user": "0xf9b04d191dadfa68b2dd10e8359f5e5f48aa8b41"
}order_ack
Direction: Server → Client
The venue accepted the order. cancel_reason is present only on self-trade cancellations.
{
"type": "order_ack",
"client_tag": "0x1",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"exchange_ts_micros": 1735689600000000,
"cancel_reason": "self_trade"
}order_reject
Direction: Server → Client
The venue refused the order. Branch on code.
{
"type": "order_reject",
"client_tag": "0x1",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"code": "valid_till_too_soon",
"message": "valid_till within the venue buffer"
}cancel_ack
Direction: Server → Client
The order was cancelled.
{
"type": "cancel_ack",
"client_tag": "0x1",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"executed_quantity": 50,
"exchange_ts_micros": 1735689600000000
}cancel_reject
Direction: Server → Client
The cancel failed. order_not_found is benign (a fill or expiry beat it); invalid_request is a client bug.
{
"type": "cancel_reject",
"client_tag": "0x1",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"code": "order_not_found",
"message": "no such order"
}cancel_all_ack
Direction: Server → Client
Result of a sweep, chunked across frames. The final frame has more: false.
{
"type": "cancel_all_ack",
"scope": "all",
"cancelled": [
{
"client_tag": "0x1",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"executed_quantity": 50
}
],
"exchange_ts_micros": 1735689600000000,
"more": false
}cancel_all_reject
Direction: Server → Client
The sweep request itself failed.
{
"type": "cancel_all_reject",
"code": "insufficient_balance",
"message": "…"
}dead_mans_switch_ack
Direction: Server → Client
Confirms the armed timeout.
{
"type": "dead_mans_switch_ack",
"timeout_ms": 30000
}heartbeat_ack
Direction: Server → Client
Reply to heartbeat; timestamp is unix seconds.
{
"type": "heartbeat_ack",
"timestamp": 1735689600
}fill
Direction: Server → Client
A fill on the user's order, pushed over the private feed.
{
"type": "fill",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"client_tag": "0x1",
"is_maker": false,
"side": "bid",
"price": 5.25,
"quantity": 25,
"cumulative_quantity": 75,
"exchange_ts_micros": 1735689600000000,
"sequence_id": 12
}order_cancelled
Direction: Server → Client
A resting order left the book (expiry, cancel, or self-trade).
{
"type": "order_cancelled",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"client_tag": "0x1",
"reason": "expired",
"cancelled_quantity": 25,
"exchange_ts_micros": 1735689600000000,
"sequence_id": 13
}orders
Direction: Server → Client
Reply to get_orders; same shape as REST GET /orders.
{
"type": "orders",
"user": "0xf9b04d191dadfa68b2dd10e8359f5e5f48aa8b41",
"items": [
{
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"client_tag": "0x1",
"side": "bid",
"price": 5.25,
"quantity": 100,
"valid_till": 1735689600,
"signature": "0xabcd",
"executed_quantity": 50
}
]
}account
Direction: Server → Client
Reply to get_account; same shape as REST GET /account/{user}.
{
"type": "account",
"user": "0xf9b04d191dadfa68b2dd10e8359f5e5f48aa8b41",
"balance": "1000000000000000000",
"margin": "250000000000000000"
}account_subscribed
Direction: Server → Client
Confirms account updates are enabled.
{
"type": "account_subscribed"
}account_unsubscribed
Direction: Server → Client
Confirms account updates are disabled.
{
"type": "account_unsubscribed"
}account_update
Direction: Server → Client
Updated account after a deposit, withdrawal, or settlement.
{
"type": "account_update",
"user": "0xf9b04d191dadfa68b2dd10e8359f5e5f48aa8b41",
"balance": "1100000000000000000",
"margin": "250000000000000000"
}position
Direction: Server → Client
Reply to get_position; same shape as REST GET /account/{user}/positions/....
{
"type": "position",
"user": "0xf9b04d191dadfa68b2dd10e8359f5e5f48aa8b41",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"quantity": "42",
"outstanding_bid_margin": "150",
"outstanding_ask_margin": "275",
"outstanding_bid_quantity": 7,
"outstanding_ask_quantity": 9
}products
Direction: Server → Client
Reply to get_products; always a list.
{
"type": "products",
"items": [
{
"product_ptr": "0x0101010101010101010101010101010101010101",
"initial_cycle_ts": 1767225600,
"cycle_duration_secs": 86400,
"params_list": [
"0x",
"0x00000000000000000000000000000000000000000000003635c9adc5dea000000000000000000000000000000000000000000000000000000de0b6b3a7640000"
],
"pyth_price_ids": [2],
"settler_id": "0x0202020202020202020202020202020202020202",
"name": "ETH-BinaryAboveBelow-Daily"
}
]
}error
Direction: Server → Client
Any failure; see the frame at the top of this file.
{
"type": "error",
"code": "unauthorized",
"message": "not authenticated",
"ref_type": "order_insert"
}Market Data WebSocket (GET /market-data/ws)
unauthenticated (public). Subscribe to instruments to receive a full-book snapshot followed by a delta stream. Sequencing: after subscribe, the server sends subscribed then one in-band depth_snapshot per accepted instrument (its sequence_id is the book's next-expected sequence, so it already reflects every delta with first_seq_id < sequence_id), then depth_delta / trade events. If a subscriber falls behind the broadcast it is lagged and the connection is dropped so it can resync via REST. Inbound text frames are capped at ~4096 bytes at the application layer, instrument ids at 66 chars; the subscription cap is market_data_ws_max_subscriptions (default 32).
Client → Server
type | Purpose |
|---|---|
subscribe | Subscribe to one or more instruments |
unsubscribe | Unsubscribe from instruments |
subscribe
Subscribes to instruments (hex). errors reports per-instrument failures; instrument: "*" means the whole request failed.
{
"type": "subscribe",
"instruments": [
"0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000"
]
}unsubscribe
Unsubscribes from instruments.
{
"type": "unsubscribe",
"instruments": [
"0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000"
]
}Server → Client
type | Purpose |
|---|---|
subscribed / unsubscribed | Subscription state changed |
depth_snapshot | Full-book seed sent once per accepted instrument |
depth_delta | Incremental book update |
trade | A public trade print |
error | Any failure |
subscribed
Acknowledges accepted instruments and reports per-instrument failures.
{
"type": "subscribed",
"active": [
"0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000"
],
"errors": []
}unsubscribed
Acknowledges removed instruments.
{
"type": "unsubscribed",
"active": [],
"errors": []
}depth_snapshot
Full book for one instrument, sent once after subscribed and before any delta. It already reflects every delta with first_seq_id < sequence_id.
{
"type": "depth_snapshot",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"sequence_id": 10,
"bids": [
{
"price": 5.25,
"quantity": 100
}
],
"asks": [
{
"price": 5.3,
"quantity": 50
}
]
}depth_delta
Incremental update. Prices are human dollars; timestamp_micros is unix microseconds.
{
"type": "depth_delta",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"first_seq_id": 10,
"next_seq_id": 11,
"timestamp_micros": 1735689600000000,
"bids": [
{
"price": 5.25,
"quantity": 100
}
],
"asks": []
}trade
A public trade print. side is the taker's side.
{
"type": "trade",
"instrument": "0x046b6d51871422655107632c891e0cfaa0e077690000000000000000000000000000",
"sequence_id": 90,
"timestamp_micros": 1735689600000000,
"side": "bid",
"price": 5.25,
"quantity": 10
}error
Any failure, e.g. an over-limit request or malformed frame.
{
"type": "error",
"code": "invalid_request",
"message": "message too large"
}API Reference
REST and WebSocket APIs for trading and market data.
Cancel all orders DELETE
Cancels every resting order of a user within the requested scope: `all`, a single `instrument`, or a whole `product`. Cancelling zero orders succeeds with an empty `cancelled` list. Requires an `Authorization: Bearer` SIWE token proving control of `user`; otherwise 401.