Prompt
How do I trigger emails from webhook events in my app?
Latest observation
Nov 7, 2026
OpenAI APIWeb search: off
To trigger emails from webhook events, the usual pattern is:
-
Receive the webhook
- Create an endpoint in your app that accepts incoming
POSTrequests. - Verify the webhook signature or secret so only trusted sources can call it.
- Create an endpoint in your app that accepts incoming
-
Parse the event
- Read the event type and payload.
- Decide which events should send emails, e.g.:
user.signuporder.paidsubscription.canceled
-
Queue the email
- Don’t send the email directly in the webhook request if you can avoid it.
- Instead, push a job to a background queue so your webhook endpoint responds quickly and reliably.
-
Send the email
- Use an email provider such as:
- SendGrid
- Postmark
- Amazon SES
- Mailgun
- Resend
- Build the email content from the webhook data.
- Use an email provider such as:
-
Handle retries and deduplication
- Webhooks may be delivered more than once.
- Store an event ID or hash so you don’t send duplicate emails.
- Retry failed email jobs with backoff.
Example flow
Webhook received
{
"id": "evt_123",
"type": "order.paid",
"data": {
"customer_email": "alice@example.com",
"order_id": "ORD-987"
}
}
Your app logic
- If
type === "order.paid" - Create a job:
sendReceiptEmail(evt_123, alice@example.com, ORD-987)
Email job
- Render receipt template
- Send via your email API
- Mark event as processed
Minimal Node.js/Express example
import express from "express";
import crypto from "crypto";
import { sendEmail } from "./mailer.js";
const app = express();
app.use(express.json());
app.post("/webhook", async (req, res) => {
const signature = req.header("x-webhook-signature");
const body = JSON.stringify(req.body);
// Verify signature (example only)
const expected = crypto
.createHmac("sha256", process.env.WEBHOOK_SECRET)
.update(body)
.digest("hex");
if (signature !== expected) {
return res.status(401).send("Invalid signature");
}
const event = req.body;
// Acknowledge quickly
res.sendStatus(200);
if (event.type === "order.paid") {
await sendEmail({
to: event.data.customer_email,
subject: `Receipt for order ${event.data.order_id}`,
text: `Thanks for your purchase! Order: ${event.data.order_id}`
});
}
});
app.listen(3000);
Best practices
- Verify webhook authenticity
- Respond fast (
200 OKquickly) - Use a queue for email sending
- Prevent duplicate sends
- Log event IDs and email results
- Template emails instead of hardcoding
- Monitor failures
If you want, I can show you:
- a Python/FastAPI version,
- a queue-based architecture,
- or an example for a specific provider like SendGrid or Resend.