# Verify webhook signatures
Check that a webhook request really came from Crate by recomputing its signature with your signing secret.
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.
1. Open **Settings**, then **Webhooks** under **Developer**.
2. Click **Create webhook**, enter the **URL**, choose the **Webhook events**, then click **Create webhook**.
3. In the **Signing secret** box, click **Copy signing secret**.
4. Store the secret on your server.
```screenshot developers/verify-webhook-signatures/01-signing-secret
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.
> [!WARNING]
> Crate retries `5xx` answers and timeouts, but not `4xx`. If a real event fails your check, it isn't sent again.
## Example in Node.js
```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
```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)
end
```
> [!WARNING]
> Don't parse the body and turn it back into JSON before you hash it. Spacing and key order change, and the signature won't match.
## Rotate the secret
Rotate the secret if it leaks, or when someone with access to it leaves your team.
1. On the **Webhooks** page, open the endpoint's menu.
2. Choose **Rotate signing secret** and confirm.
3. Copy the new secret from the **Signing secret** box.
4. 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.
