Developer & Network Tools

Webhooks Explained

Learn how webhooks push events to your server, how to verify and process them safely, and what to do when deliveries fail.

Key takeaways

  • A webhook is an HTTP request a provider sends to your URL when an event happens.
  • Verify every delivery, typically by checking a signature made with a shared secret.
  • Reply with a success status quickly and do slow work afterward from a queue.
  • Expect duplicate, delayed and out-of-order deliveries, and make your handler safe to repeat.
  • Reconcile against the provider's API from time to time, because some events can be missed.

A webhook is a message one service sends to your server when something happens. You give the service a URL, and when an event occurs, such as a payment succeeding or code being pushed to a repository, it sends an HTTP request, usually a POST with a JSON body, to that address.

It reverses the usual direction of an API. Instead of your program asking again and again whether anything changed, the other side tells you. To use one well you need an endpoint that verifies who sent the message, answers quickly, and copes with duplicates and gaps.

Push instead of polling

Polling means calling an API on a schedule to look for changes. It's simple and it works from anywhere, but most calls find nothing, and news arrives only as fast as your interval.

A webhook delivers the news when it happens. The cost is that you must now run something reachable on the public internet that is ready at all times.

Question Polling Webhook
Who starts the connection Your program The provider
Delay Up to one polling interval Usually short
Wasted requests Many Few
Needs a public endpoint No Yes
Missed events Found on the next poll Depends on retries

If you only need a daily summary, a scheduled job that asks the API is often the better design.

What a delivery looks like

A typical delivery for a fictional billing service might be:

POST /hooks/billing HTTP/1.1
Host: www.example.com
Content-Type: application/json
X-Signature: sha256=5d41402abc4b...
X-Event-Id: evt_8821

{"type": "invoice.paid", "created": 1790000000,
 "data": {"invoice_id": "inv_309", "amount": 4200}}

Header names differ between providers. What you usually get is an event type, a unique event ID, a timestamp, some data about the affected object and a signature. Some providers send only the object's ID and expect you to fetch the details from their API, which keeps sensitive data out of the delivery.

The full round trip

A webhook delivery handled well
Provider Your endpoint Your queue Worker POST event with signature 1 Store verified event 2 200 OK, quickly 3 Hand over the job 4 Fetch current details 5 if needed Provider Your endpoint Your queue Worker POST event with signature 1 Store verified event 2 200 OK, quickly 3 Hand over the job 4 Fetch current details 5 if needed

The reply goes back before the slow work begins.

Verifying that the sender is real

Your webhook URL is public. Anyone who learns it can send a request that claims an invoice was paid. A secret-looking path isn't enough protection.

The common defense is a keyed hash called HMAC. You and the provider share a secret. The provider computes the HMAC of the request body, often combined with a timestamp, and sends the result in a header. You repeat the calculation and compare.

  1. Read the raw request body exactly as it arrived. Parsing and re-serializing JSON changes bytes and breaks the check.
  2. Build the signed string the way the provider documents it.
  3. Compute the HMAC with your secret.
  4. Compare using a constant-time comparison function from your language's security library.
  5. Reject deliveries whose signed timestamp is too old, which limits replay of captured messages.

Some providers sign with a private key and publish a public key instead, an approach explained in digital signatures explained. Others offer client certificates or published source IP ranges. Address lists can change, so treat them as an extra layer. Keep the secret in environment variables or a secrets manager, never in your repository.

Answer fast, work later

Providers wait only a limited time for your reply. If you generate a report or send emails before responding, the provider may decide the delivery failed and send it again while the first attempt is still running.

The reliable pattern is to verify, record the event somewhere durable, return a 2xx status and let a background worker do the rest. Even a single database table with a "processed" column works as a queue for a small site.

Return an error status only when you want a retry: your storage is down, for instance. If the event is valid but of a type you don't care about, accept it with success so the provider doesn't keep resending it.

Retries, duplicates and ordering

Most providers retry failed deliveries, usually with growing pauses, and give up eventually. Check how long yours keeps trying and whether you can replay events from its dashboard.

Retries mean you'll sometimes receive the same event twice. Store each event ID with a unique constraint and skip ones you've already handled. Design actions to be idempotent, meaning repeating them changes nothing further: "set order 309 to paid" is safe to repeat, while "add 42 to the balance" isn't. See retries, backoff and idempotency.

