Skip to main content
This guide builds a candlestick chart for one token that loads its history over REST and then updates as trades happen. It uses two endpoints: Both build candles from the same pool and the same trades, so live updates continue the REST series. The examples follow WETH on Base (0x4200000000000000000000000000000000000006) with 1-minute candles. They run on Node.js 22 or later, which has fetch and WebSocket built in.

How the pieces fit

Both accept the same periods: 1s, 5s, 15s, 30s, 1m, 5m, 15m, 30m, 1h, 4h, 6h, 12h, 1d, 1w, 1M. 1m is one minute and 1M is one calendar month. All buckets are aligned to UTC, and 1w buckets start on Monday. t is the bucket start in Unix milliseconds on both sides, so it is the key you merge on. Prices are in USD. Use the evm:8453 form everywhere. The WebSocket API rejects a bare 8453.

Steps

1

Subscribe to live candles first

Open the socket and subscribe before you fetch history. Keep any events that arrive while the REST request runs, so nothing is lost between the two.
The server answers with subscribed and a cursor. If the token has no pool, it answers with an error of code TOKEN_NOT_FOUND and message Token or pool not found.
2

Load history

  • limit is 1 to 2000 candles (default 500). Without from, the request covers the last limit periods up to to (default now), so you get at most limit candles from that window.
  • fill=true inserts flat candles (o = h = l = c = previous close, v = 0) for buckets without trades between the first and last candle. Without it, those buckets are missing.
  • Candles come back oldest first. The last one is the newest bucket that had a trade, which is the bucket in progress if a trade already happened in it.
3

Merge live updates by t

Each ohlcv event carries the whole current candle, not a delta. Merge it by t:
  • Same t as your last candle: replace the last candle.
  • Greater t: a new bucket started; append it.
  • Smaller t: ignore it.
Apply the buffered events first, then every event as it arrives.
4

Load older candles on scroll

To page back, request the same period with to set to one millisecond before your oldest candle:
Here the oldest candle has t = 1796306580000, so to is 1796306579999 (candles[0].t - 1). Prepend the result. The request only looks back limit periods before to, so an empty array means there were no trades in that window, not necessarily that there is no older data. To keep going back, move to further back or switch to a longer period.

Full example

In the Python version, events that arrive while history is loading wait in the socket’s receive queue and are merged afterwards, so no separate buffer is needed.

Reconnects, gaps and expired cursors

  • Reconnect. Keep the newest cursor from events and from cursor checkpoints (sent every 30 seconds). After a disconnect, subscribe again with since set to it. The server replays the updates you missed, then continues live. See Cursors and resume.
  • RESUME_EXPIRED. The stream keeps about 10 minutes of history. If you were away longer, the subscription is refused. Subscribe again without since and reload the candles from REST.
  • gap. If the stream had to skip blocks, you get a gap message with an inclusive fromBlock..toBlock range. Trades in those blocks never reach the channel, so your last candles may be incomplete. Reload the recent candles from REST, which reads from storage.
  • Restarts. A {"type":"reconnect"} message followed by close code 1012 means the server is restarting. Reconnect and resume as above. See Limits and errors.

Things to know

  • Which pool. For a token, both endpoints use the token’s best pool: the one with the highest 24-hour volume. The REST endpoint picks it on every request; the live channel re-checks it at most once a minute. Each event’s data.pool tells you which pool it came from. If it changes, the best pool changed, so reload history from REST. To follow one pool regardless, use GET /market/ohlcv and subscribe with pool instead of token.
  • Launchpad tokens. For a token that graduated from a bonding curve, REST history can include older candles from the curve before graduation. Live updates only come from the current pool.
  • Updates come with trades. The channel sends a candle only after a trade changes it. Quiet buckets produce no event, and a new bucket opens at the previous close when the next trade arrives. If you want flat candles for quiet periods, like fill=true does, insert them yourself between the last candle and the new one.
  • Not every trade produces an event. No event is sent until the candle has an opening price, and an update can be skipped if the server is slow to compute it. The next event carries the full candle again, so replacing by t keeps the chart correct.
  • Too many updates. For a busy token, add maxUpdatesPerMinute (1 to 600) to the subscription params to sample updates. Sampled subscriptions have no cursors and can’t resume, so on reconnect subscribe again without since and reload recent candles from REST.
  • Sub-minute periods. For 1s to 30s, REST looks back at most limit × period before to.