Back to results for “Hooks”
Node.js › Email & Notifications

How would you handle email delivery webhooks (bounces, complaints, opens) sent from a provider like SendGrid back to a Node.js application?

Advanced

Answer

Email providers send asynchronous webhook events (a message bounced, a recipient marked it as spam, an email was opened) to a URL your application exposes -- the handler should verify the webhook's authenticity (typically via a signature header), process events quickly (often just recording them and returning a fast 200 response, with heavier processing deferred to a background job), and use bounce/complaint data to automatically suppress future sends to addresses that have permanently failed or complained, protecting sender reputation.
app.post('/webhooks/sendgrid', express.raw({ type: 'application/json' }), (req, res) => {
  if (!verifySendGridSignature(req)) return res.status(401).end();
  const events = JSON.parse(req.body);
  for (const event of events) {
    if (event.event === 'bounce') suppressList.add(event.email);
  }
  res.status(200).end(); // acknowledge quickly
});
Real-world example A platform automatically adds any email address that generates a hard bounce or spam complaint to an internal suppression list via a SendGrid webhook handler, ensuring future sends never target that address again, protecting the company's sender reputation with mailbox providers over time.

Common follow-ups: Why is verifying the webhook's signature critical before trusting and acting on its payload?;What's the risk of doing heavy processing synchronously inside the webhook handler itself rather than deferring it to a queue?