← Back to the map

Unit 1 · Lesson 7

Webhooks

A customer pays. The payment provider knows instantly. Your shop doesn't, unless someone tells it. Asking "has it been paid yet?" every second would be silly. So the provider calls you instead.

By the end you can

Watch

Prefer reading? The same lesson is written out below.

A webhook is an HTTP request that another service sends to you when something happens. You give the provider a URL, like https://shop.example.com/webhooks/payments, and say which events you care about. When a payment succeeds, the provider POSTs a small JSON message there.

paysPOST /webhooksCustomerPayment providerYour appOrders DB
A customer pays through a payment provider. When the payment succeeds, the provider sends an HTTP POST, a webhook, to your app, which records the order in its database.

It's polling turned around. Instead of you asking every minute, "Is order 81 paid yet?", they tell you once, the moment it happens. Payment providers, code hosts (a push happened), shipping companies (a parcel was delivered) and chat tools all work this way.

Expect retries and duplicates

The sender can't be sure you got the message: your server might be down, slow, or the reply might be lost. So many providers retry until you answer with a 2xx status, often for hours or days with growing gaps between attempts (Stripe keeps trying for up to three days). Others don't retry at all: GitHub, for example, leaves failed deliveries for you to redeliver. Either way:

Check who's calling

Your webhook URL is public, so anyone could send a fake "payment succeeded" to it. Providers sign each request: they compute a signature (usually HMAC) over the body with a secret only you and they know, and send it in a header. Your handler recomputes it and rejects anything that doesn't match. Include a timestamp in the check, too, so an old request can't be replayed.

Key idea: a webhook is the provider calling your URL when an event happens. Expect retries and duplicates (be idempotent), reply fast and do work in the background, and verify the signature on every request.

Check yourself

What is a webhook?

Your new flashcards

Practice them now. With a free account they come back in your daily review.

Card 1 of 3