Skip to the page

Background work, webhooks and real-time · mid

Receiving webhooks safely

A webhook is an HTTP POST a provider sends you when something happens; you must verify its HMAC signature over the raw body, reject old timestamps, answer 2xx quickly, process it asynchronously, and ignore duplicates because providers retry.

The notes

What you must know

  • Verify the signature: the provider signs the exact raw body with a shared secret (HMAC-SHA256). Stripe's Stripe-Signature header has t=timestamp and v1=signature over 'timestamp.body'; Razorpay sends X-Razorpay-Signature. Compute it yourself and compare with crypto.timingSafeEqual.
  • Use the raw bytes: in Express, mount express.raw({ type: 'application/json' }) on the webhook route before express.json(); a parsed-and-restringified body will not match.
  • Replay protection: reject timestamps older than a few minutes (Stripe's libraries use a tolerance, 5 minutes by default).
  • Respond 2xx fast (before slow work) or the provider times out and retries; put the event on a queue and process it there.
  • Providers retry for a long time (Stripe: up to three days) and can send duplicates or out of order: store processed event ids, and re-fetch the object's current state when order matters.
  • Exempt the webhook route from CSRF middleware; keep the signing secret in a secrets manager and support rotating it.

The red pen

Common traps: mistake → fix

  • Mistake: Checking the source IP is enough

    Fix: IPs change and can be spoofed behind proxies; always verify the signature (IP allowlists are extra).

  • Mistake: Verify after express.json() parsed the body

    Fix: the signature is over the raw bytes, so verification fails or, worse, gets skipped.

  • Mistake: Each webhook arrives exactly once and in order

    Fix: expect retries, duplicates and reordering.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you make sure a webhook really came from Razorpay or Stripe?
  2. Why must you use the raw body?
  3. The same 'payment.captured' event arrives twice. What happens in your system?
  4. Why respond 200 before doing the work?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview