Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.0xarchive.io/llms.txt

Use this file to discover all available pages before exploring further.

This is the WebSocket connection-start checklist. WebSocket Connection remains the broader connection reference. Connect to WebSocket when the workflow needs live messages or replay control. Use REST for a one-off snapshot or bounded historical pull where streaming order does not matter.

Minimal Connection

const ws = new WebSocket("wss://api.0xarchive.io/ws?apiKey=0xa_your_api_key");

ws.onopen = () => {
  ws.send(JSON.stringify({
    op: "subscribe",
    channel: "orderbook",
    symbol: "BTC"
  }));
};
This example is useful for a smoke test. A production client should keep the API key in a backend or secret-managed process, not in browser logs, copied URLs, or prompt transcripts.

Connection Checklist

1

Choose stream or replay

Decide whether the job needs live subscription messages, replay, or REST history before opening the socket.
2

Open with auth

Pass the API key through the environment or backend-controlled connection path.
3

Subscribe after open

Send subscription or replay commands only after the socket reaches the open state.
4

Handle every event class

Implement open, message, error, close, keep-alive, reconnect, and unsubscribe behavior.
5

Log connection state

Preserve active channels, symbols, retry count, close code, close reason, and restored subscriptions.

What To Decide Before Code

DecisionWhy
Channel familyPrevents core, Spot, HIP-3, HIP-4, and Lighter mixups
Symbol formatKeeps pair symbols, builder prefixes, and outcome identifiers intact
Consumer capacityPrevents message backlog and stale local state
Reconnect policyPrevents tight reconnect loops
Gap policyDecides whether a downstream job can continue

Connection State To Track

Keep connection state in one object rather than scattering it across callbacks.
FieldStore
statusconnecting, open, closing, closed, reconnecting, or failed
activeSubscriptionschannel-symbol pairs that should be restored after reconnect
lastMessageAtlatest server message timestamp seen by the client
retryCountreconnect attempts for the current outage
closeclose code, reason, and whether the close was expected
unsafeStatelocal books, alerts, or replay outputs that need rebuild after a gap
This state makes the reconnect path deterministic. After a new socket opens, restore only the tracked subscriptions, restart keep-alive timers, and rebuild any local state marked unsafe.

Secret Handling Pattern

The URL example is a minimal smoke test. For shared tools, browser apps, and agent workflows, put the key in a backend-controlled process or secret store and keep it out of copied URLs. If an agent is generating code, ask it to show where the key is read, where the URL is constructed, and which logs are safe to keep.

Next Step

Use WebSocket Channels to choose subscriptions, Keep Alive for liveness and reconnect policy, and WebSocket Limits before scaling channel count.

Production Notes

Treat the first successful open event as a connection start, not as evidence that downstream state is usable. The client still needs an acknowledged subscription, message parser, heartbeat policy, reconnect behavior, and gap handling. For local book workflows, rebuild from a known snapshot when reconnects or missed messages make state ambiguous.
Last modified on May 18, 2026