Developers
Webhooks · 1 min read

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

  1. Read the raw request body (before parsing it), plus the X-ZigCRM-Timestamp and X-ZigCRM-Signature headers.
  2. 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 ) )
  1. Compare it with the X-ZigCRM-Signature header using a constant-time comparison. Different? Reply 401 and ignore the request.
  2. 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">&quot;sha256=&quot;</span> + hmac.new(
    SECRET, timestamp.encode() + <span class="hljs-string">b&quot;.&quot;</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>)) &gt; <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[&quot;event&quot;] / event[&quot;data&quot;]</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-number">200</span></code></pre></div></div></div>
Warning

Always 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.