# Verify a webhook signature (X-Camairetech-Signature) and reject forged calls

_Yes: every webhook carries X-Camairetech-Signature, an HMAC-SHA256 of the raw body keyed by your secret; recompute it, compare in constant time, refuse an old timestamp and de-duplicate on wa_message_id._

Keywords: webhook signature, X-Camairetech-Signature, HMAC, sha256, verify webhook, secure webhook, forged webhook, replay protection, timestamp, webhook secret, 401, authenticate the call, raw body

## Scenario

Your server exposes a public address to receive your customers' messages. Anyone can guess it and send it a fake "message.inbound" to trigger a refund or an automatic reply. **Yes, you can prove a call really comes from the API**: every POST is signed with a secret only you know.

## What you receive

```text
POST https://your-server.example/webhooks/whatsapp
Content-Type: application/json
User-Agent: CamaireTech-WhatsApp-API/1.0
X-Camairetech-Signature: sha256=3f1c…(64 hexadecimal characters)

{"event":"message.inbound","data":{…},"timestamp":"2026-10-30T09:14:07.123456+00:00"}
```

The signature is the HMAC-SHA256 of the **exact bytes of the body**, keyed by your secret, prefixed with `sha256=`. The `timestamp` is **inside the body**, so the signature covers it: an attacker cannot refresh it without breaking the signature.

## Where the secret is

It is created when you register the address in the console (Connection page, Webhook). The address must be HTTPS. The secret is shown **only once**: store it straight away in an environment variable (`GESKAP_WEBHOOK_SECRET`). Lost it? Register the address again: a new secret is generated and the old one stops working.

## Code

The four checks in order: signature, freshness, duplicate, then processing.

```js
// Node.js
// Express — signature, freshness, duplicate, then your code.
import express from "express";
import crypto from "node:crypto";

const app = express();
const SECRET = process.env.GESKAP_WEBHOOK_SECRET;
const MAX_AGE_MS = 5 * 60 * 1000;   // your choice: the API imposes no window
const seen = new Set();             // production: a table with a UNIQUE index on wa_message_id

// express.raw keeps the exact bytes, which is what the signature covers.
app.post("/webhooks/whatsapp", express.raw({ type: "application/json" }), (req, res) => {
  const expected = "sha256=" + crypto.createHmac("sha256", SECRET).update(req.body).digest("hex");
  const got = req.get("X-Camairetech-Signature") || "";
  if (got.length !== expected.length ||
      !crypto.timingSafeEqual(Buffer.from(got), Buffer.from(expected))) {
    return res.sendStatus(401);
  }
  const { event, data, timestamp } = JSON.parse(req.body.toString("utf8"));
  if (Math.abs(Date.now() - new Date(timestamp).getTime()) > MAX_AGE_MS) {
    return res.sendStatus(401);     // replay: the timestamp is inside the signed body
  }
  res.sendStatus(200);              // answer fast: one attempt, 8 s, no retry
  const id = data.wa_message_id;
  if (id && seen.has(id)) return;   // already handled
  if (id) seen.add(id);
  if (event === "message.inbound") onInbound(data);   // your code
});

app.listen(3000);
```

```php
// PHP
<?php
// Signature, freshness, duplicate, then your code.
$raw = file_get_contents("php://input");   // raw bytes, before json_decode
$expected = "sha256=" . hash_hmac("sha256", $raw, getenv("GESKAP_WEBHOOK_SECRET"));
$got = $_SERVER["HTTP_X_CAMAIRETECH_SIGNATURE"] ?? "";
if (!hash_equals($expected, $got)) {
    http_response_code(401);
    exit;
}
$payload = json_decode($raw, true);
$age = abs(time() - strtotime($payload["timestamp"]));
if ($age > 300) {                          // your choice: the API imposes no window
    http_response_code(401);               // replay: the timestamp is inside the signed body
    exit;
}
http_response_code(200);                   // one attempt, 8 s, no retry
$id = $payload["data"]["wa_message_id"] ?? null;
// Production: INSERT into a table with a UNIQUE index on wa_message_id, skip on conflict.
if ($id && already_handled($id)) {
    exit;
}
if ($payload["event"] === "message.inbound") {
    on_inbound($payload["data"]);          // your code
}
```

