Verifying signatures
Check that a webhook came from CashPayment and was not changed.
Every webhook is signed with your endpoint's signing secret, so you can reject forged or modified requests. Each request carries three headers:
| Header | Content |
|---|---|
svix-id | Message ID. The same on every retry of a message. |
svix-timestamp | Unix time in seconds when the message was signed. |
svix-signature | One or more signatures, separated by spaces. |
Get the signing secret (whsec_…) from the endpoint's page in the merchant console. Keep it secret, like an API key.
Verify the raw request body exactly as received. If your framework parses JSON and you serialize it again, the bytes change and verification fails.
With the Svix libraries
The official Svix libraries check the signature and reject timestamps more than 5 minutes off.
npm install sviximport express from "express";
import { Webhook } from "svix";
const webhook = new Webhook(process.env.CASHPAYMENT_WEBHOOK_SECRET);
const app = express();
app.post("/webhooks/cashpayment", express.raw({ type: "application/json" }), (req, res) => {
let event;
try {
event = webhook.verify(req.body, {
"svix-id": req.header("svix-id"),
"svix-timestamp": req.header("svix-timestamp"),
"svix-signature": req.header("svix-signature"),
});
} catch {
return res.status(400).end();
}
if (event.type === "order.paid") {
markPaid(event.data.reference);
}
res.status(204).end();
});Verifying manually
If you can't use a library, follow the Standard Webhooks algorithm:
Build the signed content: svix-id, svix-timestamp and the raw body, joined with dots.
msg_2pLqV7sM1hYk3fNb0aXcRw9dT4e.1763380982.{"id":"0b6a3c52-…","type":"order.paid",…}Take the signing secret without its whsec_ prefix and Base64-decode it. Use the bytes as the key.
Compute HMAC-SHA256 of the signed content with that key and Base64-encode the result.
svix-signature holds space-separated entries such as v1,g0hM9S…. Remove the v1, prefix from each and compare it to your signature in constant time. One match is enough.
Reject the request if svix-timestamp is more than 5 minutes away from your current time, to prevent replay attacks.
import { createHmac, timingSafeEqual } from "node:crypto";
export function verify(secret, headers, rawBody) {
const id = headers["svix-id"];
const timestamp = headers["svix-timestamp"];
const signatures = headers["svix-signature"];
if (!id || !timestamp || !signatures) return false;
if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false;
const key = Buffer.from(secret.replace(/^whsec_/, ""), "base64");
const expected = createHmac("sha256", key).update(`${id}.${timestamp}.${rawBody}`).digest();
return signatures.split(" ").some((entry) => {
const [version, signature] = entry.split(",");
if (version !== "v1" || !signature) return false;
const received = Buffer.from(signature, "base64");
return received.length === expected.length && timingSafeEqual(received, expected);
});
}Rotating the secret
Rotate the secret from the endpoint's menu in the merchant console. For the next 24 hours, each message is signed with both the old and the new secret, so svix-signature carries two entries. Switch your server to the new secret within that window; after it, only the new secret works.