Skip to content

Webhook

Also called: web hook

Published:

How does a webhook work?

You give the service a URL and subscribe to the events you care about. On GitHub, when a subscribed event occurs, GitHub sends an HTTP request containing data about that event to your URL, and your server acts on it [1]. Typical events include code being pushed to a repository, a pull request being opened or a new member joining a team [1]. Stripe works the same way, sending events to a registered HTTPS endpoint as POST requests with a JSON payload. A customer's bank confirming a payment or a recurring charge going through are the kind of asynchronous events you learn about this way [3].

How is a webhook different from polling an API?

With polling, the client keeps asking the API whether anything has changed. With a webhook the data arrives as the event happens, and you subscribe once [1]. GitHub notes that polling many resources can quickly exhaust your API rate limit, while webhooks scale better [1].

How do you verify an incoming webhook?

The endpoint is public, so you need to confirm each request really came from the service you expect. On GitHub you set a random, high-entropy secret. GitHub signs every delivery with an HMAC SHA-256 digest computed from that secret and sends it in the X-Hub-Signature-256 header. Your server computes the same digest and compares the two with a constant-time function, never a plain == [2]. Stripe likewise asks you to verify the Stripe-Signature header against your endpoint secret, and to return a 2xx status quickly before any slow logic that might time out: send the 200 before you mark an invoice as paid in your accounting system, for example [3].

Frequently asked questions

Is a webhook an API?

Not exactly. With an API the client asks for data; with a webhook the service pushes data to the client's URL when an event happens.

Sources

  1. About webhooks — GitHub Docs
  2. Validating webhook deliveries — GitHub Docs
  3. Receive Stripe events in your webhook endpoint — Stripe Docs

Related terms