How API Webhooks Work
How API webhooks work, for beginners, with a real GitHub webhook calling a small Flask app on a laptop: polling vs webhooks measured, what GitHub sends (headers, body, signature), why anyone can call your URL and how an HMAC signature stops them, why you must answer within 10 seconds, and what to do about missed and repeated deliveries.

Polling vs a webhook
Your app needs to know when new code lands on GitHub. So every five seconds, it asks: anything new? Eight checks. Seven said no.
poll.sh19:34:07 check 1: nothing new 19:34:12 check 2: nothing new 19:34:18 check 3: nothing new 19:34:23 check 4: nothing new 19:34:28 check 5: nothing new 19:34:34 check 6: nothing new 19:34:39 check 7: nothing new 19:34:45 check 8: new commit 9ac4f38
It's the API version of: are we there yet? A webhook flips it around: GitHub calls your app, the moment it happens.
We built both, for real, and timed them. The webhook took one request, and 1.8 seconds after the push, the site was already updated.

Along the way, a fake request will deploy your site, meaning put new code live. And a deploy that worked will get counted as a failure.
Polling every five seconds, all day, is seventeen thousand two hundred and eighty requests, nearly all of them to hear: nothing new. And you can still hear about a push five seconds late, unless you ask even more often.

Picture waiting for a parcel. Polling is walking to the mailbox every five minutes. A webhook is the doorbell: the courier rings it when the parcel is there. You only need two things: give them your address, and answer the door.
Your first webhook
So what is a webhook, exactly? It's an ordinary web request that a service sends to a URL you gave it, when an event happens. You set it up once, with three things: the URL, the events you want, and a secret: a shared password. We'll see what it's for soon. On your side, you run a small web app that listens at one address for these requests. We'll call it the receiver.
You've likely met webhooks already: Stripe telling a shop a payment went through, or a form service sending each new signup.

Two web words before we build one. GET asks for something; POST sends something, and a webhook is a delivery, so it's a POST. And every answer starts with a three-digit status code. 200 means OK: got it. We'll meet a few others along the way.

