Prompt
How do I send billing emails from a webhook trigger?
Latest observation
Nov 20, 2026
OpenAI APIWeb search: off
To send billing emails from a webhook trigger, the usual pattern is:
- Webhook receives an event
- Example: payment succeeded, invoice created, subscription renewed, etc.
- Webhook handler validates the event
- Verify signature / secret so you know it’s from your billing provider.
- Handler looks up the customer + billing details
- Email address, invoice number, amount, due date, plan, etc.
- Send the email through an email service
- Use a transactional provider like SendGrid, Mailgun, Postmark, AWS SES, etc.
- Respond quickly to the webhook
- Return
200 OKas soon as the event is accepted, and do the email work asynchronously if possible.
- Return
Typical flow
1) Billing provider sends webhook
For example, Stripe/Chargebee/Recurly sends:
invoice.paidinvoice.payment_failedsubscription.canceled
2) Your webhook endpoint handles it
Pseudo-example:
app.post("/webhooks/billing", async (req, res) => {
const event = verifySignature(req); // important
if (event.type === "invoice.paid") {
const customerEmail = event.data.customer_email;
const invoice = event.data.invoice;
await sendEmail({
to: customerEmail,
subject: `Payment received for invoice ${invoice નંબર}`,
text: `Thanks for your payment of ${invoice.amount}.`
});
}
res.sendStatus(200);
});
Best practices
- Verify webhook signatures to prevent spoofing.
- Idempotency: avoid sending duplicate emails if the webhook is retried.
- Store processed event IDs in a database.
- Use a queue/background job if email sending might take time.
- Use templated emails for billing messages.
- Log failures and retry safely.
- Don’t block the webhook response on slow external calls if you can avoid it.
Recommended architecture
Webhook → validate → enqueue job → email worker → email provider
That gives you:
- faster webhook responses
- less chance of retries from timeout
- safer handling of duplicates
Example with a queue
Webhook:
app.post("/webhooks/billing", async (req, res) => {
const event = verifySignature(req);
if (event.type === "invoice.paid") {
await queue.add("send-billing-email", {
eventId: event.id,
customerEmail: event.data.customer_email,
invoiceId: event.data.invoice_id
});
}
res.sendStatus(200);
});
Worker:
queue.process("send-billing-email", async (job) => {
const { customerEmail, invoiceId } = job.data;
await sendEmail({
to: customerEmail,
subject: `Invoice ${invoiceId} paid`,
text: "Your billing payment was successful."
});
});
If you want, I can show you a working example in Node.js, Python, or PHP, and tailor it to a specific billing provider like Stripe.