API Directory โ€บ Guides โ€บ What Is a Webhook? (And How It Differs From an API)

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

PollingYou call GET /orders?since=... every minute. Simple, but wasteful, and you learn about events up to a minute late.
WebhookThe 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

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

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

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

Browse all 196 free APIs โ†’

๐Ÿ”Œ Explore free public APIs

Hundreds of free APIs, searchable by category, with auth & CORS at a glance.

Browse the directory โ†’