What Is a Webhook? (And How It Differs From an API)
A webhook is like an API in reverse โ instead of you asking, it tells you when something happens. Here's how they work.
A webhook is a reversed API call. Instead of your app repeatedly asking an API ‘has anything changed?’, the service sends an HTTP POST to a URL you provide, the moment an event happens. Webhooks are how you get told about a completed payment, a new message, a finished video render or a git push — without polling.
Polling vs. webhooks
| Polling | You call GET /orders?since=... every minute. Simple, but wasteful, and you learn about events up to a minute late. |
| Webhook | The service calls POST https://yourapp.com/hooks/orders within seconds of the event. Efficient and near-real-time, but you must run a public endpoint. |
Setting one up
- 1. Build an endpoint on your server that accepts
POSTwith a JSON body. - 2. Register its URL with the provider (dashboard or an API call), and choose which event types to receive.
- 3. The provider sends a test event; confirm you return
2xxquickly. - 4. Parse the payload, verify the signature, and act on it.
Verifying the payload is genuine
Your webhook URL is public, so anyone could POST fake events to it. Providers sign each request — typically an HMAC of the raw body using a shared secret, in a header like X-Signature or Stripe-Signature. Recompute the HMAC over the raw, unparsed body and compare. Reject anything that doesn’t match.
import hmac, hashlib
def verify(raw_body: bytes, header_sig: str, secret: str) -> bool:
expected = hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, header_sig)Handling them reliably
- Respond fast (under a few seconds) with
2xx, then do slow work in a background job. Providers retry if you’re slow, causing duplicates. - Be idempotent — the same event can arrive more than once. Store the event id and skip duplicates.
- Expect out-of-order delivery — use timestamps/versions in the payload, not arrival order.
- Return 2xx only when you’ve safely stored the event. A
5xxor timeout triggers the provider’s retry schedule (often exponential backoff over hours or days). - Log everything — raw body, headers, your response — for debugging.
Developing locally
Your laptop has no public URL, so use a tunnel — ngrok, cloudflared, or a provider’s own CLI (e.g. stripe listen) — to forward public webhook traffic to localhost. Most dashboards also let you re-send a past event for testing.
Webhooks vs. other push options
- WebSockets / SSE keep a live connection open for a client (often a browser). Webhooks are server-to-server and connectionless.
- Polling is the fallback when a provider has no webhooks, or your endpoint can’t be public.
- Message queues (SNS/SQS, Pub/Sub) are what large systems use internally; some APIs can deliver events straight to them.
FAQ
What is the difference between an API and a webhook?
With an API you make the request when you want data. With a webhook the provider makes the request to you when an event happens. They are complementary.
How do I know a webhook really came from the provider?
Verify the signature header against an HMAC of the raw request body using the signing secret from your dashboard. Never trust an unverified webhook.
What should my endpoint return?
A 2xx status as fast as possible, after you have durably stored the event. Do the actual processing asynchronously so a slow job doesn’t cause retries.
Why am I getting the same webhook twice?
Retries and at-least-once delivery. De-duplicate on the event id, and make your handler idempotent so processing the same event twice is harmless.
Keep going
Try it on a real API
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