Bookmarks

Webhook

A way for services to communicate in which the source delivers data to the recipient’s URL over HTTP as soon as an event happens, with no polling.

Also: HTTP callback, reverse API

checked 9 Nov

All terms

A webhook turns ordinary API communication around: data is not pulled by scheduled requests but pushed — when an event occurs, the source itself sends an HTTP request to a URL the recipient registered in advance. GitHub reports pushes this way; a payment system reports payments.

How it works

The recipient publishes an HTTPS URL and gives it to the source together with a list of the events it wants to hear about. When an event happens, the source sends a POST request with data about it; a 2xx response means delivered, and any other outcome means a retry. It is an ordinary API call, except that this time the server is the party that subscribed.

bash
POST /hooks/9f2c HTTP/1.1Host: example.comwebhook-id: msg_2KWPBgLlAfxdpx2AI54pPJ85f4Wwebhook-timestamp: 1674087231webhook-signature: v1,K5oZfzN95Z9UVu1EsfQmfVNQhnkZ2pj9o9NDN/H/pI4=Content-Type: application/json {"type": "invoice.paid", "timestamp": "2022-11-03T20:26:10.344522Z", "data": {"id": "1f81eb52-5198-4599-803e-771906343485"}}

The payload is usually “thin”: JSON with the event type, a timestamp and only the identifiers of the affected objects, and the details are fetched with an API request. The event identifier stays the same across retries and serves as an idempotency key: process the event once, even if it arrives twice.

Why it is needed

The alternative to a webhook is polling: the recipient keeps asking the API whether anything new has appeared. Frequent checks waste quota; infrequent ones arrive late. A webhook removes both problems: interest in an event is expressed once, at subscription time, data arrives almost in real time, and the load does not grow with the number of watched objects.

  • builds and deployments: a push to a repository triggers a CI pipeline;
  • notifications: an event in a system lands in Slack or Telegram;
  • payments: a gateway tells the shop about a payment, refund or dispute;
  • audit: events are written to a log as they happen;
  • automation: clouds run functions in response to each other’s events.

Request signing

A webhook URL is an open door: anyone who knows it can send a forged request. So the source signs each notification with a secret key, most often HMAC-SHA256 in a header such as webhook-signature or X-Hub-Signature-256. The recipient computes the signature over the body itself and compares the two with a constant-time comparison, to avoid opening a timing attack. If the two ends are not under the same control, the signature is asymmetric: the source holds a private key and the recipient verifies with the public one.

A signature protects against forgery, but not against a substituted address: the recipient should dereference identifiers rather than execute commands and URLs it was sent.

Reliability and timing

Networks are unreliable, so the source retries delivery on a schedule with growing pauses — exponential backoff with some jitter. In the Standard Webhooks example, attempts are made after 5 seconds, 5 minutes, 30 minutes, 2, 5, 10, 14, 20 and 24 hours.

RuleIn practice
Respond quicklyfinish within 3–10 seconds; GitHub closes the connection after 10, Microsoft Graph after 3
Accept first, think laterput the event in a queue and process it in the background, replying 2xx
Idempotencyretries arrive with the same event identifier, so deduplicate on it
Catch gapsif the recipient was down longer than the retry schedule, backfill events through the API or the delivery log

Some sources, such as Azure Event Grid and Microsoft Graph, first validate the URL: they send a test request with a confirmation code and expect the recipient to return it.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • WebSocket protocol

    WebSocket keeps a persistent two-way connection and sends messages in both directions; a webhook is a single HTTP request from the source to the recipient.

  • APIapplication programming interface

    With an API the client pulls data whenever it wants; with a webhook the server pushes data when an event happens.

In depth in the references

where the topic is covered in detail