Receive webhooks in your app
A webhook endpoint is a public door into your database. Five habits make it safe: verify, record, reply fast, process once, and reconcile.

Key takeaways
- A webhook receiver is a public edge function. It must verify the provider’s signature on the raw request body before trusting a single field.
- Providers retry and send duplicates, sometimes out of order. Record each event id once and make processing idempotent.
- Reply 2xx within a few seconds and do slow work afterwards, or the provider will time out and send the event again.
Receiving webhooks means giving another service a URL on your backend that it calls whenever something happens on its side: a payment succeeded, an order shipped, a form was submitted. In practice you deploy a public server function, register its URL and get a signing secret from the provider, store that secret, and write a handler that verifies the signature, records the event, replies quickly and then updates your data. If you only need the concept, the webhook glossary entry explains it. This page is about building one that does not break.
The example: a print shop with two providers
An online poster shop takes payment through Stripe and sends orders to a print-on-demand partner. Two kinds of events arrive: payment events from Stripe (payment succeeded, refunded) and fulfillment events from the printer (`order.printed`, `order.shipped` with a tracking number, `order.failed`). Each one changes an `orders` row and may email the customer. Every lesson below comes from one of these going wrong.
What you need
- A backend with [edge functions](https://docs.startbase.ai/cloud/cloud-edge-functions). Each function's detail page shows whether it requires sign-in or is public, and has a Copy URL button. Webhook receivers must be public, because the provider has no user session; the signature is what proves who is calling.
- The provider's signing secret. You get it when you register the endpoint URL in the provider's dashboard. Store it in Backend → Secrets, for example `PRINTER_WEBHOOK_SECRET` or `STRIPE_WEBHOOK_SECRET`.
- A table for events: `webhook_events` with `provider`, `event_id`, `type`, `received_at`, `processed_at`, `status`, `error`, `payload`, and a unique constraint on `(provider, event_id)`.
- The provider's documentation for its signature scheme, retry schedule and event list. They differ enough that guessing is a mistake.
The handler in five steps
1. Verify the signature on the raw body
Most providers sign each request with an HMAC (usually SHA-256) of the request body using your signing secret, and put the result in a header. Many include a timestamp in what they sign. Your handler recomputes the HMAC over the exact raw bytes it received and compares in constant time. Parse the JSON first and re-serialize it, and the bytes change, the signature fails, and you lose an hour wondering why. Reject anything with a missing or wrong signature with 400 or 401, and reject timestamps older than about five minutes to block replayed requests.
2. Record the event once
Insert into `webhook_events` using the provider's event id. If the insert hits the unique constraint, you have seen this event before: reply 200 and stop. This single step is what prevents double emails, double credits and double shipments when a provider retries.
3. Reply fast
Providers wait only a few seconds (often somewhere between 5 and 30, depending on the service) before treating the delivery as failed and scheduling a retry. Answer 200 as soon as the event is safely stored. Slow work such as generating a PDF invoice, calling another API or sending several emails happens after the reply, from a worker function or a scheduled job that picks up `status = 'pending'` rows every minute.
4. Process idempotently
Write each step so running it twice changes nothing: 'set status to shipped and tracking to X' rather than 'append a shipment'; send the shipped email only if `shipped_email_at` is empty. Mark the event `processed` with a timestamp, or `failed` with the error text.
5. Expect events out of order
Webhooks are not a queue with ordering guarantees. `order.shipped` can arrive before `order.printed`, and a retried old event can arrive after a newer one. Two defenses: only move an order forward through its states (never from shipped back to printed), and for anything important, use the event as a nudge and fetch the current state from the provider's API before writing.
Know your provider’s retry window
Every provider has a retry policy: how many times it retries, how far apart, and when it gives up. Some retry for days with growing gaps; some try only a handful of times; some disable an endpoint that keeps failing and email the account owner. Look this up for each provider you integrate and write it next to the function, because it tells you how long an outage you can survive without losing events. If your endpoint was broken for a weekend and the provider gives up after 24 hours, those events are gone from the push channel and only your reconciliation job will find them.
Many dashboards also show a delivery log with each attempt, the status your endpoint returned and a resend button. When a customer says 'my order never updated', that log plus your `webhook_events` table answers the question in a minute: either the provider never sent it, your endpoint rejected it, or it arrived and processing failed.
Paste-ready brief
Add a webhook receiver for my print partner. The signing secret is in Secrets as PRINTER_WEBHOOK_SECRET. Create a public edge function printer-webhook that verifies the HMAC-SHA256 signature from the provider's signature header over the raw request body, compares in constant time, rejects timestamps older than 5 minutes, then inserts the event into a webhook_events table with a unique provider plus event_id, replying 200 immediately (and 200 without reprocessing for duplicates). A worker that runs every minute processes pending events: order.printed, order.shipped (save tracking number, email the customer once) and order.failed (flag for admin). Orders only move forward through pending, printed, shipped, delivered. Show failed events in the admin panel with a Retry button.
Testing webhooks before launch
- Use the provider's test mode. Most dashboards can send a test event to your URL; Stripe also has a CLI that forwards and triggers events.
- Send each event type twice. The second delivery must change nothing.
- Send one with a bad signature. It must be rejected and must not touch the database.
- Send events out of order. Shipped before printed should still end at shipped.
- Read the logs. Backend → Logs shows function events; the Fix with AI button on a failing row hands the error straight to the agent.
Pitfalls
- Skipping verification 'for now'. A public URL without signature checks lets anyone mark orders paid or shipped. The check is a few lines; there is no 'for now'.
- Returning 500 for events you do not handle. The provider retries for hours or days. Reply 200 for unknown event types and ignore them.
- Secrets in the code. The signing secret goes in Secrets, never in the function source, a client environment variable or chat.
- One endpoint for every provider. Each provider signs differently. One function per provider keeps verification simple and failures contained.
- Rotating secrets carelessly. When you roll a signing secret, some providers sign with both for a short window. Accept either during the overlap, then drop the old one.
- No reconciliation. Webhooks can be missed if your endpoint was down longer than the retry window. A nightly job that compares recent orders with the provider's API catches the stragglers.
Frequently asked questions
Does a webhook endpoint need authentication?
Not user sign-in, since the provider has no session. It needs signature verification instead: recompute the provider HMAC over the raw body with your signing secret and reject anything that does not match.
Why does signature verification fail even with the right secret?
Usually because the handler verified a parsed and re-serialized body instead of the raw bytes, or used the wrong secret for test versus live mode. Verify against the raw request body.
What should my webhook return?
A 2xx status as soon as the event is stored, ideally within a couple of seconds. Return 2xx for duplicates and unknown event types too, and a 4xx only for failed verification.
How do I handle the same webhook arriving twice?
Store each provider event id with a unique constraint. If the insert fails as a duplicate, reply 200 and skip processing. Design each processing step so repeating it changes nothing.

