Instead of you asking again and again, the other system calls you when something happens.
A webhook is a message that one system sends to your URL when something happens. You do not keep asking. They call you.
Normally you call an API. With a webhook, you give the other system a URL, and they call you when an event happens.
POST /webhooks/payments HTTP/1.1
Content-Type: application/json
X-Signature: sha256=9f2b61c0…
X-Event-Id: evt_1042
{
"id": "evt_1042",
"type": "payment.succeeded",
"data": { "order": "A-77", "amount": 49900, "currency": "INR" }
}| Part | Job |
|---|---|
| Event | Something that happened, such as invoice.paid |
| Endpoint URL | Your public HTTPS address that receives the call |
| Payload | The JSON that describes the event |
| Signature header | Proof that the sender is real and the body was not changed |
| Event id | A unique id, used to ignore repeats |
| Retry policy | How the sender tries again when you do not reply with 2xx |
POST /webhooks/pay
X-Signature: sha256=9f2b…(HMAC of body + secret)
Idempotency/Event-Id: evt_1042
{ "type": "payment.succeeded", "id": "evt_1042" }import crypto from "node:crypto";
function isValid(rawBody, headerSig, secret) {
const expected = "sha256=" + crypto.createHmac("sha256", secret).update(rawBody).digest("hex");
const a = Buffer.from(expected);
const b = Buffer.from(headerSig);
return a.length === b.length && crypto.timingSafeEqual(a, b);
}| Term | Meaning in easy words |
|---|---|
| Webhook | An HTTP request sent to your URL when an event happens |
| Callback URL / endpoint | The address you give the sender |
| Payload | The data inside the message |
| HMAC | A secret-key checksum that proves who sent it and that it was not changed |
| Shared secret | A key known only to you and the sender |
| Idempotent | Safe to process the same event more than once |
| Retry / backoff | Trying again later, waiting longer each time |
| Replay attack | Sending a copy of an old valid message again |
| Dead letter | A message that kept failing and is parked for a human to look at |
| 2xx | A success status code (200, 202, 204) |
| Polling | Webhook | SSE / WebSocket | |
|---|---|---|---|
| Who starts it? | You | The other server | Server pushes over your open connection |
| Talks to | Server | Server (your backend) | Browser or app |
| Delay | Up to polling gap | Seconds | Instant |
| Connection | New each time | New for each event | Stays open |
| Good for | Simple checks | Server-to-server events | Live UI updates |