Here's a receiver, in Python with Flask. One route: slash webhook, for POST requests. It reads two headers, short labels that come with the request: which event this is, and a delivery id, a label GitHub puts on each message it sends. On a push, it deploys: it pulls the new code into your site. Then it answers 200, which means: got it.
GitHub can't reach a laptop at home. So for testing, we run a free Cloudflare quick tunnel. It gives us a random public web address. We save that address in a variable called URL, for the next command. Anything sent to it gets passed through to the receiver on the laptop. No account needed. On a real server, you'd skip this step. And the receiver itself runs in a second window.
cloudflared tunnel --url localhost:5000 > ~/tunnel.log 2>&1 &[1] 398
URL=$(grep -o 'https://[a-z0-9-]*\.trycloudflare\.com' ~/tunnel.log); echo $URLhttps://sept-vehicles-trembl-makeup.trycloudflare.com
python3 hook.pylistening on port 5000 19:35:14 ping a8e56dc6 200 19:35:18 push aae138bc 200 deployed 0d49431 19:35:20 push ? 200 deployed 0d49431
Now give GitHub the three things, with its API: the URL, the push event, and the secret. The secret is just a long random password, saved in a file. Only we and GitHub know it. GitHub answers with the new webhook's id. And straight away, it sends a first test event, called a ping. The receiver got it: 200.
export HOOK=$(gh api repos/$REPO/hooks --jq .id \ -f config[url]=$URL/webhook -f config[content_type]=json \ -f config[secret]=$(cat ~/.hook-secret) -f events[]=push); echo $HOOK720245752
19:35:14 ping a8e56dc6 200
jq '.body | {zen, hook_id}' ~/app/last.json{ "zen": "Practicality beats purity.", "hook_id": 720245752 }
Now a real push, which uploads a new commit to GitHub. Commit, push, and 1.8 seconds later, the receiver logs it: a push event, and it deployed the new commit. Nobody asked. GitHub called.
echo '<p>New: gift cards</p>' >> index.html
git commit -qam "Add gift cards" && git push -q
19:35:18 push aae138bc 200 deployed 0d49431
What did GitHub actually send? First, the headers, all the labels this time. The event's name: push. A delivery id, unique to this delivery. And a signature. Keep that one in mind.
jq '.headers | with_entries(select(.key | test("^X-(Github|Hub)"; "i")))' ~/app/last.json{ "X-Github-Delivery": "aae138bc-ef50-11f1-8673-1a4d99527225", "X-Github-Event": "push", "X-Github-Hook-Id": "720245752", "X-Github-Hook-Installation-Target-Id": "1482585101", "X-Github-Hook-Installation-Target-Type": "repository", "X-Hub-Signature": "sha1=831f0feee84096fa30d7f9259244d294e26ebe8c", "X-Hub-Signature-256": "sha256=27384a150dea2ce28cce35932343a7011fe6382ce8dcd64037e23b888e744e9c" }
Then the body, in JSON: which branch, the new commit, and its message. A push sends much more than this. Your app reads what it needs and ignores the rest. And GitHub keeps its own record of every delivery. A small helper script we wrote, called deliveries, shows it: two deliveries, the ping and the push, both answered 200. For the push, GitHub timed under a second from sending to getting our 200, deploy included.
jq '.body | {ref, after, message: .head_commit.message}' ~/app/last.json{ "ref": "refs/heads/main", "after": "0d49431ea63dd1f10abca28a836ae960c9c61e60", "message": "Add gift cards" }
deliveries 219:35:18 push 200 0.96 aae138bc 19:35:14 ping 200 0.21 a8e56dc6
Anyone can call your URL
Here's the catch. Your URL is just an address, and anyone who finds it can send a request. This push doesn't come from GitHub. We send it ourselves, with curl, a command-line tool for web requests. Guess first: does the receiver deploy? It does. 200, deployed. Your app can't tell who called.
cat fake.json{"ref": "refs/heads/main", "after": "0000000000000000000000000000000000000000", "head_commit": {"message": "not from GitHub"}}
curl -X POST $URL/webhook -H 'X-GitHub-Event: push' --json @fake.json -w ' %{http_code}\n'ok 200
19:35:20 push ? 200 deployed 0d49431
That's what the secret is for. GitHub mixes the secret and the exact bytes of the body into a short signature, like a fingerprint. Only someone with the secret can make that same signature. The method is called HMAC-SHA256. GitHub sends it in the signature header. Your receiver makes its own signature from the body it got, and compares. Change one byte, and they don't match.

