Developers
Verify webhook signatures
Check that a webhook request really came from Crate by recomputing its signature with your signing secret.
Reviewed
On this page
Anyone who learns your webhook address can send it fake events. Every request Crate sends is signed with your endpoint's secret, so your server can tell a real request from a forged one.
How the signature works
Crate joins the X-Crate-Timestamp value, a full stop and the exact request body into one string. It runs HMAC with SHA-256 over that string, using your signing secret as the key. It sends the result as lowercase hex in X-Crate-Signature.
Because the timestamp is part of the signed string, an old request can't be replayed with a new time.
Step 1: Copy the signing secret
The secret is shown once, when you create the endpoint.
- Open Settings, then Webhooks under Developer.
- Click Create webhook, enter the URL, choose the Webhook events, then click Create webhook.
- In the Signing secret box, click Copy signing secret.
- Store the secret on your server.
alt: The Signing secret box on the Webhooks page with the secret blurred and the Copy signing secret button
caption: Copy the secret now. You won't see it again.Step 2: Read the raw body and two headers
In your handler, read the body exactly as it arrived, before you parse the JSON. Then read the X-Crate-Timestamp and X-Crate-Signature headers.
Step 3: Compute the signature
Build the string <timestamp>.<raw body>. Run HMAC-SHA256 over it with your secret as the key, and write the result as lowercase hex.
Step 4: Compare and check the age
Compare your result with X-Crate-Signature using a constant-time comparison. Reject the request if they differ. Also reject it if the timestamp is more than five minutes from your server's clock.
Step 5: Answer quickly
Answer 200 once the check passes, then handle the event. If the check fails, answer 400 and ignore the request.
Example in Node.js
import crypto from "node:crypto";
const secret = process.env.CRATE_WEBHOOK_SECRET;
export function isFromCrate(rawBody, headers) {
const timestamp = headers["x-crate-timestamp"];
const received = headers["x-crate-signature"] || "";
if (!timestamp || Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false;
const expected = crypto
.createHmac("sha256", secret)
.update(`${timestamp}.${rawBody}`)
.digest("hex");
const a = Buffer.from(expected);
const b = Buffer.from(received);
return a.length === b.length && crypto.timingSafeEqual(a, b);
}Example in Ruby
require "openssl"
def from_crate?(raw_body, timestamp, received, secret: ENV.fetch("CRATE_WEBHOOK_SECRET"))
return false if timestamp.to_s.empty? || (Time.now.to_i - timestamp.to_i).abs > 300
expected = OpenSSL::HMAC.hexdigest("SHA256", secret, "#{timestamp}.#{raw_body}")
Rack::Utils.secure_compare(expected, received.to_s)
endRotate the secret
Rotate the secret if it leaks, or when someone with access to it leaves your team.
- On the Webhooks page, open the endpoint's menu.
- Choose Rotate signing secret and confirm.
- Copy the new secret from the Signing secret box.
- Update your server with the new secret.
The old secret stops verifying straight away. Crate emails the store owner when a secret is rotated.
Troubleshooting
- The signature never matches. Your code hashes a changed body. Hash the raw bytes, and check the secret has no extra spaces.
- Only some requests fail the age check. Your server's clock is off. Sync it with a time service.
- Every request fails after you rotated the secret. Your server still has the old secret. Replace it.
- I lost the secret. Rotate it to get a new one.
Troubleshooting
Next steps
Was this helpful?
Still need a hand?
Tell us the task you were trying to complete and the message you saw. Include your store name or order reference when relevant. Keep passwords, API secrets and card details private.
Contact Crate