Verifying signatures
Make sure a webhook request really came from ZigCRM.
Anyone can send a request to a public URL, so check each one. Every webhook request is signed with your webhook's signing secret.
How to check
- Read the raw request body (before parsing it), plus the
X-ZigCRM-TimestampandX-ZigCRM-Signatureheaders. - Work out the expected signature — HMAC-SHA256 of
timestamp + "." + raw_body, with your secret as the key, written as hex:
sha256=HEX( HMAC_SHA256( secret, timestamp + "." + raw_body ) )- Compare it with the
X-ZigCRM-Signatureheader using a constant-time comparison. Different? Reply401and ignore the request. - Reject requests whose timestamp is more than 5 minutes from your clock. This stops old requests being replayed.
Use the secret as plain text — don't hex-decode it.
Copy-paste code
import crypto from 'node:crypto';
import express from 'express';
const app = express();
const SECRET = process.env.ZIGCRM_WEBHOOK_SECRET;
const TOLERANCE_SECONDS = 300;
// express.raw keeps the exact bytes — express.json() would lose them.
app.post('/zigcrm-webhook', express.raw({ type: '/' }), (req, res) => {
const timestamp = req.get('X-ZigCRM-Timestamp') || '';
const signature = req.get('X-ZigCRM-Signature') || '';
const rawBody = req.body.toString('utf8');
const expected = 'sha256=' + crypto
.createHmac('sha256', SECRET)
.update(<span class="hljs-subst">${timestamp}</span>.<span class="hljs-subst">${rawBody}</span>)
.digest('hex');
const valid = expected.length === signature.length &&
crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(signature));
const fresh = Math.abs(Date.now() / 1000 - Number(timestamp)) <= TOLERANCE_SECONDS;
if (!valid || !fresh) return res.status(401).send('invalid signature');
const event = JSON.parse(rawBody); // JSON format
res.sendStatus(200); // respond first…
handleEvent(event); // …then do the work
});
<?php
$secret = getenv('ZIGCRM_WEBHOOK_SECRET');
$rawBody = file_get_contents('php://input');
$timestamp = $_SERVER['HTTP_X_ZIGCRM_TIMESTAMP'] ?? '';
$signature = $_SERVER['HTTP_X_ZIGCRM_SIGNATURE'] ?? '';
$expected = 'sha256=' . hash_hmac('sha256', $timestamp . '.' . $rawBody, $secret);
if (!hash_equals($expected, $signature) || abs(time() - (int) $timestamp) > 300) {
http_response_code(401);
exit('invalid signature');
}
$event = json_decode($rawBody, true); // JSON format
http_response_code(200);
// … handle $event['event'] / $event['data']
import hashlib, hmac, os, time
from flask import Flask, request, abort
app = Flask(name)
SECRET = os.environ["ZIGCRM_WEBHOOK_SECRET"].encode()
@app.post("/zigcrm-webhook")
def zigcrm_webhook():
raw_body = request.get_data() # exact bytes
timestamp = request.headers.get("X-ZigCRM-Timestamp", "")
signature = request.headers.get("X-ZigCRM-Signature", "")
expected = <span class="hljs-string">"sha256="</span> + hmac.new(
SECRET, timestamp.encode() + <span class="hljs-string">b"."</span> + raw_body, hashlib.sha256
).hexdigest()
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> hmac.compare_digest(expected, signature) <span class="hljs-keyword">or</span> <span class="hljs-built_in">abs</span>(time.time() - <span class="hljs-built_in">int</span>(timestamp <span class="hljs-keyword">or</span> <span class="hljs-number">0</span>)) > <span class="hljs-number">300</span>:
abort(<span class="hljs-number">401</span>)
event = request.get_json() <span class="hljs-comment"># JSON format</span>
<span class="hljs-comment"># … handle event["event"] / event["data"]</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">""</span>, <span class="hljs-number">200</span></code></pre></div></div></div>
WarningAlways sign the raw body. Parsing the JSON and turning it back into text changes spacing and key order, so the signature won't match.
Good to know
- Retries are signed again with a new timestamp, so their signature differs from the first try. Always use the headers of the request you actually received.
- Rotating the secret: click Rotate secret on the webhook in Settings → Webhooks. The new secret is used straight away, so update your endpoint right after — checks fail until you do.