Sign in
← Back to the blog

How to trigger an AI agent when an email arrives

How does an AI agent start work the moment an email lands, with or without a public URL?

The short answer

Subscribe a webhook to message.received, verify its signature, answer 2xx at once and hand the message id to your agent. With Zurchin the event is signed with HMAC-SHA256 and retried up to five more times if your server is down. If your agent has no public URL, it makes one call instead, which holds open until mail lands at its address.

Two ways to be told

An agent that reads email needs something to wake it up. There are two shapes, and the right one depends on where your code runs:

  • A webhook. Zurchin sends a POST to your URL when something happens. Your code sleeps until then. This is the shape for a server, a serverless function or a workflow tool, anything with an HTTPS address the internet can reach.
  • A wait. Your code asks, and the answer comes back when the mail does. This is the shape for a script on a laptop, a notebook, a browser agent or an agent runtime with no public address.

Both hand you the same message, so you can start with the wait while you build and move to a webhook without changing what your agent does with it.

The webhook

Register an endpoint with POST /v1/webhooks (or the CLI, or the MCP server) and name the events you act on, here message.received. It needs a public https:// address.

Zurchin shows the signing secret once. Store it.

What arrives is ids and metadata, never the body: the inbox, message and thread ids, the sender, the subject, whether it was marked spam, and the Firewall verdict. Your handler fetches the text by the message's id when the agent needs it, so the mail stays out of your request logs.

Check two things before your agent acts. If the sender is one of your own inboxes, an agent that answers every message.received will answer its own mail. And if a person has taken the conversation (its owner is human), fetch the thread and leave it alone.

Other events are worth a trigger too. message.held fires instead of message.received when the Firewall holds mail for a person, thread.updated when a conversation changes owner or status, and message.bounced when the receiving server refuses something your agent sent.

Verify, then acknowledge

An unverified webhook endpoint is an open POST that says mail arrived. Anyone who finds the URL can tell your agent what to do. Every delivery carries Pidgeon-Signature: t=<unix seconds>,v1=<hex>, where v1 is an HMAC-SHA256 over the timestamp, a full stop and the raw body, keyed with the endpoint's secret. Both SDKs, @zurchin/sdk and pidgeon in Python, check it for you.

Verify the raw text before you parse it, because re-serialised JSON no longer matches the signature. A timestamp more than five minutes old is refused as a replay.

Then return. A model call takes seconds, and Zurchin gives your endpoint 10 seconds before it counts the attempt as failed and tries again. Put the work on a queue and answer 204.

Retries and what they mean for your handler

A delivery that fails is retried up to five times, with a longer wait each time, and Zurchin records whether each delivery arrived or failed.

Delivery is at least once. If your handler finished and the connection dropped before Zurchin saw the 2xx, the event comes again. Every event has a stable id in its body, the same as in the event log, and its type is sent as the Pidgeon-Event header. Record the ids you have handled and return early on a repeat, or a retry becomes a second reply from your agent.

No public URL: wait for the mail

A webhook needs an HTTPS address that is not private or loopback, so localhost cannot receive one. For an agent without a public address, one call holds open until a message arrives at an inbox, for up to 50 seconds. It answers with the new mail and a next_since; call again from that, and nothing that arrived between the two calls is missed. Mail held for a person is not returned, only counted.

An agent on the MCP server can wait the same way, with await_message, and chain the waits the same way.

Test it before anything is real

A test key runs everything a live key runs, including events and webhooks, and no mail leaves Zurchin. With it, Zurchin can play the sender: it delivers a message to one of your inboxes through the real receiving pipeline, with threading, rules and the Firewall, so your webhook gets the same message.received a stranger's email would produce, as a new conversation or as a reply in an existing thread.

That makes the whole loop testable in CI: an inbound message, your verified handler, your agent's reply, and a second inbound message answering it. To test your handler's retry path, return a 500 once and watch the delivery come back.

When this is not the right tool

Zurchin's events cover mail that arrives at a Zurchin address. If the mail your agent should act on lands in a person's existing inbox at another provider, Zurchin never sees it unless it is forwarded to a Zurchin address. And if your code only needs to know about mail once a day, a scheduled read of the event log (GET /v1/events, kept for 90 days) is simpler than either a webhook or a wait.

Start

Sign up, create an inbox, and register a webhook. Everything an agent's inbox does is on Zurchin Email.

Questions

Which webhook event fires when an email arrives?

message.received. It carries the inbox, message and thread ids, the sender, the subject, the spam flag and the Firewall verdict, never the body; fetch the text by the message's id.

How do I verify a Zurchin webhook?

Check the Pidgeon-Signature header: its v1 is an HMAC-SHA256 over its t, a full stop and the raw body, keyed with the endpoint's secret. Both SDKs do it, and refuse anything older than five minutes.

What if my agent has no public URL?

Make the call that waits. It holds the request open until mail arrives at the address, and returns a cursor so the next call misses nothing.

How do I test it before production?

Use a test key and have Zurchin play the sender. It delivers a message through the real inbound pipeline, so your webhook receives message.received, and no mail leaves Zurchin.