Dev tools
Webhook vs API: What's the Difference?
Webhook vs API: an API answers when you ask, a webhook calls you when something happens. See how each works, when to use which, and how to secure webhooks.
Webhook vs API is a question every developer and no-code builder runs into when connecting two systems. With an API, your app asks another service for data whenever it wants. With a webhook, the other service calls your app automatically when something happens, like a payment succeeding or a form being submitted. One is "pull," the other is "push," and most real integrations use both.
This guide explains what an API is, what a webhook is, how they compare, why webhooks beat polling for event updates, real examples, how to secure and handle webhooks reliably, and how to choose between them.
Webhook vs API at a Glance
| API | Webhook | |
|---|---|---|
| Direction | Your app asks, the service answers | The service sends to your app |
| Trigger | Your request | An event, such as "payment succeeded" |
| Pattern | Pull (request-response) | Push (event notification) |
| Timing | Whenever you call it | As soon as the event happens |
| What you build | Code that calls the service | A public URL (endpoint) that receives calls |
| Typical use | Read or change data on demand | Get notified about changes |
| Analogy | Calling a shop to ask if your order is ready | The shop texting you when it's ready |
The short version: an API is something you call; a webhook is something that calls you.
What Is an API?
An API (application programming interface) is a defined way for one program to talk to another. On the web, that usually means sending HTTP requests to a service's URLs and getting data back, often as JSON.
A REST API is the most common style. You use standard HTTP methods on resources:
GET /orders/123to read an orderPOST /ordersto create onePATCH /orders/123to update itDELETE /orders/123to remove it
The key feature: you start the conversation. The service doesn't contact you; it waits for your request and responds. That's perfect when you need data at a specific moment, such as loading a customer's profile when they open a page.
What Is a Webhook?
What is a webhook? It's an automatic HTTP request a service sends to a URL you provide when a specific event happens. It's sometimes called a "reverse API" or "HTTP callback."
Here's how it works:
- You register a URL with the service, such as
https://yourapp.com/webhooks/payments, and choose which events you care about. - An event happens: a payment succeeds, code is pushed, a form is submitted.
- The service sends an HTTP POST to your URL with details of the event, usually as JSON.
- Your app checks the request is genuine, replies quickly with a success status (2xx), and processes the event.
You don't ask; you're told. That makes webhooks ideal for reacting to things as they happen.
The webhook URL (also called the webhook endpoint or callback URL) is simply the address on your server that receives these requests. It must be publicly reachable, and in production it should use HTTPS.
A Simple Webhook Example, Step by Step
Here's a typical webhook example for an online shop taking payments:
- A customer pays at checkout. The payment provider takes the card details on its side.
- A few seconds later, the payment is confirmed. The provider sends an HTTP POST to your webhook URL.
- The body is JSON describing the event. It typically looks something like this:
{
"id": "evt_123",
"type": "payment.succeeded",
"data": { "payment_id": "pay_456", "amount": 4900, "currency": "usd" }
}- Your endpoint checks the signature, records the event ID so it won't process the same event twice, and replies with a 2xx status straight away.
- A background worker then marks the order as paid, sends the receipt email and, if needed, calls the provider's API to fetch more details.
The exact field names differ between providers, but the flow is almost always the same: event, POST, verify, acknowledge, process.
Webhooks vs Polling
Before webhooks, the usual way to find out about changes was polling: calling an API over and over to ask "anything new?"
Imagine waiting for a payment to clear. With polling, your app might call the payment API every 30 seconds. Most of those calls return "nothing yet," wasting requests, server time and possibly rate limits. And you still find out up to 30 seconds late.
With a webhook, the payment service sends one request the moment the payment clears. No wasted calls, no delay.
Polling still has a place:
- When a service doesn't offer webhooks.
- When your app can't expose a public URL, such as on a laptop behind a firewall.
- As a backup to catch anything a webhook missed.
Webhook vs API: Real-World Examples
- Payments: payment providers send webhooks when a charge succeeds, fails or is disputed. Your app then uses the API to fetch full details or update the order.
- Code hosting: GitHub can send a webhook on every push or pull request, which triggers your build or deployment pipeline.
- Chat tools: many chat apps accept incoming webhooks, so other systems can post messages to a channel with a single HTTP request.
- Forms and CRMs: a form tool sends a webhook when someone submits it, and an automation adds the lead to your CRM.
- No-code automation: tools like n8n and Zapier start workflows from webhook triggers. Our n8n tutorial shows how.
- AI workflows: long-running jobs, such as batch processing or a slow agent task, are a natural fit for webhooks: start the job with an API call, then get notified when it finishes instead of waiting on an open connection.
Notice the pattern: the webhook says what happened, and the API is used to act on it or get more detail.
Webhook Security
A webhook endpoint is a public URL. Anyone who finds it can send fake requests unless you check where they come from. Webhook security comes down to a few practices.
Verify signatures
Most providers sign each webhook with a secret you share with them, using an HMAC hash of the request body.
- GitHub sends the signature in the
X-Hub-Signature-256header, computed with HMAC-SHA256 and starting withsha256=. Its docs warn never to compare signatures with a plain==, and to use a constant-time comparison instead. - Stripe signs events in the
Stripe-Signatureheader, which includes a timestamp as part of the signed payload. Its libraries reject events older than a tolerance of 5 minutes by default, which helps stop replay attacks.
Always verify against the raw request body. If your framework parses and re-serialises the JSON first, the signature won't match.
Other protections
- Use HTTPS so payloads can't be read or changed in transit.
- Allowlist IP addresses if the provider publishes them.
- Rotate secrets periodically and whenever you suspect a leak.
- Don't trust the payload blindly. For sensitive actions, fetch the latest state from the provider's API before acting.
Example: verifying a GitHub webhook in Python
This follows the approach in GitHub's documentation, using Flask:
import hashlib
import hmac
from flask import Flask, request, abort
app = Flask(__name__)
SECRET = b"your-webhook-secret"
@app.post("/webhooks/github")
def github_webhook():
signature = request.headers.get("X-Hub-Signature-256", "")
expected = "sha256=" + hmac.new(SECRET, request.get_data(), hashlib.sha256).hexdigest()
if not hmac.compare_digest(expected, signature):
abort(403)
event = request.headers.get("X-GitHub-Event")
payload = request.get_json()
# put the work on a queue here, then reply fast
return "", 204Handling Webhooks Reliably
Webhooks travel over the internet, so things go wrong: your server is down, a request times out, a message arrives twice. Good providers and good receivers plan for it.
- Reply fast. Return a 2xx status quickly and do the heavy work afterwards, ideally through a queue. Stripe's docs say your endpoint must return a success code before any complex logic that could cause a timeout.
- Expect retries. If you don't reply with success, providers retry. Stripe, for example, retries for up to three days with exponential back-off in live mode.
- Handle duplicates. The same event can arrive more than once. Store the event ID and skip events you've already processed. This is called making your handler idempotent.
- Don't rely on order. Events may arrive out of order. Stripe states plainly that it doesn't guarantee delivery order, so design your logic to cope, for example by fetching the current state from the API.
- Log everything so you can replay or debug missed events.
When to Use a Webhook vs an API
Use an API when:
- You need data at a specific moment, like when a user opens a page.
- You want to create, update or delete something.
- The data changes rarely, or you need a full list rather than individual changes.
Use a webhook when:
- You need to react quickly to events, like payments, sign-ups or code pushes.
- Polling would waste lots of requests.
- The event can happen at any time, and you don't want to keep checking.
Use both when you're building a real integration. The webhook tells you something changed; the API gives you the details and lets you act. That combination is the standard pattern for payments, CRMs and automation platforms.
Webhook vs Callback
A callback, in programming, is a function you hand to another piece of code so it can call you back when it finishes. A webhook is the same idea across the internet: instead of a function, you hand over a URL, and the other service "calls back" by sending an HTTP request to it. That's why webhook URLs are often called callback URLs.
Webhook vs WebSocket
Webhooks and WebSockets both deliver updates without polling, but they work differently. A webhook is a one-off HTTP request between servers when an event happens. A WebSocket is a long-lived two-way connection, usually between a browser and a server, for live updates such as chat or dashboards. Our guide to server-sent events vs WebSockets covers real-time options for browsers.
In AI apps, these ideas meet: an LLM can call APIs through function calling, while webhooks notify your system when long tasks complete.
FAQ
What is the difference between a webhook and an API?
An API is a way for your app to request data or actions from another service whenever it wants. A webhook is an automatic message the other service sends to your app when a specific event happens. APIs are pull-based; webhooks are push-based.
Is a webhook an API?
In a broad sense, yes: a webhook uses HTTP like an API, and the provider calls an endpoint you expose. But it flips the direction. Instead of you calling the service, the service calls you, triggered by events rather than your requests.
Why use a webhook instead of polling?
Polling means calling an API repeatedly to check for changes, which wastes requests and still reports changes late. A webhook sends one request the moment something happens, so updates are faster and cheaper. Polling remains useful when webhooks aren't available.
How do you secure a webhook?
Verify the signature the provider sends with each request, using a shared secret and the raw request body, with a constant-time comparison. Also use HTTPS, check timestamps to block replays, allowlist the provider's IP addresses when possible and rotate secrets regularly.
What happens if my webhook endpoint is down?
Most providers retry failed deliveries for a while, often with increasing delays. Stripe retries for up to three days in live mode. Your handler should accept duplicate deliveries safely and you can usually replay missed events from the provider's dashboard.