WebSocket streams
Subscribe to market data and Trading Account events, then resnapshot whenever continuity is lost.
Connect to market data
Use wss://paper.mariex.com/public/stream for combined streams, or /public/ws/{streamName} for a single raw stream. The corresponding /market/stream and /market/ws/{streamName} paths serve the same supported topics. Unrouted /stream and /ws paths are not served.
Stream names use lowercase symbols. Combined messages contain stream and data fields. A raw single-stream connection receives the payload directly.
const stream = new WebSocket("wss://paper.mariex.com/public/stream");
stream.addEventListener("open", () => {
stream.send(JSON.stringify({
method: "SUBSCRIBE",
params: ["btcusdt@bookTicker"],
id: 1,
}));
});
stream.addEventListener("message", event => {
console.log(JSON.parse(event.data));
});Choose a supported topic
Higher candle intervals are available through REST, not WebSocket subscriptions. Market-wide !bookTicker, !markPrice@arr, !markPrice@arr@1s and !forceOrder@arr are also supported.
Bootstrap a full depth stream with a REST depth snapshot and reconcile its update IDs before applying buffered events. If the pu chain breaks, discard the local book and start again with a new snapshot.
| Topic | Use |
|---|---|
| {symbol}@bookTicker | Best bid and ask updates |
| {symbol}@depth or @depth@100ms | Incremental order-book updates |
| {symbol}@depth5, @depth10 or @depth20 | Partial book snapshots; optional @100ms |
| {symbol}@aggTrade | Aggregated local fills |
| {symbol}@kline_1m | Current one-minute candle |
| {symbol}@markPrice or @markPrice@1s | Mark price and funding updates |
| {symbol}@forceOrder | Liquidation events |
Listen to Trading Account updates
Create a Listen Key with POST /fapi/v1/listenKey and your X-MBX-APIKEY header. This operation does not need a signature or timestamp. Connect to wss://paper.mariex.com/private/ws/{listenKey} using the returned bearer.
Each API Credential has its own active Listen Key. POST creates or extends it to 60 minutes; PUT renews the active key before expiry; DELETE removes it and closes its sockets. A supplied listenKey parameter does not select another credential's key.
Events include ORDER_TRADE_UPDATE, ACCOUNT_UPDATE, ACCOUNT_CONFIG_UPDATE and listenKeyExpired. Keep Listen Keys out of logs and public URLs you share.
Recover after every reconnect
Private events have at-most-once delivery. There is no event replay, durable sequence or gap cursor. A quiet stream does not prove that no state change occurred.
After every reconnect, refresh account, positions and open orders through REST. Treat events as change notifications rather than a complete account ledger. Persisted order and fill history may lag current state.
| Signal | Client action |
|---|---|
| 4001: resnapshot required | Reconnect and fetch fresh REST state before continuing. |
| 4002 or listenKeyExpired | Check credential authority, POST a Listen Key, reconnect and resnapshot. |
| Disconnect or network timeout | Reconnect with backoff and resnapshot even if no expiry event arrived. |
Respect connection limits
- Respond to server Ping frames. Pings arrive every 30 seconds; a connection closes 35 seconds after its last Pong.
- Plan to reconnect before the 24-hour maximum connection lifetime, then resnapshot.
- A public connection supports up to 200 streams. The inbound limit is 10 frames per second, including Pong frames.
- Private connections are limited to 10 per Trading Account. The Paper ingress also limits WebSocket connections to 20 per client IP.