APITube Help Center

How to Verify Webhook Signatures (HMAC)

Check that a webhook request came from APITube

Erick Horn

Written by Erick Horn

June 26, 2026

Updated October 16, 2026

How to verify webhook signatures (HMAC)

Check each webhook’s X-Webhook-Signature before processing it. Use your signing secret to calculate HMAC-SHA256 over timestamp + "." + rawBody, then compare it with the header. HMAC is a signature made with a shared secret.

POST https://your-app.example.com/webhook
Content-Type: application/json
X-Webhook-Signature: sha256=abc123def456...
X-Webhook-Id: 7
X-Webhook-Delivery-Id: 42
X-Webhook-Timestamp: 1709000000

Reject a request with 401 if verification fails. You need the signature header, the X-Webhook-Timestamp value, the unmodified request body and your webhook’s secret.

What signature does APITube send?

The header is sha256=<hex>, where <hex> is the HMAC-SHA256 result for timestamp + "." + rawBody. Use the timestamp exactly as sent in X-Webhook-Timestamp, in Unix seconds, and the body’s original bytes.

X-Webhook-Id identifies the webhook. X-Webhook-Delivery-Id identifies one delivery and stays the same on retries; use it to skip deliveries you have already processed.

The JSON body contains the same article fields as a news search:

{
  "event": "articles.new",
  "webhook_id": 7,
  "delivered_at": "2026-06-26T12:00:00.000Z",
  "articles": [
    { "id": 123, "title": "...", "source": { "domain": "reuters.com" } }
  ]
}

Where do I get the signing secret?

Create the webhook in the dashboard. Its whsec_ secret is shown once, immediately after creation. Store it securely on your server, for example in an environment variable. Keep it out of browser code.

Use that secret for every delivery from this webhook. If you lose it, recreate the webhook to get a new one.

How to verify the signature in Node.js

Read the raw body before parsing JSON. Parsing and then calling JSON.stringify can change whitespace or key order, which changes the signature.

This example also rejects timestamps more than five minutes from the current time and compares signatures with timingSafeEqual:

import crypto from 'node:crypto';

const MAX_SKEW_SECONDS = 300; // reject deliveries older than 5 minutes (replay protection)

function verifyWebhook(secret, signature, timestamp, rawBody) {
    const age = Math.floor(Date.now() / 1000) - Number(timestamp);
    if (!Number.isFinite(age) || Math.abs(age) > MAX_SKEW_SECONDS) return false;

    const expected = `sha256=${crypto.createHmac('sha256', secret).update(timestamp + '.' + rawBody).digest('hex')}`;
    const received = String(signature || '');

    if (received.length !== expected.length) return false;
    return crypto.timingSafeEqual(Buffer.from(received), Buffer.from(expected));
}

app.post('/webhook', (req, res) => {
    const signature = req.headers['x-webhook-signature'];
    const timestamp = req.headers['x-webhook-timestamp'];
    const rawBody = req.rawBody; // use express.raw / the unparsed body, not JSON.stringify(req.body)

    if (!verifyWebhook(SECRET, signature, timestamp, rawBody)) {
        return res.status(401).send('Invalid signature');
    }

    const { articles } = JSON.parse(rawBody);
    res.status(200).send('OK');
});

How to verify the signature in Python

This example checks the timestamp, calculates the signature and compares it with hmac.compare_digest:

import hmac
import hashlib
import time

MAX_SKEW_SECONDS = 300  # replay protection

def verify_webhook(secret, signature, timestamp, raw_body):
    try:
        age = int(time.time()) - int(timestamp)
    except (TypeError, ValueError):
        return False
    if abs(age) > MAX_SKEW_SECONDS:
        return False

    expected = 'sha256=' + hmac.new(
        secret.encode(),
        (timestamp + '.' + raw_body).encode(),
        hashlib.sha256
    ).hexdigest()

    return hmac.compare_digest(signature or '', expected)

Common Questions

Why doesn’t my computed signature match?

Check that you signed the original body bytes. Do not sign JSON that your app parsed and rebuilt. In Express, for example, capture the body with express.raw({ type: 'application/json' }).

Use the exact timestamp header, followed by a literal . and the raw body. Signing the body alone will not match. To compare the payload, open the webhook’s Delivery log in the dashboard:

The webhook Delivery log in the dashboard exposing the raw request payload of a delivery for signature debugging

Should I reject old webhooks?

Yes. Check the signed timestamp against the current time. A five-minute window (300 seconds), as in these examples, helps prevent someone from replaying an old request.

Also track X-Webhook-Delivery-Id so a retry does not repeat work your app has already completed.

Do I need to compare signatures in constant time?

Yes. Use crypto.timingSafeEqual in Node.js or hmac.compare_digest in Python. These functions avoid revealing how much of a signature matched through comparison timing.

The Node.js function requires equal-length inputs, so the example checks their lengths first. Verify the signature before acting on any article in the request.


Related Articles