Connect
Authorization header on a WebSocket. A browser instead passes metastreams.v1 and a session token as subprotocols, and the server selects metastreams.v1. An API key is not accepted in the subprotocol list.
Browser
Routes
A connection can use one of two routes. Frames, limits and billing are the same on both.
The direct route connects to the server with no proxy between. The connect request costs no credit, and one token opens connections, within the key’s connection limit, until it expires. Browsers and session tokens use the proxied route only.
404 NOT_FOUND, no direct URL is available; use the proxied route. When it or the direct handshake fails for another reason, the proxied route is a safe fallback. A 401, 402, 403 or 429 is returned on both routes, so do not retry it on the other one.
Neither route is faster from every location. Measure both from where your client runs. The Rust SDK takes the direct route by default and falls back to the proxied route on its own.
Channels
?marks an optional key, so{}matches every trade, transfer, candle or token, and…means the channel takes more keys. Each channel page lists all of them.spot.trades,spot.transfers,spot.candlesandspot.tokensshare one filter language, described in Filtering streams.spot.tokensalso sends anexitframe when a token stops matching.spot.tokens,spot.candlesandspot.positionsacceptthrottleMsalongsidefilters, up to 60,000. Each channel page gives its default.spot.candlesalso acceptsmetric,denominationandclosedOnly.- No REST endpoint returns transfers. Every other
dataobject has the same shape as the matching REST response, with two exceptions. A streamed candle also includes itschain,token,poolandtimeframe. A streamed holder has norank. A streamed position is priced the same way asGET /v1/positions.
Frames
Client messages
Server messages
- Use
subscriptionIdto route updates and to unsubscribe. The subscribe ack returns it, and every update includes it. - The ack for a subscription always arrives before its first update, and the ack for an unsubscribe arrives after that subscription’s last update.
Frame timing
Addtiming=true to the handshake URL, such as wss://api.metastreams.dev/v1/stream?timing=true, to measure delivery latency. Every update frame on that connection then includes a timing object beside data. Other frames never include it, and it costs no credit.
sentAt minus receivedAt is the server’s processing time, plus any delay that throttleMs or closedOnly adds. Compare sentAt with the time your client reads the frame to measure the network leg. The two clocks are not synchronized, so treat a small negative difference as zero. The parameter works on both routes.
Initial snapshot
A stream only sends changes that happen after you subscribe.spot.trending and spot.discovery are the exceptions: their first updates contain the full board. To build a complete view on any other channel:
- Subscribe and buffer the updates.
- Read the snapshot from the REST endpoint, with the parameters that match the stream:
hideLowLiquidity=falseonGET /v1/positions, because the stream includes low-liquidity positions, and the subscription’speriod,lifetimeby default, onGET /v1/holders, with alimitof up to 150. - Apply the buffered updates, and then each new update, on top of the snapshot.
A subscription survives a server-side interruption, but missed updates are not replayed and no frame marks the gap. If you need every row, also re-read the snapshot on a schedule.
Errors
When a frame is rejected, the server returns anerror frame with a REST error code, and the connection stays open. A rejected frame’s error has no subscriptionId.
An error frame with a subscriptionId ends that subscription and frees its slot and addresses. No public channel sends one, but a client can handle it: drop the subscription’s state, and subscribe again only when the code is INTERNAL_ERROR or SERVICE_UNAVAILABLE.
The error includes the
id of the frame that caused it. It has no id when that frame is not JSON, has no string id, or is an unsubscribe.
Heartbeat
Send{"action": "ping"} about every 30 seconds. Reconnect when no pong has arrived for 45 seconds. The examples below check this at each ping. The server never closes an idle connection and sends no pings of its own.
Close codes
Subscriptions do not survive a reconnect. Send your subscribe frames again, then fill the gap with a fresh snapshot.
Reconnection
The examples below resubscribe after every close. They reconnect immediately after1001, back off after any other close, and stop on 401, 402, 403, 1008, 4402 or 4403. They connect with an API key. A browser client with a session token requests a new token from its backend and reconnects after a 1008 close with the reason token expired.