Order isn't guaranteed either. A "subscription updated" event can arrive before "subscription created". Compare timestamps or version numbers, or fetch the current object from the API and act on that.

What your endpoint should answer
Is the signature missing or wrong? YES Reject with a 400 or 401 and log it. 1 NO Have you already stored this event ID? YES Return 200 and do nothing more. 2 NO Does saving the event fail right now? YES Return a 500 range error so it is sent again. 3 NO Accept and queue Save the event, return 200, and process it in the background. Is the signature missing or wrong? 1 IF YES Reject with a 400 or 401 and log it. NO Have you already stored this event ID? 2 IF YES Return 200 and do nothing more. NO Does saving the event fail right now? 3 IF YES Return a 500 range error so it is sent again. NO Accept and queue Save the event, return 200, and process it in the background.

Setting up your first webhook

The steps look much the same across providers, whatever the menus are called.

  1. Create a route on your server that accepts POST requests, served over HTTPS.
  2. In the provider's developer or integration settings, add the full URL of that route.
  3. Choose only the event types you plan to handle. Subscribing to everything buries the useful ones and adds load.
  4. Copy the signing secret into your server's configuration.
  5. Trigger a test event, using the provider's test mode if it has one.
  6. Check that your logs show the delivery, that verification passed and that the provider recorded a success.
  7. Send a deliberately wrong signature from your own machine and confirm it's rejected.

Keep separate endpoints and secrets for testing and production. Mixing them is a common cause of confusion, and it risks test events changing real records.

What to log and watch

Webhooks fail quietly, because no person is sitting at a screen waiting for them. Record the event ID, type, time received, verification result and processing outcome for each delivery. Avoid logging whole payloads if they contain personal data, or restrict who can read them and delete them after a sensible period.

A few measures tell you most of what you need: how many events arrived, how many failed verification, how long the queue's oldest unprocessed item has waited, and how many jobs failed repeatedly. Move those repeated failures to a separate holding list, sometimes called a dead letter queue, so one bad record doesn't block everything behind it. A person can then inspect the item, fix the cause and run it again.

A burst of verification failures deserves attention. It might mean the secret was rotated on one side only, or that someone is probing the address.

Developing and debugging

Your laptop usually isn't reachable from the internet. During development you can use the provider's command line forwarding tool if it has one, a tunneling service that gives you a temporary public address, or a test server. A tunnel exposes something on your machine, so run only what you need and close it afterward.

When deliveries fail, check these in order:

  • The provider's delivery log, which often shows the status and body you returned.
  • Your web server logs, to see whether the request arrived at all.
  • Redirects, since many senders don't follow a 301 or 302.
  • A firewall, bot filter or cross-site request forgery protection blocking the POST.
  • The TLS certificate, which must be valid and complete.
  • Signature failures caused by a framework altering the body, or the test secret being used in production.

Sending webhooks yourself

If your own product offers webhooks, give receivers what you'd want: a documented signature scheme, unique event IDs, retries with backoff, a delivery log and a way to resend. Set short timeouts so one slow customer doesn't stall the queue.

Be careful with customer-supplied URLs. A server that fetches any address on request can be tricked into calling internal systems, a flaw known as server-side request forgery. Require HTTPS, resolve the destination and refuse private, loopback and link-local addresses, and don't follow redirects blindly.

When a simpler option will do

Not everyone needs to write a receiver. Many chat tools, form builders and automation platforms accept or send webhooks through a settings screen: you copy a URL from one product and paste it into another. In that setup the platform handles retries and parsing, but the URL itself often acts as the only credential. Keep it private, don't post it in screenshots, and generate a new one if it escapes.

Check what data travels too. An automation that forwards every new customer record to a third party creates another copy of personal information, with its own retention rules and its own chance of exposure. Send the fields the next step needs and leave the rest where they are.

Keep a safety net

Webhooks are good at speed and weaker at completeness. If your server is down longer than the retry window, events can be lost. For anything involving money or access rights, run a periodic reconciliation that lists recent objects through the provider's API and repairs any mismatch. Also alert yourself when no events arrive for unusually long, since silence is easy to overlook. Some providers disable an endpoint after repeated failures, so watch for their notices and re-enable it after you fix the cause.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Developer & Network Tools →

Latest news on this topic