Webhooks Explained: Signed Payloads and Retries

Backend2026-11-27TryQuickToolBox

You've just integrated a third-party service that sends webhooks to your endpoint, but you're worried about security and reliability. How do you ensure that the payload is genuinely from the service and not an attacker? What happens if your server is down when the webhook is sent? In this article, we'll explore how to secure webhooks with signed payloads and implement robust retry mechanisms.

What Are Webhooks?

Webhooks are user-defined HTTP callbacks. They are triggered by specific events, such as a payment being processed or a new user signing up. When the event occurs, the source application sends an HTTP POST request to a URL you provide, carrying data about the event in the request body.

Unlike polling, where you repeatedly ask the service for updates, webhooks push data to you in near real-time. This makes them efficient and ideal for event-driven architectures.

Why Secure Webhooks with Signed Payloads?

Webhook endpoints are publicly accessible, which means anyone can send a POST request to them. Without verification, an attacker could forge a request to trigger unauthorized actions, such as marking an order as paid or granting credits.

Signing payloads with a shared secret allows the receiver to verify that the request originated from the trusted sender and that the payload hasn't been tampered with.

How Signed Payloads Work

The sender and receiver share a secret key. The sender computes a cryptographic signature of the payload using the secret and includes it in a header. The receiver recomputes the signature and compares it to the one received.

Typically, the signature is an HMAC (Hash-based Message Authentication Code) using SHA-256. For example, the sender might compute:

signature = HMAC-SHA256(secret, payload)

and send it in a header like X-Signature.

Example: Signing a Payload in Node.js

const crypto = require('crypto');

function signPayload(payload, secret) {
  return crypto.createHmac('sha256', secret).update(payload).digest('hex');
}

// On the sender side:
const payload = JSON.stringify({ event: 'payment.success', amount: 100 });
const signature = signPayload(payload, 'your-secret-key');
// Include signature in header X-Signature

Verifying the Signature on the Receiver

const crypto = require('crypto');

function verifySignature(payload, signature, secret) {
  const expected = crypto.createHmac('sha256', secret).update(payload).digest('hex');
  return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
}

// In your webhook endpoint:
app.post('/webhook', (req, res) => {
  const signature = req.headers['x-signature'];
  const payload = JSON.stringify(req.body); // or raw body if available
  if (!verifySignature(payload, signature, 'your-secret-key')) {
    return res.status(401).send('Invalid signature');
  }
  // Process the event
  res.status(200).send('OK');
});

Important: Always use a constant-time comparison function like crypto.timingSafeEqual to prevent timing attacks.

Implementing Retries for Reliable Delivery

Webhooks are sent over HTTP, which is unreliable. The receiver might be down, the network might fail, or the request might time out. To ensure events are not lost, senders should implement retries.

Retry Strategies

Example: Retry Logic with Exponential Backoff

async function sendWebhook(url, payload, secret, maxRetries = 5) {
  const signature = signPayload(payload, secret);
  for (let attempt = 0; attempt < maxRetries; attempt++) {
    try {
      const response = await fetch(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-Signature': signature },
        body: payload
      });
      if (response.ok) return;
    } catch (error) {
      // Log error
    }
    const delay = Math.pow(2, attempt) * 1000; // 1s, 2s, 4s...
    await new Promise(resolve => setTimeout(resolve, delay));
  }
  // Store in dead-letter queue
}

Best Practices for Webhook Security and Reliability

FAQ

What is a signed webhook payload?

A signed payload includes a cryptographic signature (usually HMAC) generated with a shared secret. It allows the receiver to verify the sender's authenticity and ensure the payload hasn't been altered.

How many times should I retry a failed webhook?

There's no universal number, but common practice is 3-5 retries with exponential backoff. After that, move the event to a dead-letter queue for manual handling.

Can I use webhooks without signatures?

It's not recommended. Without signatures, your endpoint is vulnerable to forged requests. Always verify signatures if the sender provides them.

When debugging webhook payloads or analyzing logs, a tool like the JSON Formatter can help you inspect and validate JSON data quickly.