What Is a Webhook? Webhooks vs API Polling
A webhook is an HTTP request a service sends to your URL when an event happens. Compare webhooks with API polling and learn to verify webhook signatures in Python.
Short answer: A webhook is an HTTP request that one system sends to your URL automatically when an event happens, such as a payment succeeding. Instead of your code asking "anything new?" again and again (API polling), the other system tells you the moment something changes. Webhooks are faster and lighter; polling is simpler and works anywhere.
What is a webhook, in plain words?
A webhook is a "don't call us, we'll call you" arrangement between two systems. With a normal API call, your code sends a request and gets a response. With a webhook, you give a service a URL on your server, choose the events you care about, and the service sends a request to that URL whenever one of those events happens. The request is usually a POST with a JSON body that describes the event.
Common examples: a payment provider tells your shop that an order was paid, a code host tells your build server that new code was pushed, or a form tool sends each new submission to your app.
What is API polling?
API polling means your code calls an API on a schedule to check for changes, for example every minute: "are there new orders since last time?". Polling needs nothing special from your side: no public URL, no incoming traffic. The cost is that most calls return nothing new, and a change that happens just after a check waits until the next one.
Webhooks vs API polling: which is better?
Webhooks are better when you need to react quickly to events and the provider offers them; polling is better when you cannot receive incoming requests or the provider has no webhooks. Here is the comparison:
Webhook | API polling | |
|---|---|---|
Who starts the request | The other system (push) | Your code (pull) |
Speed | Near real time | Delayed by up to one polling interval |
Wasted calls | Few: only real events | Many: most checks find nothing |
What you need | A public HTTPS endpoint | Just outgoing requests |
Missed events | Possible if your server is down; providers often retry | Unlikely; the next check catches up |
Security work | Verify every request is genuine | Protect your API key |
Good for | Payments, pushes, form submissions | Scripts, laptops, APIs without webhooks |
Many real systems use both: webhooks for speed, plus an occasional polling job that catches anything a webhook missed.
How do you receive a webhook safely?
Receiving a webhook safely starts with remembering that your webhook URL is public, so anyone who finds it can send fake events. Many providers therefore sign each request: they compute a hash of the body with a secret only you and they know, and send it in a header. Your server recomputes the hash and compares. The script below shows the idea with Python's standard library and runs as-is; check your provider's documentation for its exact header name and signing format.
import hashlib
import hmac
SECRET = b"demo-shared-secret" # shared secret from the provider's dashboard
def sign(body):
return hmac.new(SECRET, body, hashlib.sha256).hexdigest()
def is_genuine(body, signature):
return hmac.compare_digest(sign(body), signature)
body = b'{"event": "payment.succeeded", "id": "evt_123"}'
signature = sign(body) # the sender puts this in a header
print(is_genuine(body, signature)) # real request
print(is_genuine(body, "forged-signature")) # fake signature
tampered = body.replace(b"evt_123", b"evt_999")
print(is_genuine(tampered, signature)) # body was changedRunning it prints:
True
False
Falsehmac.compare_digest compares in constant time, which avoids leaking hints through timing. Keep the secret in an environment variable, never in your code.
What should a webhook endpoint do after verification?
Reply fast with a 2xx status code, then do slow work in the background. Slow replies may be treated as failures and retried.
Expect duplicates. Retries mean the same event can arrive twice. Store each event ID and skip ones you have already handled (this is called idempotency).
Do not trust order. Events can arrive out of sequence; check timestamps or fetch the latest state from the API.
Log everything so you can replay or debug a failed event.
Common mistakes
Skipping signature checks and trusting any request that reaches the URL.
Doing heavy work before replying, which causes timeouts and repeated deliveries.
Processing the same event twice, for example sending two confirmation emails.
Polling far too often and hitting the provider's rate limits. The chapter on reliable API calls covers timeouts and retries.
Frequently Asked Questions
Is a webhook an API?
A webhook is a way of using HTTP in the opposite direction to a normal API call. The provider calls an endpoint you built, so in practice your webhook receiver is a small API that the provider uses.
Can I test webhooks on my own computer?
Your laptop usually has no public address, so a provider cannot reach it directly. Developers use the provider's test tools, a tunnelling tool that exposes a local port, or a test server while building.
What happens if my server is down when a webhook is sent?
Many providers retry failed deliveries for a while, but the details differ, so read their documentation. A periodic polling job is a good safety net for anything that was never delivered.
Are webhooks real time?
Webhooks are close to real time: the request is sent soon after the event, usually much sooner than a polling job would notice. Network delays and retries mean you should not rely on exact timing.
Key Takeaway
A webhook pushes an event to your URL the moment it happens; polling pulls by asking on a schedule. Use webhooks for speed, verify every request, reply fast and handle duplicates, and keep polling as a simple fallback.
Ready to start? You will learn HTTP, APIs and backend basics step by step, then build and test your own API. Begin with the free course Git, APIs and Backend for AI: Beginner.
Related articles
More in APIs and Backend
What Is a Pull Request? A Beginner's Guide to Code Review
A pull request is a proposal to merge one branch into another, reviewed by teammates first. Learn the step-by-step cycle, a description template, what reviewers check and the three merge methods.
6 min readDec 2, 2026How to Resolve a Git Merge Conflict
A step-by-step guide to resolving a Git merge conflict, using a real conflict created from scratch, with the output at each step and how to cancel safely.
4 min readNov 26, 2026What Is Docker and Why Do AI Developers Use It?
Docker packs your app and everything it needs into a container that runs the same everywhere. Learn images, containers and Dockerfiles, and why AI developers rely on them.
4 min readNov 23, 2026FastAPI vs Flask vs Django for AI Apps
Which Python web framework should you use for an AI app? A plain comparison of FastAPI, Flask and Django, when each fits, and a tiny working endpoint.
4 min readNov 19, 202615 Essential Git Commands (Cheat Sheet)
The 15 Git commands you will use almost every day, each with a one-line purpose and an example, plus a daily workflow and common mistakes.
4 min readNov 17, 2026How to Keep API Keys Safe: .env Files and Secrets
Keep API keys out of your code: use .env files and environment variables, ignore them in Git, and know exactly what to do if a key leaks.
4 min readNov 14, 2026
Comments
Sign in to join the discussion.

