GET /api/v1/events/stream is a WebSocket that pushes mail events as they happen. GET /api/v1/events/sse is the same stream as Server-Sent Events, for clients that cannot open a WebSocket.
The events are the webhook events, with the same JSON payloads:
| Type | When |
|---|---|
message.received |
Inbound mail was stored |
message.sent |
A message was handed to the provider |
message.delivery_failed |
The provider refused or failed it |
message.bounced |
A bounce came back |
message.opened / message.clicked |
Tracking recorded an open or a click |
Webhooks remain the durable channel: a webhook delivery is retried for hours. The stream is for clients that are connected and want low latency. It keeps 24 hours of events, so a client that drops can catch up.
- Use the usual credentials:
Authorization: Bearer ttk_…or the session cookie. Browsers can't set headers on a WebSocket, so the web app uses the cookie. - An API token needs the
mail:readscope. - You only receive events for mailboxes you can read. That means your grant, intersected with the token's mailbox list, with
readnot revoked. This is checked for every event, whatever you subscribed to. - Authorization is re-checked while the connection is open, not only when it opens:
- The object polls the auth epoch every 5 seconds, the same counter that bounds the request path's credential cache. When the epoch changes, every session and token is re-validated. They're also re-validated every 30 seconds regardless. Each distinct credential is checked once, however many connections share it.
- A revoked or expired session or token, or a disabled user, gets
{"type":"credential_revoked"}and a close with code4401. - Mailbox grants are re-read on the same cadence. A mailbox you can no longer read gets
{"type":"subscription_revoked","mailbox":…}, and its events stop. The connection stays open for the others. - If the check itself can't run (the auth store is unreachable), every connection gets
{"type":"auth_unavailable"}and a close with code4503. Reconnect withsince.
- Every connection is also closed with code
4001after an hour, or when its token expires, so a client re-authenticates at least that often. Cloudflare Access callers have no server-side session to re-check, so for them that hour is the bound, while their mailbox grants are still re-read every 30 seconds.
Connect, then subscribe:
{ "type": "subscribe", "mailboxes": ["[email protected]"], "events": ["message.received"] }- Omit
mailboxes(or pass[]) for every mailbox you can read. - Omit
eventsfor every type. - Mailboxes you can't read are left out and listed in
denied. They're never widened to anything else. - Sending
subscribeagain replaces the subscription.{"type":"unsubscribe"}stops events, and{"type":"ping"}gets apong.
Server frames:
| Frame | Meaning |
|---|---|
{"type":"hello","latest_id":N,"oldest_id":M,"readable":[…] | "all","expires_at":ms,"limits":{…}} |
Sent on connect |
{"type":"subscribed","mailboxes":[…] | "all","events":[…] | "all","denied":[…]} |
Your subscription |
{"type":"event","id":N,"event":{…webhook payload…}} |
One event |
{"type":"replayed","count":N,"latest_id":N} |
Replay finished |
{"type":"replay_truncated","resume_from":N} |
More than 1000 to replay; reconnect with since=resume_from |
{"type":"cursor_expired","since":N,"oldest_id":M} |
Some events after your cursor are older than 24 hours and gone |
{"type":"overflow","resume_from":N} |
You fell behind; the server closes with 1013 |
{"type":"subscription_revoked","mailbox":…} |
You can no longer read that mailbox; its events stop |
{"type":"credential_revoked"} |
Your session or token was revoked or expired; the server closes with 4401 |
{"type":"auth_unavailable"} |
Authorization couldn't be re-checked; the server closes with 4503. Reconnect with since |
{"type":"error","code":…,"error":…} |
A bad message from you; the connection stays open |
Event ids are monotonic across the whole deployment. Keep the highest id you've processed.
?since=<id> on the URL (or "since" in the subscribe message) replays every retained event after that id that matches your subscription, then continues live. For SSE, the browser's automatic Last-Event-ID header does the same.
let last = Number(localStorage.getItem("events:last") ?? 0); function connect() { const ws = new WebSocket(`wss://mail.example.com/api/v1/events/stream?since=${last}`); ws.onopen = () => ws.send(JSON.stringify({ type: "subscribe", events: ["message.received"] })); ws.onmessage = (m) => { const frame = JSON.parse(m.data); if (frame.type === "event") { handle(frame.event); last = frame.id; localStorage.setItem("events:last", String(last)); } }; ws.onclose = () => setTimeout(connect, 2000); } connect();
curl -N -H "Authorization: Bearer $CLOUDMAIL_TOKEN" \ "https://mail.example.com/api/v1/events/[email protected]&event=message.received"
Each event arrives as id: N, event: <type>, data: <webhook payload>. Repeat mailbox and event to filter. A comment line every 25 seconds keeps proxies from closing the connection.
- Each user can hold 5 open connections (WebSocket and SSE together). The 6th is refused with
429. - A deployment can hold 500 connections. Past that, new connections get
503. - A subscription can name up to 100 mailboxes.
- Client messages are capped at 16 KiB.
- Each connection has a send budget of 500 events in a burst, refilling at 100 a second. A WebSocket that exceeds it, or has more than 1 MiB queued, gets an
overflowframe withresume_fromand is closed with1013. A full SSE stream is closed the same way. The client reconnects withsinceand replays, so slow clients catch up from the log rather than being buffered in memory.
cloudmail events tail --mailbox [email protected] cloudmail events tail --event message.received --output ndjson | jq .event.data.subject cloudmail events tail --since 1042
tail reconnects on its own and resumes from the last id it printed. It stops on an auth or scope error, since a retry can't fix those.
The stream needs the EVENTS Durable Object binding (EventsDO, migration v7 in wrangler.jsonc), which wrangler deploy creates. It needs no secret or variable. Without the binding, /api/v1/events/* answers 503 and mail handling is unaffected.
There is one EventsDO per deployment. MailboxDO feeds it from the same delivery ledger that feeds webhooks, but with its own cursor, so turning the stream on or off never affects webhook delivery. The inbound handler publishes message.received directly. WebSockets use the hibernation API, so idle connections don't keep the object in memory.
/api/v1/openapi.json lists both endpoints with their parameters and status codes. OpenAPI 3.1 can't describe the message exchange over a WebSocket, so the frames above are documented here only.