```python
# Python
# Flask — signature, freshness, duplicate, then your code.
import hashlib, hmac, json, os
from datetime import datetime, timezone
from flask import Flask, abort, request

app = Flask(__name__)
SECRET = os.environ["GESKAP_WEBHOOK_SECRET"].encode()
MAX_AGE_S = 300      # your choice: the API imposes no window
seen = set()         # production: a table with a UNIQUE index on wa_message_id

@app.post("/webhooks/whatsapp")
def whatsapp_webhook():
    raw = request.get_data()   # raw bytes, before any JSON parsing
    expected = "sha256=" + hmac.new(SECRET, raw, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, request.headers.get("X-Camairetech-Signature", "")):
        abort(401)
    payload = json.loads(raw)
    sent_at = datetime.fromisoformat(payload["timestamp"])
    if abs((datetime.now(timezone.utc) - sent_at).total_seconds()) > MAX_AGE_S:
        abort(401)             # replay: the timestamp is inside the signed body
    wamid = payload["data"].get("wa_message_id")
    if wamid and wamid in seen:
        return "", 200         # already handled
    if wamid:
        seen.add(wamid)
    if payload["event"] == "message.inbound":
        on_inbound(payload["data"])   # your code (answer fast, queue the slow work)
    return "", 200
```

```bash
# curl
# Fake signed call to YOUR server, to test your verifier.
SECRET="$GESKAP_WEBHOOK_SECRET"
TS=$(date -u +"%Y-%m-%dT%H:%M:%S+00:00")
BODY='{"event":"message.inbound","data":{"from":"+237690000001","number":"100000000000001","wa_message_id":"wamid.TEST1","type":"text","text":"Bonjour","button_id":null},"timestamp":"'"$TS"'"}'
SIG="sha256=$(printf '%s' "$BODY" | openssl dgst -sha256 -hmac "$SECRET" | sed 's/^.* //')"
curl -i -X POST "https://votre-serveur.example/webhooks/whatsapp" \
  -H "Content-Type: application/json" \
  -H "X-Camairetech-Signature: $SIG" \
  --data-binary "$BODY"
```

## Test without waiting for a real message

The API sends no test event. Compute the signature yourself and send a fake call to your own server: a correctly signed call must go through, a signature changed by one character must be refused with 401.

## What to refuse

| Case | Recommended answer |
|---|---|
| Header missing or signature different | 401, process nothing |
| Timestamp older than a few minutes | 401 (probable replay) |
| `wa_message_id` already handled | 200, do not process again |
| Unknown event | 200 and ignore it: other events may be added |

> **Pitfall:** verify the signature on the **raw** bytes received, before any `JSON.parse`, `json_decode` or `request.json()`. Re-serializing the JSON changes spaces, key order or accents, and the signature no longer matches. With Express, use `express.raw`, not `express.json`.

