Webhook Setup Guide
Step-by-step guide to configure webhooks.
Step-by-step guide to configure webhooks.
This guide walks you through the complete process of setting up webhooks to receive real-time notifications from AgentMail. You’ll learn how to create an ngrok account, set up an inbox, configure webhooks, and write a simple webhook receiver.
Before you start, make sure you have:
pip package managerWebhooks require a public URL and tools like ngrok. If you’d rather skip that setup, WebSockets give you real-time events over a persistent connection with no external tooling. See the WebSockets quickstart.
First, install the required Python packages:
Ngrok creates a secure tunnel from a public URL to your local development server, allowing AgentMail to send webhooks to your machine during development.
Visit ngrok.com and click “Sign up” to create a free account.
After logging in, ngrok will guide you through the setup process. Select your operating system and follow the installation instructions.
For macOS, you can install ngrok via Homebrew:
After installation, authenticate ngrok with your authtoken (found in your ngrok dashboard):
Before you can receive webhooks, you need an inbox to receive messages. Create one using the AgentMail API:
The client_id parameter ensures that running this code multiple times won’t create duplicate inboxes. If the inbox already exists, it will return the existing one.
In your terminal, start an ngrok tunnel to expose your local server (we’ll use port 3000):
You should see output similar to this:
Copy the Forwarding URL (e.g., https://your-subdomain.ngrok-free.app). This is the public URL that AgentMail will use to send webhooks.
When viewing your webhook receiver in the browser, use the http://127.0.0.1:3000 URL shown in the “Web Interface” line, not the ngrok URL. The ngrok URL is only for external services like AgentMail.
Why localhost? Safari is stricter than Chrome/Firefox when viewing development servers through HTTPS ngrok tunnels. Safari blocks local WebSockets and some dev-only scripts, which causes the page to show a loading spinner indefinitely due to Hot Module Replacement (HMR). This is expected development behavior and not a documentation issue. Using localhost or static builds avoids this problem.
Now register your webhook endpoint with AgentMail:
Create a file named webhook_receiver.py with the following code:
Open your browser and visit http://127.0.0.1:3000 to see the status page confirming your webhook receiver is running:
To test your webhook setup:
webhook-demo@agentmail.to)Now that your webhook is working, you can extend the receive_webhook() function to:
Check out the Event-Driven Agent Example for a more advanced implementation.
/webhooksFree ngrok accounts have 2-hour session limits. The tunnel will disconnect and you’ll need to restart ngrok and update your webhook URL with AgentMail.
If port 3000 is already in use, choose a different port:
app.run(port=XXXX)ngrok http XXXXCopy one of the blocks below into Cursor or Claude for webhook setup in one shot.
For production deployments:
text and html body fields are omitted from the payload. Use the Get Message endpoint to fetch the complete message content, including the full body and attachments. See Payload size limit for details