2026-12-01

Why Your Webhook Signature Verification Fails

When a verifier rejects a delivery you know is real, the cause is almost always the body. The signature covers the exact bytes Klap sent, and something between the socket and your verifier changed them. Parsed JSON, a re-serialized object or an extra newline all produce a different HMAC.

Below, the causes in the order we'd check them. Each has a symptom and a fix. If you'd rather test the verifier than read, the free webhook signature tester builds a signed sample with a throwaway secret and a curl command that sends it to your endpoint.


What the verifier checks

Every delivery carries an X-Klappay-Signature header shaped like t=1790000000,v1=9f2c…. The v1 value is a hex HMAC-SHA256 of the timestamp, a dot and the raw request body, keyed with your signing secret. To verify, you recompute that value, compare it with v1 and check that t isn't too far from your clock. How crypto payment webhooks work explains why the timestamp is in there.

Each step below breaks one part of that.


The body was parsed and serialized again

Symptom: every delivery fails, including the ones you send yourself with a correct signature.

express.json() turns the body into an object, and JSON.stringify on that object gives a string that can differ from what Klap sent in whitespace or key order. The signature was made over the original string, so the recomputed one never matches.

Fix: give the webhook route the raw body and verify before anything parses it. In Express that means express.raw on this route, and no express.json() ahead of it:

import express from 'express'
import { constructWebhookEvent } from '@klappay/node'
const app = express()
app.post('/webhooks/klappay', express.raw({ type: 'application/json' }), (req, res) => {
try {
const event = constructWebhookEvent(
req.body.toString('utf8'),
req.header('x-klappay-signature') ?? '',
process.env.KLAP_WEBHOOK_SECRET ?? '',
)
console.log(event.event, event.id)
res.sendStatus(200)
} catch {
res.sendStatus(400)
}
})

express.raw hands you a Buffer, which is why the body goes through toString('utf8') before it reaches the verifier. If you also use express.json() globally, mount it after this route, or Express will have parsed the body before your handler runs.

The same function is on the client as klap.webhooks.constructEvent. It verifies the signature, checks the 300-second tolerance and parses the body into a typed event. verifyWebhookSignature only checks the signature.


A trailing newline or a different line ending

Symptom: verification passes on a sample you saved and fails on the real delivery, or the other way around.

The signed bytes have to match exactly. A body that gained a final newline, or whose line endings became CRLF somewhere on the way, produces a different HMAC. It happens when a payload is saved to a file and read back, pasted into a test, or passed through a proxy or logging layer that rewrites it.

Fix: verify the bytes as they arrive, in the handler, and build your tests from a real delivery's raw body instead of a pretty-printed copy. When you test with curl, send the body with --data-binary, because -d strips newlines.


The wrong header name

Symptom: the verifier throws, or reports a missing signature, before it computes anything.

The header is X-Klappay-Signature. Node lowercases incoming header names, so in Express you read x-klappay-signature. Other frameworks keep the case as sent. Read it case-insensitively, and don't confuse it with X-Klappay-Event or X-Klappay-Delivery, which sit next to it and carry other values.

Check the value too. It is a single header with two parts, t= and v1=, separated by a comma, and the verifier needs the whole string.


Comparing with ===

Symptom: verification works, and a review or a security scanner flags it.

A plain string comparison stops at the first character that differs, and the time it takes can leak how much of a guess was right. Use a constant-time comparison such as crypto.timingSafeEqual. The SDK does this for you, which is one reason to use it.


The clock

Symptom: the signature is valid and the delivery is still rejected, and the signature checks out when you compare it by hand.

A valid HMAC on an old timestamp is a replay, so a verifier rejects it when t is too far from now. The SDK's default is 300 seconds. If your server clock is minutes off, every delivery looks old or from the future. Sync it with NTP before you widen the tolerance.

This is also why a signed sample from the tester stops working if you wait. Generate a fresh one right before you send it.


The wrong secret

Symptom: everything above checks out and the signatures still never match.

The key is the full signing secret, whsec_ prefix included. Strip the prefix, cut it at an underscore or load a different environment variable, and you compute a perfectly valid HMAC with the wrong key. Print the length of what the process reads from KLAP_WEBHOOK_SECRET and compare it with what you stored. Whitespace added by a .env file or a secrets manager is a common culprit.

To tell a bad secret from a bad body, test with a throwaway secret: generate a sample in the tester, set that secret as your endpoint's, and send the curl command. A 200 means the verifier is fine and the problem is the real secret. A rejection means the problem is in your code. Don't paste your real secret into any web page, including ours.


A verifier with no SDK

The scheme is plain HMAC, so any language can do it. In Node:

import { createHmac, timingSafeEqual } from 'node:crypto'
const TOLERANCE_SECONDS = 300
export function verify(rawBody: string, header: string, secret: string): boolean {
const parts = Object.fromEntries(header.split(',').map((part) => part.split('=')))
const timestamp = Number(parts.t)
if (!parts.v1 || !Number.isFinite(timestamp)) return false
const expected = createHmac('sha256', secret).update(`${timestamp}.${rawBody}`).digest()
const received = Buffer.from(parts.v1, 'hex')
if (expected.length !== received.length || !timingSafeEqual(expected, received)) return false
return Math.abs(Date.now() / 1000 - timestamp) <= TOLERANCE_SECONDS
}

Check the lengths before timingSafeEqual, because it throws on buffers of different sizes. Parse the body only after this returns true.


Once it verifies

A valid signature means the delivery came from Klap. It doesn't mean you haven't seen it before, and it can arrive more than once. Deduplicate on the event id before you act on it, as idempotent payment webhook handlers shows.