In Python, it's a few lines. Make the signature from the raw body and the secret. Then compare it with the header, using Python's compare digest. It's a safe compare: it takes the same time whether the first character differs or the last, so an attacker can't guess the signature by timing your answers. If they don't match, answer 401, unauthorized, and stop.
Restart the receiver with the check in place, and send the fake again: 401, bad signature. Now a real push: signature OK, deployed.
curl -X POST $URL/webhook -H 'X-GitHub-Event: push' --json @fake.json -w ' %{http_code}\n'bad signature 401
19:35:22 push ? 401 bad signature
One mistake catches a lot of people. Flask hands you the body already unpacked into Python data, and it's tempting to turn that back into JSON and check the signature on that. Let's try it, and push a real commit. 401: GitHub's real push fails, in its own record too. Python writes JSON its own way, the spacing can differ, and those aren't the bytes GitHub signed. Always check the signature on the raw bytes, exactly as they arrived.
git commit -qam "Holiday hours" && git push -q
19:35:30 push b2b4ae0c 401 bad signature
deliveries 119:35:30 push 401 0.26 b2b4ae0c
Answer within 10 seconds
What if the work takes a while? Say your deploy also builds the site, and the build takes twelve seconds. To fake that build, we add one line: wait twelve seconds, before the receiver answers.
Push. The receiver pulls the new code and builds it: 12.5 seconds in all. Everything worked. Guess first: what does GitHub record for this delivery? Failed. It waited ten seconds for an answer, then hung up: timed out.
19:35:51 push b763d126 200 signature OK, deployed 78d3efb in 12.5 s
deliveries 119:35:48 push 500 10 b763d126
gh api repos/$REPO/hooks/$HOOK/deliveries --jq '.[0].status'POST https://sept-vehicles-trembl-makeup.trycloudflare.com/webhook giving up after 1 attempt(s): Post "https://sept-vehicles-trembl-makeup.trycloudflare.com/webhook": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
GitHub's docs say so: answer within ten seconds, or the delivery counts as a failure.
The fix: answer first, do the work after. The receiver checks the signature, then hands the deploy to a background thread: a separate piece of work that runs on its own. So it can answer 202 straight away, which means: accepted, I'll get to it. In a bigger app, you'd put the job on a queue, for another program to pick up.
Push again. 202, in 0.09 seconds, and GitHub records a success. The build still takes 12.6 seconds, but now nobody is waiting on it.
19:35:59 push c3b04676 202 signature OK, queued
deliveries 119:35:59 push 202 0.09 c3b04676
Missed and repeated deliveries
What if your server is down at the exact moment GitHub calls? Stop the receiver, and push. GitHub gets an error from the tunnel: 502, bad gateway, meaning nobody answered behind it. We wait five minutes. Still just one attempt. That push is simply missed.
git commit -qam "Phone number" && git push -q
deliveries 119:36:14 push 502 0.28 cd042792
deliveries 319:36:14 push 502 0.28 cd042792 19:35:59 push 202 0.09 c3b04676 19:35:48 push 500 10 b763d126
That's by design. GitHub's docs: it does not redeliver failed deliveries automatically.
So once the receiver is back, you ask for it again, with GitHub's command-line tool, gh: one API call with the delivery's number. The missed push arrives, and deploys.
ID=$(gh api repos/$REPO/hooks/$HOOK/deliveries --jq '.[0].id'); echo $ID3857581946076921856
gh api -X POST repos/$REPO/hooks/$HOOK/deliveries/$ID/attempts{}
19:41:32 push cd042792 202 signature OK, queued
But a redelivery, or a service's automatic retry, can bring the same event twice. Stripe, for one, retries failed deliveries for up to three days, and says an event can arrive more than once. You don't want two deploys. So our receiver writes down every delivery id it handles, and skips any it has already seen.
Send that same request once more, as a retry would. Same delivery id, and the receiver skips it: already handled. GitHub's record shows one id sent three times: the attempt that failed, the redelivery that deployed, and this repeat, which was skipped. One deploy.
gh api -X POST repos/$REPO/hooks/$HOOK/deliveries/$ID/attempts{}
19:41:46 push cd042792 200 already handled, skipped
deliveries 319:41:46 push 200 0.33 cd042792 19:41:32 push 202 0.32 cd042792 19:36:14 push 502 0.28 cd042792
And since a delivery can be missed, many apps do both: webhooks for speed, plus a slow poll, maybe once an hour, to catch anything that never arrived. Or, like GitHub's docs suggest, a small script on a schedule that lists failed deliveries and redelivers them.
The four rules
So: a webhook is a POST to your URL when something happens. Check the signature on the raw body. Answer within ten seconds, and do the work later. And expect deliveries to go wrong: skip ids you've already handled, and redeliver the ones that never arrived.

If your app can hear a push now, instead of asking for one, subscribe for more of how the web really works. And a like helps YouTube show this to more beginners.
The receiver code, the helper scripts, and every command are linked in the description, so you can wire up a webhook of your own. Thanks for watching Cloud Explainers.
Sources
- GitHub Docs, About webhooks
- GitHub Docs, Best practices for using webhooks
- GitHub Docs, Validating webhook deliveries
- GitHub Docs, Handling failed webhook deliveries
- GitHub REST API, Repository webhooks (create, list deliveries, redeliver)
- Stripe Docs, Receive Stripe events in your webhook endpoint
- Cloudflare Docs, Quick Tunnels
Demos: a laptop container, a private GitHub repository made for this video, and a free Cloudflare quick tunnel. The site is made up; every command, output and number was real. Watch the full video on YouTube.