Webhooks Overview
Get real-time notifications for email events.
Webhooks are the best way to get real-time information about what’s happening with your emails. Instead of constantly asking the AgentMail API if there’s a new email (a process called polling), you can register a URL, and we will send you a POST request with the details as soon as an event happens.
This event-driven approach is more efficient and allows you to build fast, responsive agents that can react instantly to incoming messages.
Why Use Webhooks?
- Real-Time Speed: Build conversational agents that can reply to incoming emails in seconds.
- Efficiency: Eliminates the need for constant polling, which saves you computational resources and simplifies your application logic.
If you don’t want to expose a public URL or set up ngrok, WebSockets let you receive the same events over a persistent connection with no external tooling required. If your agent cannot stay connected either, a polling webhook queues the events for you to fetch whenever it runs.
Available Events
AgentMail supports twenty webhook event types. When creating a webhook, you list the event types it should receive in event_types. The field is required and must name at least one type: there is no “all events” default, and a create request without it returns a 400. See Webhook Events for full payload details.
Message events:
message.received— New email received and processed in one of your inboxesmessage.received.spam— A message was received and classified as spam (requireslabel_spam_readpermission)message.received.blocked— A message was received and matched a block list entry (requireslabel_blocked_readpermission)message.received.unauthenticated— A message was delivered with theunauthenticatedlabel: none of SPF, DKIM, or DMARC passed, or DMARC failed under the sender’snonepolicy (requireslabel_unauthenticated_readpermission)message.sent— Message successfully sent from your inboxmessage.delivered— Delivery confirmed by the recipient’s mail servermessage.bounced— Message failed to deliver and bounced backmessage.complained— Recipient marked your message as spammessage.rejected— Message rejected before send (validation or policy)message.opened— A tracked message was opened for the first time (requires open tracking)message.security.completed— Agent Armor reached a verdict on a received message, in Monitor mode (requiresmessage_read)message.security.review— Agent Armor in Hold mode held a received message for review (requiresmessage_read)message.security.override— someone released a message Agent Armor held (requiresmessage_read)
Domain events:
domain.verified— Custom domain successfully verified
Calendar events (require calendar_event_read; see Calendar Webhooks):
calendar.event.created— A calendar event was created, or a date of a recurring event was edited for the first timecalendar.event.updated— A calendar event, or one or more dates of a recurring event, changed or were cancelledcalendar.event.deleted— A one-off or recurring calendar event was deletedcalendar.event.responded— An attendee’s response to an invitation changedcalendar.event.starting— A calendar event, or one date of a recurring event, startedcalendar.event.ending— A calendar event, or one date of a recurring event, ended
Spam, blocked, and unauthenticated events are excluded by default. To receive them, explicitly include message.received.spam, message.received.blocked, or message.received.unauthenticated in the event_types list when creating a webhook. These messages are no longer sent as message.received.
Updating event_types on a webhook
When you update a webhook, you can change which events it receives by sending event_types on PATCH:
- Non-empty array: Replaces the webhook’s subscribed event types in full (same idea as create: you set the whole list, not a diff). If you send only one type, the webhook will only receive that type afterward.
- Omitted: Leaves the current event types unchanged.
- Empty array: Rejected with a
400. To keep the current event types, omit the field instead. A webhook always has at least one event type; to stop all deliveries, setenabledtofalseor delete the webhook.
Subscribing to message.received.spam, message.received.blocked, or message.received.unauthenticated on update still requires the same label permissions as on create.
Sending "enabled": true re-enables a disabled webhook, including one disabled after repeated failed deliveries (see Retries and disabled endpoints). Re-enabling needs the read permission for every restricted event type the webhook subscribes to: label_spam_read, label_blocked_read, or label_unauthenticated_read for the received variants, message_read for message.security.*, and calendar_event_read for calendar.event.*.
Custom delivery headers
Add custom HTTP headers when you create a webhook to authenticate AgentMail deliveries with your endpoint or pass routing metadata. The values are write-only: AgentMail sends them with each delivery but never returns them in a webhook response.
Use get_headers to list the configured header names without exposing their values. Use the separate update_headers method to rotate values, add keys, and remove keys atomically. An update must set at least one header or name at least one header to remove, and the same header cannot appear in both operations regardless of casing.
The same headers create field and get_headers / update_headers methods are available under pods.webhooks and inboxes.webhooks. Store header values in a secret manager and rotate them with update_headers; retrieving the configured names cannot recover a lost value.
Scoping a webhook to a pod or inbox
By default a webhook is registered at the organization level. You can limit it to a pod or inbox in two ways. Both receive the same events; they differ in who owns the webhook:
- From the organization endpoint: pass
pod_idsorinbox_idswhen youwebhooks.create(and add or remove them later withadd_pod_ids/add_inbox_idson update). The webhook still belongs to the organization, so pod- and inbox-scoped API keys cannot see or change it. Use this when one webhook should cover several pods, or when the pod’s own users should not manage it. - From the scoped endpoints: call
pods.webhooks.create(pod_id, ...)orinboxes.webhooks.create(inbox_id, ...). The pod or inbox comes from the path, so you don’t passpod_ids/inbox_idsin the body. A pod-scoped webhook receives events for the whole pod (optionally narrowed to specific inboxes withinbox_ids); an inbox-scoped webhook is fixed to that inbox: it can change itsevent_typesandenabled, but not which inboxes or pods it covers. The webhook belongs to that pod or inbox.
The scoped endpoints are the natural fit for pod- or inbox-scoped API keys: such a key can manage only its own webhooks, and a scoped webhook must always keep at least one pod or inbox subscription.
A webhook receives an event that matches any of its pods or inboxes. A pod covers every inbox in it, so adding one of that pod’s inboxes does not narrow the webhook. To receive only specific inboxes, create the webhook with inbox_ids and no pod. A webhook can list at most 10 pods and inboxes combined; for example, 6 inboxes and 6 pods is rejected with limit_exceeded.
The Webhook Workflow
The process is straightforward:
1. Create a Webhook Endpoint
This is a public URL on your server that can accept POST requests. For local development, a tool like ngrok is perfect for creating a secure, public URL that tunnels to your local machine. Your endpoint should immediately return a 200 OK response to acknowledge receipt and process the payload in the background to avoid timeouts.
2. Register the Endpoint with AgentMail
You can register your URL using the AgentMail API. When you create a webhook, you’ll specify your endpoint’s URL as well as event types you want to receive.
event_types is required: list every event type the webhook should receive. Spam, blocked, and unauthenticated events must always be explicitly included.
Payload Structure
When AgentMail sends a webhook, the payload includes event_type and event_id, plus event-specific data. The example below shows a message.received payload; other events use different top-level objects (send, delivery, bounce, etc.). See Webhook Events for each event’s payload shape.
Payload size limit
message.received and its spam, blocked, and unauthenticated variants include the message body inline only when the stored body is 64 KiB or smaller. The stored body holds the plain-text and HTML bodies together with their extracted reply content, so the HTML alone can be noticeably smaller than 64 KiB before the limit is reached. Inline images embedded in the HTML (such as base64-encoded data URIs) count toward it and are a common reason for reaching it.
Above the limit, the payload omits text, html, extracted_text, and extracted_html, and carries two fields instead:
body_url: a pre-signed URL that downloads the stored body as a JSON object that includes thetext,html,extracted_text, andextracted_htmlfields. It is valid for one hour.body_expires_at: the time at whichbody_urlstops working.
All other message metadata and the thread object are still included. Other event types carry no message body.
Omitted content is always available through the API, including after body_url expires. After receiving a webhook, fetch the full message to access the complete body and attachment data:
Field Descriptions
event_type(string): The event type (e.g.message.received,message.sent,message.delivered). Payload structure varies by event—see Webhook Events for each event’s shape.event_id(string): A unique identifier for the event. It stays the same across retries and redeliveries of the event, and across every webhook that receives it, so use it as your deduplication key. Its format varies by event type; treat it as an opaque string.message(object): A dictionary containing the full details of the received email message.from_(array<string>): The sender’s email address. Note the trailing underscore to avoid conflict with the Python keyword.organization_id(string): The ID of your organization.inbox_id(string): The ID of the inbox that received the message.thread_id(string): The ID of the conversation thread.message_id(string): The unique ID of this specific message.labels(array<string>): Labels associated with the message (e.g.,received,sent).subject(string): The subject line of the email.preview(string): A short plain-text preview of the email body.text(string): The plain-text body of the email. Omitted when the stored body exceeds the 64 KiB inline limit; readbody_urlor fetch the full message via the API instead.html(string): The HTML body of the email, if present. Omitted when the stored body exceeds the 64 KiB inline limit; readbody_urlor fetch the full message via the API instead.body_url(string): Present only when the body was omitted. A pre-signed URL to the stored body, valid untilbody_expires_at(one hour).attachments(array<object>): A list of attachment metadata, each with its ownattachment_id,filename,content_type,size, andinlinestatus. Attachment content is not included in the webhook payload; download attachments through the API.in_reply_to(string): Themessage_idof the email this message is a reply to, if applicable.
Delivery, retries, and missed events
Retries and disabled endpoints
A delivery attempt succeeds when your endpoint responds with a 2xx status within 15 seconds. Any other status, including a redirect, or a slower response counts as a failure. AgentMail retries a failed delivery on this schedule: immediately, then after 5 seconds, 5 minutes, 30 minutes, 2 hours, 5 hours, 10 hours, and 10 hours. That is 8 attempts over about 27.5 hours, after which the event is marked failed for that endpoint.
If every delivery to an endpoint keeps failing for 5 days, the endpoint is disabled automatically. AgentMail does not notify you when this happens: the only signal is that the webhook’s enabled field reads false when you get or list it, so check it from your monitoring. Fix the endpoint, then re-enable it by updating the webhook with "enabled": true. Events that occurred while it was disabled are not redelivered.
Duplicates and ordering
Delivery is at least once, so your endpoint can receive the same event more than once. Deduplicate on event_id: it is the same on every retry of an event and on every webhook that receives it. The svix-id header stays the same across retries to one webhook, but differs between webhooks registered at different levels (organization, pod, inbox), so do not use it to deduplicate across webhooks.
Events are not guaranteed to arrive in order. Order them by the timestamps in the payload, not by arrival time.
Recovering missed events
If your endpoint was down or disabled, catch up from the API instead of waiting for redelivery:
- List messages with
afterset to the time of the last event you processed andascendingset totrue. Addinclude_spam,include_blocked, orinclude_unauthenticatedif you subscribe to those events. To skip messages you already handled from webhooks, match oninbox_idplusmessage_id, notmessage_idalone: one email sent to several of your inboxes is a separate message in each. - The inbox events feed (
GET /inboxes/{inbox_id}/events), which records alabel.addedevent for each label on every new message, includingreceived. - Polling webhooks for agents that are often offline. A polling webhook queues events until your agent fetches them, so events wait instead of failing, but it only holds events from after it was created. Polling webhooks are available in the US region only for now.
Copy for Cursor / Claude
Copy one of the blocks below into Cursor or Claude for complete Webhooks API knowledge in one shot.