> **Pitfall:** there is **one attempt** per event, with an 8-second timeout, and no retry. Answer 2xx fast, do the work afterwards, and catch up on what is missing with `GET /v1/messages/inbound` (see [Archive messages and media](https://console.geskap.com/docs/archiver-messages-medias?lang=en)). A 401 returned by mistake on a real event loses it.

> **Pitfall:** since there is no retry, a legitimate event always arrives with a brand-new timestamp. Refusing anything older than a few minutes therefore costs nothing. The exact duration is your choice: the API imposes no window.

## See what was attempted

`GET /v1/logs?category=webhook` (or the console Logs page) lists the `message.inbound` and `message.echo` attempts with the HTTP code your server answered. `message.status` events are not traced in that log: when in doubt, compare with `GET /v1/messages/{id}`.

To plug this handler into a no-code tool or an LLM, see [Connect WhatsApp to n8n, Zapier, a CRM or an LLM](https://console.geskap.com/docs/webhook-n8n-zapier-crm-llm?lang=en).

_Proof — verified in the API source: client_webhooks.py, console_api.py, meta_webhooks.py._

---

# Vérifier la signature d'un webhook (X-Camairetech-Signature) et refuser les faux appels

_Oui : chaque webhook porte X-Camairetech-Signature, un HMAC-SHA256 du corps brut avec votre secret ; recalculez-le, comparez en temps constant, refusez un timestamp trop vieux et dédoublonnez sur wa_message_id._

Mots-clés: signature webhook, X-Camairetech-Signature, HMAC, sha256, vérifier webhook, sécuriser webhook, faux webhook, anti-rejeu, replay, timestamp, secret du webhook, 401, authentifier l'appel, corps brut, raw body

## Scénario

Votre serveur expose une adresse publique pour recevoir les messages de vos clients. N'importe qui peut la deviner et lui envoyer un faux « message.inbound » pour déclencher un remboursement ou une réponse automatique. **Oui, vous pouvez prouver que l'appel vient bien de l'API** : chaque POST est signé avec un secret que vous seul connaissez.

## Ce que vous recevez

```text
POST https://votre-serveur.example/webhooks/whatsapp
Content-Type: application/json
User-Agent: CamaireTech-WhatsApp-API/1.0
X-Camairetech-Signature: sha256=3f1c…(64 caractères hexadécimaux)

{"event":"message.inbound","data":{…},"timestamp":"2026-10-30T09:14:07.123456+00:00"}
```

La signature est le HMAC-SHA256 des **octets exacts du corps**, avec votre secret comme clé, préfixé par `sha256=`. Le `timestamp` est **dans le corps**, donc il est couvert par la signature : un attaquant ne peut pas le rafraîchir sans casser la signature.

## Où est le secret

Il est créé quand vous enregistrez l'adresse dans la console (page Connexion, Webhook). L'adresse doit être en HTTPS. Le secret n'est affiché **qu'une seule fois** : stockez-le tout de suite dans une variable d'environnement (`GESKAP_WEBHOOK_SECRET`). Perdu ? Enregistrez de nouveau l'adresse : un nouveau secret est généré et l'ancien cesse de fonctionner.

## Code

Les quatre contrôles dans l'ordre : signature, fraîcheur, doublon, puis traitement.

```js
// Node.js
// Express — signature, freshness, duplicate, then your code.
import express from "express";
import crypto from "node:crypto";

const app = express();
const SECRET = process.env.GESKAP_WEBHOOK_SECRET;
const MAX_AGE_MS = 5 * 60 * 1000;   // your choice: the API imposes no window
const seen = new Set();             // production: a table with a UNIQUE index on wa_message_id

// express.raw keeps the exact bytes, which is what the signature covers.
app.post("/webhooks/whatsapp", express.raw({ type: "application/json" }), (req, res) => {
  const expected = "sha256=" + crypto.createHmac("sha256", SECRET).update(req.body).digest("hex");
  const got = req.get("X-Camairetech-Signature") || "";
  if (got.length !== expected.length ||
      !crypto.timingSafeEqual(Buffer.from(got), Buffer.from(expected))) {
    return res.sendStatus(401);
  }
  const { event, data, timestamp } = JSON.parse(req.body.toString("utf8"));
  if (Math.abs(Date.now() - new Date(timestamp).getTime()) > MAX_AGE_MS) {
    return res.sendStatus(401);     // replay: the timestamp is inside the signed body
  }
  res.sendStatus(200);              // answer fast: one attempt, 8 s, no retry
  const id = data.wa_message_id;
  if (id && seen.has(id)) return;   // already handled
  if (id) seen.add(id);
  if (event === "message.inbound") onInbound(data);   // your code
});

app.listen(3000);
```

```php
// PHP
<?php
// Signature, freshness, duplicate, then your code.
$raw = file_get_contents("php://input");   // raw bytes, before json_decode
$expected = "sha256=" . hash_hmac("sha256", $raw, getenv("GESKAP_WEBHOOK_SECRET"));
$got = $_SERVER["HTTP_X_CAMAIRETECH_SIGNATURE"] ?? "";
if (!hash_equals($expected, $got)) {
    http_response_code(401);
    exit;
}
$payload = json_decode($raw, true);
$age = abs(time() - strtotime($payload["timestamp"]));
if ($age > 300) {                          // your choice: the API imposes no window
    http_response_code(401);               // replay: the timestamp is inside the signed body
    exit;
}
http_response_code(200);                   // one attempt, 8 s, no retry
$id = $payload["data"]["wa_message_id"] ?? null;
// Production: INSERT into a table with a UNIQUE index on wa_message_id, skip on conflict.
if ($id && already_handled($id)) {
    exit;
}
if ($payload["event"] === "message.inbound") {
    on_inbound($payload["data"]);          // your code
}
```

```python
# Python
# Flask — signature, freshness, duplicate, then your code.
import hashlib, hmac, json, os
from datetime import datetime, timezone
from flask import Flask, abort, request

app = Flask(__name__)
SECRET = os.environ["GESKAP_WEBHOOK_SECRET"].encode()
MAX_AGE_S = 300      # your choice: the API imposes no window
seen = set()         # production: a table with a UNIQUE index on wa_message_id

@app.post("/webhooks/whatsapp")
def whatsapp_webhook():
    raw = request.get_data()   # raw bytes, before any JSON parsing
    expected = "sha256=" + hmac.new(SECRET, raw, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, request.headers.get("X-Camairetech-Signature", "")):
        abort(401)
    payload = json.loads(raw)
    sent_at = datetime.fromisoformat(payload["timestamp"])
    if abs((datetime.now(timezone.utc) - sent_at).total_seconds()) > MAX_AGE_S:
        abort(401)             # replay: the timestamp is inside the signed body
    wamid = payload["data"].get("wa_message_id")
    if wamid and wamid in seen:
        return "", 200         # already handled
    if wamid:
        seen.add(wamid)
    if payload["event"] == "message.inbound":
        on_inbound(payload["data"])   # your code (answer fast, queue the slow work)
    return "", 200
```

```bash
# curl
# Fake signed call to YOUR server, to test your verifier.
SECRET="$GESKAP_WEBHOOK_SECRET"
TS=$(date -u +"%Y-%m-%dT%H:%M:%S+00:00")
BODY='{"event":"message.inbound","data":{"from":"+237690000001","number":"100000000000001","wa_message_id":"wamid.TEST1","type":"text","text":"Bonjour","button_id":null},"timestamp":"'"$TS"'"}'
SIG="sha256=$(printf '%s' "$BODY" | openssl dgst -sha256 -hmac "$SECRET" | sed 's/^.* //')"
curl -i -X POST "https://votre-serveur.example/webhooks/whatsapp" \
  -H "Content-Type: application/json" \
  -H "X-Camairetech-Signature: $SIG" \
  --data-binary "$BODY"
```

## Tester sans attendre un vrai message

Il n'existe pas d'événement de test envoyé par l'API. Calculez la signature vous-même et envoyez un faux appel à votre propre serveur : un appel correctement signé doit passer, une signature modifiée d'un caractère doit être refusée en 401.

## Ce qu'il faut refuser

| Cas | Réponse conseillée |
|---|---|
| En-tête absent ou signature différente | 401, sans rien traiter |
| Timestamp plus vieux que quelques minutes | 401 (rejeu probable) |
| `wa_message_id` déjà traité | 200, sans retraiter |
| Événement inconnu | 200 et ignorez-le : d'autres événements peuvent s'ajouter |

> **Piège :** vérifiez la signature sur les octets **bruts** reçus, avant tout `JSON.parse`, `json_decode` ou `request.json()`. Ré-sérialiser le JSON change les espaces, l'ordre ou les accents, et la signature ne correspond plus. Avec Express, utilisez `express.raw`, pas `express.json`.

> **Piège :** il n'y a qu'**une seule tentative** par événement, avec un délai de 8 secondes, et aucune relance. Répondez 2xx vite, faites le travail ensuite, et rattrapez ce qui manque avec `GET /v1/messages/inbound` (voir [Archiver messages et médias](https://console.geskap.com/docs/archiver-messages-medias)). Un 401 renvoyé par erreur à un vrai événement le perd.

> **Piège :** comme il n'y a pas de relance, un événement légitime arrive toujours avec un timestamp tout frais. Refuser au-delà de quelques minutes ne coûte donc rien. La durée exacte est votre choix : l'API n'impose aucune fenêtre.

## Voir ce qui a été tenté

`GET /v1/logs?category=webhook` (ou la page Logs de la console) liste les tentatives de `message.inbound` et `message.echo` avec le code HTTP que votre serveur a répondu. Les `message.status` ne sont pas tracés dans ce journal : en cas de doute, comparez avec `GET /v1/messages/{id}`.

Pour brancher ce gestionnaire sur un outil sans code ou un LLM, voir [Brancher WhatsApp sur n8n, Zapier, un CRM ou un LLM](https://console.geskap.com/docs/webhook-n8n-zapier-crm-llm).

_Preuve — vérifié dans le code de l'API : client_webhooks.py, console_api.py, meta_webhooks.py._

Support: WhatsApp +237 6 56 09 88 31 · All recipes: https://wa-api.geskap.com/llms.txt
