Prompt
How do I add webhooks for delivery events and bounce tracking?
Latest observation
To add webhooks for delivery events and bounce tracking, the exact steps depend on the email/sending platform you’re using, but the general setup is usually:
1) Create an endpoint on your app
Set up an HTTPS route that can receive incoming POST requests, for example:
POST /webhooks/email-events
Your endpoint should:
- accept JSON payloads
- return a fast
2xxresponse - verify the sender signature if your provider supports it
- log or store the event data
2) Subscribe to the right events
In your provider’s dashboard or API, enable webhook event types such as:
- delivery
- bounce
- deferred
- complaint
- open/click if needed
For your case, at minimum enable:
- delivered
- bounced
3) Register the webhook URL
Add your endpoint URL in the provider settings, for example:
https://yourdomain.com/webhooks/email-events
Some providers let you do this in the UI; others via API.
4) Verify webhook signatures
If your provider signs webhook requests, validate them using:
- a shared secret
- HMAC signature headers
- timestamp checks to prevent replay attacks
This is important so you only process legitimate events.
5) Parse and handle events
Typical logic:
- delivery event → mark message as delivered
- bounce event → mark recipient as undeliverable and classify as hard/soft bounce
- complaint → suppress future sends
Example event handling
delivered: update status todeliveredbounce: inspect bounce type/reason, then mark address accordingly
6) Test it
Send test events from your provider if available, or use a tool like:
- ngrok
- webhook.site
- requestbin
to inspect payloads during development.
Example webhook handler (generic Node.js/Express)
import express from "express";
const app = express();
app.use(express.json());
app.post("/webhooks/email-events", (req, res) => {
const event = req.body;
switch (event.type) {
case "delivered":
console.log("Delivered:", event.messageId);
break;
case "bounce":
console.log("Bounced:", event.messageId, event.bounceType);
break;
default:
console.log("Unhandled event:", event.type);
}
res.sendStatus(200);
});
app.listen(3000, () => console.log("Server running on port 3000"));
If you tell me which service you’re using (e.g. SendGrid, Mailgun, AWS SES, Postmark, Brevo, SparkPost), I can give you the exact steps and payload format for that platform.