Webhooks Explained: Signed Payloads and Retries

Backend2026-11-21TryQuickToolBox

You've built an API and now need to notify other services when events happen. Webhooks are the standard solution, but without proper security and reliability, they can become a source of frustration. This guide explains how to implement webhooks with signed payloads and robust retry logic, so your integrations are both secure and dependable.

What Are Webhooks?

Webhooks are user-defined HTTP callbacks. When an event occurs in your application, you send an HTTP POST request to a URL provided by the subscriber. This allows real-time communication between services without polling.

For example, a payment processor might send a webhook to your app when a payment succeeds. Your app then updates the order status. Webhooks are simple to implement but require careful design to handle security and failures.

Why Secure Webhooks with Signed Payloads?

Webhook endpoints are publicly accessible, making them vulnerable to spoofing. An attacker could send fake requests to your endpoint, triggering unwanted actions. Signed payloads solve this by allowing the receiver to verify that the request originated from the sender.

A signed payload includes a cryptographic signature generated using a shared secret. The receiver computes the signature independently and compares it. If they match, the request is authentic.

How to Generate and Verify Signatures

The most common method is HMAC (Hash-based Message Authentication Code) with SHA-256. Here's a typical flow:

  1. The sender and receiver share a secret key.
  2. For each webhook, the sender creates a signature by hashing the payload with the secret using HMAC-SHA256.
  3. The signature is sent in a header, e.g., X-Signature.
  4. The receiver recomputes the signature and compares it to the header value.

Always use a constant-time comparison function to prevent timing attacks. Also, include a timestamp in the signed data to prevent replay attacks.

// Node.js example: generating a signature
const crypto = require('crypto');
const secret = 'your_shared_secret';
const payload = JSON.stringify({ event: 'payment.success', id: 123 });
const timestamp = Date.now();
const signature = crypto.createHmac('sha256', secret)
  .update(`${timestamp}.${payload}`)
  .digest('hex');
// Send headers: X-Signature: signature, X-Timestamp: timestamp

On the receiving side, verify the timestamp is recent (e.g., within 5 minutes) and recompute the signature.

Implementing Reliable Retries

Webhook delivery can fail due to network issues, server downtime, or timeouts. A robust system must retry failed deliveries. However, retries can cause duplicate processing if not handled correctly.

Retry Strategy

Use exponential backoff with jitter. For example, retry after 1 minute, then 5 minutes, then 30 minutes, up to a maximum number of attempts (e.g., 5). Add randomness to avoid thundering herd problems.

Only retry on transient errors (5xx responses or timeouts). Do not retry on 4xx errors, as they indicate a problem with the request that won't be fixed by retrying.

Ensuring Idempotency

Because retries can cause duplicate deliveries, the receiver must handle events idempotently. Include a unique event ID in the payload and have the receiver track processed IDs. If an event is received again, ignore it.

// Example: idempotent handler
const processedEvents = new Set();
function handleWebhook(event) {
  if (processedEvents.has(event.id)) return;
  // process event
  processedEvents.add(event.id);
}

In production, use a database or cache to store processed IDs with an expiration time.

Best Practices for Webhook Security

Comparison of Retry Approaches

ApproachProsCons
Immediate retrySimpleCan overwhelm a recovering service
Exponential backoffReduces load, increases success rateDelayed delivery
Exponential backoff with jitterPrevents synchronized retriesSlightly more complex

FAQ

What is a signed webhook payload?

A signed payload includes a cryptographic signature (e.g., HMAC) that allows the receiver to verify the request's authenticity and integrity.

How many times should I retry a failed webhook?

There's no fixed number, but common practice is 3-5 retries with exponential backoff. After that, log the failure and alert the subscriber.

Can I use webhooks without HTTPS?

No, HTTPS is essential to protect the payload from interception and tampering. Always use HTTPS for webhook endpoints.

When testing your webhook endpoints, you might need to inspect JSON payloads. Use the JSON formatter to pretty-print and validate the data quickly.