Verify signatures
Check that each webhook comes from Bookline and has not been changed, with Python and Node examples.
Every request Bookline sends is signed with your endpoint’s secret, following the Standard Webhooks specification. Verify the signature before you trust or process the body, and reject the request if it does not match.
Headers
Section titled “Headers”| Header | Value |
|---|---|
webhook-id |
The event id, the same as id in the body. It does not change between retries |
webhook-timestamp |
When this attempt was signed, in Unix seconds. New on each retry |
webhook-signature |
One or more signatures separated by spaces, each as v1,<base64> |
content-type |
application/json |
user-agent |
Bookline-Webhooks/1.0 |
bookline-delivery-id |
Id of this delivery. Give it to us if you ask about a specific delivery |
bookline-attempt |
Attempt number, starting at 1 |
How the signature is built
Section titled “How the signature is built”- The secret looks like
whsec_<base64>. The key is the base64 part, decoded to bytes. - The signed content is
<webhook-id>.<webhook-timestamp>.<body>: the id, a dot, the timestamp, a dot and the raw body exactly as received. - The signature is the HMAC-SHA256 of that content with the key, base64-encoded, and prefixed with
v1,.
The header can carry more than one signature, separated by spaces: during a secret rotation each request is signed with the new and the old secret. Accept the request if any v1 signature matches.
With a library
Section titled “With a library”The Standard Webhooks libraries take the secret with its whsec_ prefix, check the signature in constant time and reject timestamps more than 5 minutes away from your clock.
Python (pip install standardwebhooks), with Flask:
import os
from flask import Flask, requestfrom standardwebhooks.webhooks import Webhook, WebhookVerificationError
app = Flask(__name__)webhook = Webhook(os.environ["BOOKLINE_WEBHOOK_SECRET"])
@app.post("/bookline")def bookline_webhook(): try: event = webhook.verify(request.get_data(), dict(request.headers)) except WebhookVerificationError: return "", 400 enqueue(event) # process it later, outside the request return "", 204Node (npm install standardwebhooks), with Express:
import express from "express";import { Webhook } from "standardwebhooks";
const app = express();const webhook = new Webhook(process.env.BOOKLINE_WEBHOOK_SECRET);
app.post("/bookline", express.raw({ type: "application/json" }), (req, res) => { let event; try { event = webhook.verify(req.body, req.headers); } catch { return res.sendStatus(400); } enqueue(event); // process it later, outside the request res.sendStatus(204);});The svix libraries are compatible: new Webhook(secret).verify(body, headers) from the svix package verifies Bookline webhooks too.
Without a library
Section titled “Without a library”If you prefer to verify by hand, compare in constant time and check the timestamp:
import base64import hashlibimport hmacimport time
TOLERANCE_SECONDS = 5 * 60
def verify(secret: str, headers: dict[str, str], body: bytes) -> bool: message_id = headers["webhook-id"] timestamp = headers["webhook-timestamp"] if abs(time.time() - int(timestamp)) > TOLERANCE_SECONDS: return False key = base64.b64decode(secret.removeprefix("whsec_")) signed = f"{message_id}.{timestamp}.".encode() + body expected = base64.b64encode(hmac.new(key, signed, hashlib.sha256).digest()).decode() for signature in headers["webhook-signature"].split(" "): version, _, value = signature.partition(",") if version == "v1" and hmac.compare_digest(value, expected): return True return FalseHeader names are case-insensitive: read them in lowercase, or with your framework’s case-insensitive lookup.
Timestamp tolerance
Section titled “Timestamp tolerance”webhook-timestamp protects you against someone replaying an old request. Reject requests whose timestamp is more than 5 minutes away from your server’s clock, and keep the clock in sync (NTP). Each retry is signed again with a fresh timestamp, so a retry is never rejected for being late.
Related
Section titled “Related”- Security: keeping the secret, rotating it.
- Responses and retries: what to answer when the signature does not match.
