A webhook is a way for one app to send data to another app the moment something happens.
Instead of your app constantly asking βdid anything change yet?β (thatβs called polling), the other app just tells you when something happens. Itβs like the difference between refreshing your email every 10 seconds vs. getting a push notification.
How it works
- You give App B a URL (your webhook endpoint)
- When something happens in App B, it sends an HTTP POST request to that URL
- Your server receives the data and does something with it
Thatβs it. A webhook is just a URL that receives POST requests.
Real-world examples
- GitHub β sends a webhook to your server every time someone pushes code
- Stripe β sends a webhook when a payment succeeds or fails
- Discord β you can send messages to a channel by POSTing to a webhook URL
- Shopify β sends a webhook when someone places an order
Webhook vs API
| API | Webhook | |
|---|---|---|
| Who initiates? | You ask for data | They send you data |
| Direction | You β them | Them β you |
| Timing | Whenever you ask | When something happens |
| Also called | Pull | Push |
Think of an API as βIβll call youβ and a webhook as βdonβt call me, Iβll call you.β
A simple example
Hereβs a Discord webhook in action. Discord gives you a URL, and you POST a message to it:
curl -X POST "https://discord.com/api/webhooks/your-id/your-token" \
-H "Content-Type: application/json" \
-d '{"content": "Hello from a webhook!"}'
Thatβs a complete webhook call. Discord receives it and posts the message in your channel.
Receiving webhooks
If you want to receive webhooks (e.g., from GitHub or Stripe), you need a server with a public URL that can handle POST requests:
from flask import Flask, request
app = Flask(__name__)
@app.route("/webhook", methods=["POST"])
def handle_webhook():
data = request.json
print(f"Received webhook: {data}")
# Do something with the data
return "OK", 200
The key things to remember:
- Webhooks are always POST requests
- The data comes as JSON in the request body
- You should return a 200 status code quickly (process heavy work in the background)
- Always verify the webhook is legit (most services include a signature header)
When to use webhooks
Use webhooks when you need real-time reactions to events. Common use cases:
- CI/CD pipelines β trigger a build when code is pushed (GitHub Actions uses this)
- Payment processing β update your database when Stripe confirms a payment
- Chat notifications β send alerts to Discord or Slack when something happens
- Monitoring β get notified when your server goes down