Currently Available: Need a skilled Software Developer for your next project?
Categories
APIs

How Webhooks Work

When one online service needs to tell another service that something happened, it sends a webhook. A webhook is an HTTP request that the sending service delivers to a URL the receiving service has registered. Web services use HTTP as their standard way to exchange requests and responses. For example, a payment provider can notify a store when a payment succeeds, so the store does not have to keep checking for an update.

The service that sends the notification is the sender, and the service that receives it is the receiver. When a chosen event occurs, the sender triggers a request. The receiver checks the request, accepts it, and takes the next step. Each provider sets its own rules for delivery speed, retries, and event order.

How a webhook reaches your service

The receiver gives the sender an endpoint, which is a URL that accepts incoming requests. The receiver also selects which event types it wants, such as a payment succeeding or a repository changing. The sender stores those settings and uses them to decide where and when to send notifications.

When a selected event occurs, the sender sends an HTTP request to the endpoint. Most senders use a POST request, which carries data in its body. The body may include the event type and information about the affected item, though each provider chooses its own format.

The receiver then checks that the request is valid and accepts it. From there, it can handle follow-up work, such as updating an order or notifying a customer. If that work takes longer than the sender’s acknowledgement deadline, the receiver should first save the event or place it in a durable queue, a storage mechanism that preserves work for later processing. It can then return a success response while a background worker handles the event.

A typical sequence looks like this:

  1. The receiver registers an endpoint and chooses event types.
  2. The sender detects a selected event and sends a request.
  3. The receiver verifies the request and checks its contents.
  4. The receiver saves the event, acknowledges it, and processes it.
  5. The receiver requests more information from the sender’s API, an interface for requesting data or actions, when the notification does not contain enough detail.

Each sender sets its own rules for successful responses and retries. For example, GitHub’s webhook guidance asks receivers to return a 2XX response within 10 seconds. That 10-second limit is specific to GitHub, so check the rules for whichever provider you use.

How webhooks work with APIs and polling

An API lets one service request data or actions from another, while a webhook alerts a service when an event occurs. The two often work together. A store can use a payment provider’s API to create a payment, receive a webhook when the payment status changes, and call the API again to retrieve the latest details.

A service can also check for changes by polling. When your service polls, it asks the provider’s API on a schedule whether anything has happened. You control when those checks run, though your service still sends requests even when there is no new information. When you use a webhook instead, your service skips those repeated checks for subscribed events, but it has to stay available to accept incoming requests and handle delivery failures.

A provider may include all the information your receiver needs in the webhook payload, or it may send a minimal payload that only identifies the event so your receiver can call the API for more detail. Stripe, for example, documents both snapshot and thin event formats. Check the provider’s documentation before assuming that a payload contains a complete or current record. For related design choices, see how APIs and webhooks work together.

Verify requests before acting

A webhook endpoint receives requests from outside your service, so it must verify that a request came from the provider. Many providers attach a signature, a value created with a cryptographic method that lets the receiver check whether the message matches what the sender signed. Because the verification method and signed data differ by provider, follow that provider’s instructions or use its maintained verification library instead of assuming another service’s method will work.

HTTPS encrypts data as it travels between services. A signature answers a different question by showing whether the request is authentic and unchanged. Use both when the provider supports signed requests. Some signature methods require the receiver to verify the exact request body before parsing it; if your code parses or reformats the body first, the signature check can fail. GitHub’s delivery validation instructions also recommend a constant-time comparison for signatures, which reduces the risk that attackers can infer a secret by measuring comparison behavior.

After your receiver verifies the signature, check that the event type and required fields match what you expect. A valid signature only proves who sent the request, so your receiver still needs to apply its own business rules before acting.

Handle retries, duplicates, and event order

A sender may retry a webhook when the receiver is unavailable or does not return a response the sender accepts. When the sender retries, it can recover from a temporary problem, though it may also deliver the same event more than once. The receiver should record an event identifier when the provider supplies one and make its handler idempotent, meaning that if it processes the same event again, it does not repeat the important effects. For instance, a repeated payment notification should not cause the receiver to charge the customer again.

Event identifiers and delivery identifiers do not always mean the same thing. Follow the provider’s documentation to see which identifier distinguishes the underlying event and which identifies a delivery attempt. Some providers also note that separate event objects can refer to the same underlying change.

The receiver should also avoid assuming that events arrive in the order they occurred. Stripe documents that its events are not guaranteed to arrive in generation order. When order matters, the receiver can fetch the current object through the provider’s API and apply the latest state instead of relying on arrival order. For practical failure scenarios, see webhook delivery and debugging.

A reliable way to receive webhooks

For important events, the receiver should do as little work as possible before acknowledging a request while still making sure it does not lose the event. A practical pattern is to verify the signature, validate the event, check for duplicates, save the event to durable storage, and return the provider’s required success response. A background worker can then run slower tasks and record failures for later investigation.

This pattern lets the receiver accept a notification before it finishes every action the notification triggers. If the receiver returns success before saving the event, it can lose work if it crashes. If it waits to finish every task, the sender may time out and retry while the receiver is still working. Because the acknowledgement deadline and retry behavior depend on the provider, check its current delivery documentation. You can also test webhook reliability to catch failures before they affect production traffic.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *