How to verify RoofStruct webhook signatures (Node.js and Python)
Every RoofStruct webhook is signed with the Standard Webhooks scheme. The three headers, the four-step check, working Node.js and Python code, and the mistakes that break it.
Every webhook RoofStruct sends carries three headers: webhook-id, webhook-timestamp and webhook-signature. The signature is an HMAC-SHA256 of the message ID, the timestamp and the raw body, keyed with the signing secret you copied when you added the webhook. Recompute it on your side, compare, and reject anything that doesn't match. That is the whole check; the rest of this page is code and the places it goes wrong.
The scheme follows the open Standard Webhooks spec, so if your stack already has a Standard Webhooks library, it works with RoofStruct as-is.
What arrives with each message
RoofStruct posts JSON to your endpoint with these headers:
| Header | What it holds |
|---|---|
webhook-id |
The event ID. The same on every retry of the same event |
webhook-timestamp |
The Unix timestamp of this attempt |
webhook-signature |
v1, followed by a base64 HMAC-SHA256 |
User-Agent |
RoofStruct-Webhooks/1 |
The body is an envelope with id, type, created_at and data. The type is one of report.delivered, report.status_changed, measurement.sent, or webhook.test when someone clicks Send Test.
Where the secret comes from
In RoofStruct, go to Settings › Integrations, then Webhooks › Add webhook. After you save, a dialog titled Copy your signing secret shows a value starting with whsec_. It is shown once. Store it next to your other secrets; if you lose it, delete the webhook and add it again to get a new one.
Only account owners and admins see the Integrations tab.
The check in four steps
- Read the three headers. If any is missing, reject.
- Reject a timestamp more than five minutes from your server's clock. That stops someone replaying an old message.
- Build the signed content:
webhook-id+.+webhook-timestamp+.+ the raw request body, exactly as received. - Strip
whsec_from the secret, base64-decode the rest, and use those bytes as the HMAC-SHA256 key. Base64-encode the result and compare it, in constant time, with eachv1,entry inwebhook-signature(the header can hold several, separated by spaces).
Node.js
Express, with the body kept raw for this route:
import crypto from "node:crypto";
import express from "express";
const SECRET = process.env.ROOFSTRUCT_WEBHOOK_SECRET; // whsec_...
const KEY = Buffer.from(SECRET.replace(/^whsec_/, ""), "base64");
const TOLERANCE_S = 5 * 60;
function verify(rawBody, headers) {
const id = headers["webhook-id"];
const ts = headers["webhook-timestamp"];
const sigHeader = headers["webhook-signature"];
if (!id || !ts || !sigHeader) return false;
if (Math.abs(Date.now() / 1000 - Number(ts)) > TOLERANCE_S) return false;
const expected = crypto
.createHmac("sha256", KEY)
.update(`${id}.${ts}.${rawBody}`)
.digest("base64");
return sigHeader.split(" ").some((part) => {
const [version, sig] = part.split(",");
return (
version === "v1" &&
sig?.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(expected))
);
});
}
const app = express();
app.post(
"/roofstruct-webhook",
express.raw({ type: "application/json" }),
(req, res) => {
if (!verify(req.body.toString("utf8"), req.headers)) {
return res.status(401).end();
}
const event = JSON.parse(req.body);
// Queue the work, then answer straight away.
res.status(204).end();
},
);
Python
Flask, reading the body as bytes:
import base64, hashlib, hmac, os, time
from flask import Flask, request, abort
SECRET = os.environ["ROOFSTRUCT_WEBHOOK_SECRET"] # whsec_...
KEY = base64.b64decode(SECRET.removeprefix("whsec_"))
TOLERANCE_S = 5 * 60
app = Flask(__name__)
def verify(body: bytes, headers) -> bool:
msg_id = headers.get("webhook-id")
ts = headers.get("webhook-timestamp")
sig_header = headers.get("webhook-signature", "")
if not msg_id or not ts or abs(time.time() - int(ts)) > TOLERANCE_S:
return False
signed = f"{msg_id}.{ts}.".encode() + body
expected = base64.b64encode(
hmac.new(KEY, signed, hashlib.sha256).digest()
).decode()
return any(
part.startswith("v1,") and hmac.compare_digest(part[3:], expected)
for part in sig_header.split(" ")
)
@app.post("/roofstruct-webhook")
def roofstruct_webhook():
if not verify(request.get_data(), request.headers):
abort(401)
event = request.get_json()
# Queue the work, then answer straight away.
return "", 204
Where the check fails
- The body was parsed first. Frameworks that parse JSON and hand you an object have already changed the bytes (key order, spacing). Sign the raw body. In Express that means
express.rawon this route, notexpress.json. - The secret was used as text. The key is the base64-decoded bytes after
whsec_, not the string itself. - The server clock drifts. A five-minute window is generous, but a container without time sync can fall outside it. Run NTP.
- A plain
===comparison. UsetimingSafeEqualorcompare_digest, so the response time doesn't leak how much of the signature matched. - The endpoint redirects. RoofStruct doesn't follow redirects, so an
httptohttpshop or a trailing-slash redirect counts as a failed delivery. Give it the final URL.
After the check: answer fast
The full retry and duplicate story is in a webhook receiver that never loses a delivery; the short version:
RoofStruct waits 10 seconds for a reply. Any 2xx counts as delivered; anything else is retried after 1 minute, 5 minutes, 30 minutes, 2 hours and 12 hours, and then shows as Failed under Recent Deliveries. If 10 deliveries in a row fail every retry, the webhook switches itself off, and you turn it back on once the endpoint is fixed.
Two habits keep that from happening:
- Verify, queue, return 204. Do the CRM call, the PDF download or the database write after you've answered.
- Deduplicate on
webhook-id. Delivery is at least once, so the same event can arrive twice, and events can arrive out of order. Store the IDs you've handled and skip repeats.
RoofStruct treats any reply other than a 2xx as a failed delivery, 410 Gone included, and retries it on the usual schedule. If 10 deliveries in a row fail every retry, the webhook switches itself off ("Switched off after repeated failures. Fix the endpoint, then turn it back on."). To stop deliveries on purpose, use Turn Off or Delete on the webhook in Settings.
Test it before a real report
Click Send Test on the webhook. A webhook.test event arrives within about a minute, signed the same way as the real ones. Recent Deliveries shows the last 50 messages with their result, and Retry resends one after you fix the endpoint.
Developer docs: Verify signatures and Retries in the RoofStruct API reference.