Guide

Webhook best practices for SaaS apps

A webhook is a public URL that changes your data when someone calls it. Treat it with the care you would give a login form: prove who is calling, expect repeats, and never let a slow handler break delivery.

7 min read · Last checked

In short

Key points

  • Verify the signature against the raw request body before you act on anything.
  • Reject requests whose timestamp is too old, so a captured request cannot be replayed later.
  • Return a 2xx quickly and do the real work from a queue.
  • Expect duplicates and out-of-order delivery. Record event ids and re-read current state when order matters.

Verify every request

Without verification, anyone who finds your endpoint URL can send fake events to grant access, mark invoices paid or change records. Providers sign each request with a shared secret. The common scheme is an HMAC with SHA-256 over a timestamp and the request body.

Two details cause most failures. First, compute the signature over the raw bytes of the body as received. A framework that parses and re-serialises the JSON first changes the bytes and the check fails. Second, compare signatures in constant time, so response timing does not leak information.

Reject old requests

Because the timestamp is part of the signed content, an attacker cannot change it without breaking the signature. Your side must still check it. Reject any request whose timestamp is more than a few minutes from your clock. Stripe’s libraries use a default tolerance of five minutes, and warn against a tolerance of zero because it turns the recency check off. Keep your server clock synchronised.

Answer fast, work later

Providers wait a limited time for your reply and treat a slow reply as a failure. Return a 2xx status as soon as you have verified the request and stored it, then process it from a queue. Stripe’s guidance is to return success before any complex logic, and to process events asynchronously so that a burst, such as every subscription renewing at the start of a month, does not overwhelm your servers.

Expect duplicates and disorder

  • The same event can arrive more than once. Store the ids of events you have processed and skip repeats.
  • Events are not guaranteed to arrive in the order they happened. Do not assume a created event comes before an updated event.
  • When order matters, treat the event as a signal and read the current state from the provider’s API instead of trusting the order of arrival.
  • Make your handlers idempotent, so running one twice leaves the same result as running it once.

Rotate secrets without downtime

Secrets leak and people leave, so you need a way to change them. A good provider lets old and new secrets work together for a while so you can deploy the new one without dropping events. Stripe offers an overlap of up to 24 hours when you roll a secret.

Protect the endpoint itself

  • Use HTTPS only.
  • Subscribe only to the event types you use.
  • Exempt the route from browser-oriented protections such as CSRF checks, which would reject legitimate server calls, and rely on signature verification instead.
  • Log the delivery id and outcome for each request so you can answer what happened to an event.

How TenancyEngine webhooks work

Each delivery is an HTTPS POST with a JSON body and three headers: X-TE-Event with the event type, X-TE-Signature, and X-TE-Delivery-Id. The signature has the form t=timestamp,v1=signature, an HMAC-SHA256 over the timestamp, a dot and the raw body. The SDK helper rejects a signature older than five minutes.

Failed deliveries are retried with exponential back-off (10 seconds, 1 minute, 5 minutes, 30 minutes, 2 hours, then 24 hours) for up to 6 attempts, then moved to a dead-letter list. After you rotate a secret, deliveries carry both signatures for 24 hours. You can see recent deliveries for an endpoint and redeliver a failed one; a redelivery re-sends the stored body unchanged, so an event id inside it stays the same and you can deduplicate on it.

Verify a signature (.NET)
using var reader = new StreamReader(req.Body);
var rawBody = await reader.ReadToEndAsync();
var signature = req.Headers["X-TE-Signature"].ToString();

return WebhookSignatureVerifier.Verify(rawBody, signature, webhookSecret)
    ? Results.Ok()
    : Results.BadRequest();

Honest limits

What to know before you rely on this

  • The SDK verification helper is for .NET. In other languages, compute the HMAC-SHA256 yourself over the timestamp, a dot and the raw body, compare it in constant time, and reject a timestamp older than five minutes.

Questions people ask

Why does my signature check fail even though the secret is right?

The most common cause is that your framework changed the request body before you verified it. Verify against the raw bytes, before any parsing.

What should my endpoint return for an event I do not recognise?

Return a 2xx. Providers add event types over time, and an error response makes them retry something you intend to ignore.

How do I recover events I missed while my endpoint was down?

Redeliver them from the provider’s delivery log once the endpoint works, and deduplicate by event id so anything you already processed is skipped.

Sources

Keep reading

Try it on the free plan

One app and five customers, no credit card required.

Request access More guides