Skip to content

Webhook Verification

Overview

When a user replies to a message, BotBell sends the reply to your reply_url via HTTP POST. Verify the signature to ensure the request is authentic.

Signature headerX-Webhook-Signature: sha256=...
Timestamp headerX-Webhook-Timestamp: 1234567890

How it works

  1. Extract X-Webhook-Timestamp and X-Webhook-Signature from headers
  2. Check timestamp is within 5 minutes (prevents replay attacks)
  3. Compute HMAC-SHA256 of {timestamp}.{body} with your webhook secret
  4. Compare with the signature (constant-time comparison)
signature = HMAC-SHA256(
  key: webhook_secret,
  message: "{timestamp}.{request_body}"
)

Python

from botbell import verify_webhook, WebhookVerificationError

try:
    verify_webhook(
        body=request.body,
        signature_header=request.headers["X-Webhook-Signature"],
        timestamp_header=request.headers["X-Webhook-Timestamp"],
        secret="your_webhook_secret",
    )
except WebhookVerificationError:
    return {"error": "Invalid signature"}, 401

# Signature valid — process the reply
data = json.loads(request.body)

JavaScript

import { verifyWebhook, WebhookVerificationError } from "@botbell/sdk";

try {
  verifyWebhook({
    body: req.body,
    signature: req.headers["x-webhook-signature"],
    timestamp: req.headers["x-webhook-timestamp"],
    secret: "your_webhook_secret",
  });
} catch (e) {
  if (e instanceof WebhookVerificationError) {
    return res.status(401).json({ error: e.message });
  }
  throw e;
}

// Signature valid — process the reply
const data = JSON.parse(req.body);