Webhooks Overview
Get real-time notifications for email events.
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.
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.
AgentMail supports ten webhook event types. When creating a webhook, you can subscribe to specific events or receive all standard events. 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 (requires label_spam_read permission)message.received.blocked — A message was received and matched a block list entry (requires label_blocked_read permission)message.received.unauthenticated — A message was received without authentication headers, so AgentMail could not verify whether it was authenticatedmessage.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)Domain events:
domain.verified — Custom domain successfully verifiedSpam, 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.
event_types on a webhookWhen you update a webhook, you can change which events it receives by sending event_types on PATCH:
Subscribing to message.received.spam, message.received.blocked, or message.received.unauthenticated on update still requires the same label permissions as on create.
The process is straightforward:
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.
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.
Specify which events to receive; omit event_types to subscribe to all standard event types. Spam, blocked, and unauthenticated events must always be explicitly included.
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.
Webhook payloads are capped at 1 MB. When a message exceeds this limit, AgentMail omits the text and html fields from the webhook payload to reduce size. All other metadata is still included. Inline images embedded in the HTML (such as base64-encoded data URIs) count toward the payload size and are a common reason for the limit being reached.
Omitted content is always available through the API. After receiving a webhook, fetch the full message to access the complete body and attachment data:
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 this specific event delivery.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. May be omitted when the webhook payload exceeds the 1 MB size limit. Fetch the full message via the API if this field is missing.html (string): The HTML body of the email, if present. May be omitted when the webhook payload exceeds the 1 MB size limit. Fetch the full message via the API if this field is missing.attachments (array<object>): A list of attachment metadata, each with its own attachment_id, filename, content_type, size, and inline status. Attachment content is not included in the webhook payload; download attachments through the API.in_reply_to (string): The message_id of the email this message is a reply to, if applicable.Copy one of the blocks below into Cursor or Claude for complete Webhooks API knowledge in one shot.
Explore the full list of available event types and their data payloads.
Learn how to verify webhook signatures to secure your endpoints.
Receive events over a persistent connection with no public URL required.
Build a fully deployable, event-driven agent that can respond to emails in real time.