Skip to main content
Connect when you need streaming data or replay control. Use REST for one-off snapshots. WebSocket uses the same 0xArchive API-key credential as other clients, but supplies it during connection setup rather than through the REST X-API-Key header. Use API keys and credential safety for creation, storage, and rotation.
These query-string examples are for server-side smoke tests and private scripts. Do not paste a real API key into browser code, public prompts, logs, screenshots, or shared notebooks; browser apps should connect through your backend.
1

Open the socket

2

Subscribe after open

3

Parse every message

4

Reconnect with backoff

Reconnect after close or network failure with capped backoff. Resubscribe only after the new socket opens.

Before you connect

Connect to wss://api.0xarchive.io/ws. For server-side clients, prefer an Authorization: Bearer <api-key> handshake when the WebSocket library supports custom headers. The query-string API-key form remains a compatibility path for private server-side scripts; never expose it in browser code, logs, telemetry, or copied prompts. Decide these before you open the socket.

Keep Alive

The application {"op":"ping"} command and {"type":"pong"} response can measure one client’s application round-trip time. Treat that as a client-side observation, not a published platform latency percentile.

Reconnect Policy

Reconnect with capped exponential backoff and jitter. Do not reconnect in a tight loop. After a new socket opens, resubscribe explicitly and rebuild any local state that depended on the previous stream. If the stream feeds a local book, replay window, alert, or model, treat the reconnect as a state transition that needs logging.

Auth And Secrets

For server-side clients, prefer an Authorization: Bearer <api-key> handshake when the WebSocket library supports custom headers. The query-string apiKey form remains a compatibility path for private server-side scripts. Load the key from the environment or application secret store, and never expose it in browser URLs, logs, telemetry, or copied prompts. Browser clients should connect through your backend.

Message Loop

Parse every message defensively. Expected classes include subscription acknowledgements, snapshots, updates, replay messages, pings or pongs, gap signals, and errors. Preserve any request or correlation identifiers that appear in the message so a streaming issue can be tied back to the connection session.

Connection State Object

Use a local state object so reconnects and support logs describe the same stream.
Update it inside onopen, onmessage, onerror, and onclose. When the socket closes, clear old timers, move the status to reconnecting, open a new socket with backoff, then restore subscriptions from activeSubscriptions. If a gap arrives for a stateful channel, add the symbol to unsafeSymbols until the client rebuilds from a snapshot or replay checkpoint.

Failure Classes

Review Rule

Connection examples should include close and error handling before they are copied into an application that maintains state. The reconnect path should recreate subscriptions from tracked state, not by replaying stale assumptions. Log close code, reason, retry count, and restored subscriptions so support can distinguish normal network churn from a broken consumer or bad auth state.
Last modified on August 13, 2026