Integrate
Realtime (WebSocket)
Stream simail events over a WebSocket from a server-side agent; subscribing, limits, revocation and reconnecting without gaps.
docs.simail.ai/realtime
Long-running agents can keep one connection open and receive events as they happen.
wss://api.simail.ai/v0/wsNeeds event_read. Authenticate the upgrade request with Authorization: Bearer <key>. This is for
server-side agents only: never put an API key in a browser.
import WebSocket from "ws";
const ws = new WebSocket("wss://api.simail.ai/v0/ws", { headers: { Authorization: `Bearer ${process.env.SIMAIL_API_KEY}` },});ws.on("open", () => { ws.send(JSON.stringify({ type: "subscribe", inbox_ids: ["shop-helper@simail.email"], event_types: ["message.received", "thread.locked"] }));});ws.on("message", (raw) => { const msg = JSON.parse(raw.toString()); if (msg.type === "event") handle(msg.event); // de-duplicate on msg.event.event_id});ws.on("close", (code) => scheduleReconnect(code));Messages
Section titled “Messages”Client to server, optional, any time:
{ "type": "subscribe", "inbox_ids": ["shop-helper@simail.email"], "event_types": ["message.received"], "include_non_wake": false }A subscription can only narrow what your key may see. Without one you get every event in your key’s scope that woke your agent.
Narrowing is cumulative: each subscribe is intersected with what the connection currently has, not with
what your key allows. After you subscribe to message.received, a later subscribe that adds message.sent (or another
inbox) is silently ignored for that part. To widen again, close the socket and reconnect. include_non_wake is a
delivery preference, not a narrowing, so you can switch it at any time.
The server answers every subscribe with a confirmation frame:
{ "type": "subscribed", "inbox_ids": ["inb_…"], "event_types": ["message.received"], "include_non_wake": false, "dropped_inbox_ids": ["unknown@simail.email"] }inbox_ids is what the connection now receives (inbox IDs, or "all" when the key covers everything), event_types and include_non_wake are the current values, and dropped_inbox_ids (present only when something was dropped) echoes the requested entries that were not in your scope, not found, or not valid. Check it: an address that was dropped will never deliver events.
Server to client:
{ "type": "event", "event": { "event_id": "evt_…", "event_type": "message.received", "occurred_at": "2026-10-18T17:02:12Z", "inbox_id": "shop-helper@simail.email", "thread_id": "thr_…", "message_id": "msg_…", "data": { "wake": true } } }The event object is the same thin payload as everywhere else; see Event types.
Inbound events that did not wake your agent (message.received, message.received.spam, message.received.blocked, message.held) are skipped unless you subscribe with include_non_wake: true. Other event types are not affected by that flag.
Lifetime and limits
Section titled “Lifetime and limits”- A connection lives at most 1 hour (less if the key expires sooner). Then it closes with code
1000(expired) and you reconnect, which re-checks the key. - Revoking or narrowing the key, removing the member, signing out a console session or pausing the
organization closes affected connections at once with code
4401(revoked). - 50 connections per organization, 10 per key, 10 handshakes per minute.
- A client that falls more than 1 MiB behind is disconnected with
1008(slow); a socket the server cannot write to closes with1011. - Pings are answered by the server without waking anything; idle connections are cheap.
Reconnecting without gaps
Section titled “Reconnecting without gaps”The WebSocket is a fast path, not a log. To never miss an event, remember the event_id of the last
event you handled and, after reconnecting, catch up with GET /v0/events using your stored
cursor. De-duplicate on event_id. Back off between reconnects (for example 1 s, 2 s, 4 s … up to
60 s) and do not keep reconnecting after 4401 with the same key: it was revoked or narrowed. Call GET /v0/auth/me to see whether the key still works before you try again. 1000, 1008 and 1011 are safe to reconnect after.