What is a webhook?

A webhook is an HTTP request that one system sends to a URL you provide when an event happens, such as a payment being captured. Your system is told about the change instead of having to keep asking.

Also called webhooks, webhook URL, webhook endpointUpdated

A webhook is an HTTP request that one system sends to another when an event happens. You give a provider a URL, the webhook URL, and choose which events you want. When a payment is captured or a document is signed, the provider sends a POST request with a JSON description of the event to that URL. Your system reacts straight away, without having to keep asking.

Webhook vs API and WebSocket

With an ordinary API call, your system asks "has payment pay_29 succeeded yet?" every few seconds. Most answers are "no", and you still find out late. With a webhook, the provider tells you once, when it happens. That is why webhooks are sometimes called reverse APIs. Most integrations use both: the API to create things and fetch current state, and webhooks to hear about changes.

A WebSocket is a long-lived, two-way connection, usually between a browser or app and your server, for live updates such as chat. A webhook is a single HTTP request between servers, and no connection stays open.

Example: a Razorpay payment webhook

A customer pays at checkout, and Razorpay sends a payment.captured event to https://api.yourapp.in/webhooks/razorpay. A correct handler in Node.js:

app.post("/webhooks/razorpay", express.raw({ type: "application/json" }), async (req, res) => {
  const expected = crypto
    .createHmac("sha256", process.env.RAZORPAY_WEBHOOK_SECRET)
    .update(req.body)              // the raw bytes, not re-serialised JSON
    .digest("hex")
  // constantTimeEqual wraps crypto.timingSafeEqual
  if (!constantTimeEqual(req.get("X-Razorpay-Signature"), expected)) {
    return res.status(400).end()
  }
  await db.query(
    "INSERT INTO webhook_events (id, body) VALUES ($1, $2) ON CONFLICT (id) DO NOTHING",
    [req.get("x-razorpay-event-id"), req.body.toString()]
  )
  res.status(200).end()            // a worker processes stored events
})

The handler does three things in order:

  1. Verifies the signature. Razorpay computes an HMAC-SHA256 of the raw request body with your webhook secret as the key and sends it in the X-Razorpay-Signature header. Compute it over the raw body, as Razorpay's documentation warns, or it won't match.
  2. Removes duplicates. Razorpay says the same event can arrive more than once and gives every event a unique x-razorpay-event-id header. The primary key on id turns a repeat into a no-op. This is idempotency applied to webhooks.
  3. Responds quickly. It stores the event and returns 200. A worker, often fed by a message queue, does the real work: mark the order paid, post to the ledger and send the receipt. Slow handlers cause timeouts, and timeouts cause retries.

Razorpay also warns that events may arrive out of order, so payment.captured can come before payment.authorized. The worker should move the order forward based on its current state, and ignore an event that would move it backwards.

Common mistakes

  • Skipping signature checks. Anyone who discovers the URL can post fake events to it.
  • Treating webhooks as the only source of truth. Deliveries do get missed. Run a daily reconciliation against the provider's API.
  • Rotating the secret carelessly. Razorpay notes that retries of older events are signed with the old secret, so validate those with the old one.
  • Testing only the happy path. Tools such as webhook.site show you real payloads, but also test duplicates, out-of-order events and your endpoint being down.

Our Razorpay vs Cashfree vs PayU vs Stripe guide compares how Indian payment gateways handle integrations like this.

Any idea in mind? Let's make it real.

Tell us what you're building. You'll hear back from an engineer, not a sales team, usually within one working day.

Book a 30-min call