Webhook signature
tester
Get a signed sample webhook from a throwaway secret and send it to your own endpoint with curl, to see whether your verifier accepts it.
Never paste your real signing secret into a web page, including this one. This page only uses a throwaway secret it generated itself.
Throwaway signing secret
Generating…
Signature header
Generating…
Raw body
Generating…
The SDK rejects a delivery whose timestamp is more than 300 seconds away from now, so generate a fresh sample right before you send it.
The URL only goes into the curl command below. It is never sent anywhere and never recorded.
curl
Generating…
How verification works
Four steps your endpoint runs on every delivery, in this order.
- Split the header
- X-Klappay-Signature looks like t=1790000000,v1=9f2c…. Read t as the Unix timestamp in seconds and v1 as the hex signature.
- Sign the timestamp and the raw body
- Compute HMAC-SHA256 over the string t, a dot, then the exact body bytes Klap sent. The key is the full signing secret, whsec_ prefix included. Hex-encode the result.
- Compare in constant time
- Compare your hex digest with v1 using a constant-time function such as crypto.timingSafeEqual. A plain === leaks how many leading characters matched.
- Reject old timestamps
- A valid signature on an old delivery is a replay. Reject it when t is too far from your clock. The SDK default is 300 seconds. Deduplicate by the event id for the narrower window inside it.
Why a signature fails
The usual causes when a correct secret still gives an invalid signature.
- The body was parsed and serialized again
- express.json() or a similar middleware turns the body into an object, and JSON.stringify gives different bytes. Verify against the raw string the framework received, before any parsing.
- A trailing newline or a changed line ending
- Signed bytes must match exactly. A body that gained a final newline, or whose line endings became CRLF, produces a different HMAC.
- The wrong header name
- The header is X-Klappay-Signature. Node lowercases it to x-klappay-signature, and other frameworks keep the case you wrote. Read it case-insensitively.
- Comparing with ===
- It works in a test and leaks timing in production. Use a constant-time comparison, or let the SDK do it.
- Clock skew
- If your server clock is minutes off, every delivery looks old or from the future. Sync it with NTP before you widen the tolerance.
Verify with your real secret
To check a delivery with your real secret, do it on your own machine with @klappay/node. constructWebhookEvent verifies the signature, checks the 300-second tolerance and parses the body. verifyWebhookSignature only checks the signature. The same two functions are on the client as klap.webhooks.constructEvent and klap.webhooks.verifySignature.
server.ts
import express from 'express'
import { constructWebhookEvent } from '@klappay/node'
const app = express()
app.post('/webhooks', 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)
}
})
app.listen(3000)Start the server with KLAP_WEBHOOK_SECRET set to the whsec_ value shown above, then run the curl command. A 200 means your verifier accepted the sample. Remember that express.raw hands you a Buffer, which is why the body is read with toString.
Related reading
- How Crypto Payment Webhooks WorkA webhook doesn't settle funds. It reports a settlement that already happened on-chain. The event flow, the payload, and why signing and idempotency count.
- Idempotent Payment Webhooks: How to Stop Double CreditsA payment webhook is a delivery attempt, not a receipt. How to make a retried or out-of-order event change your books once, enforced by the database.
- Node.js SDK for Crypto Payments: What to Look ForA checklist for picking a Node.js crypto payment SDK: who holds the funds, whether it models the real payment lifecycle, and what its sandbox can trigger.
Questions
How do I test my Klap webhook signature verification?
Generate a sample here, set the throwaway secret as your endpoint’s signing secret, and send the curl command to your local server. If your verifier returns 200 on the sample and rejects it after you change one character of the body, it works.
Can I paste my real signing secret here?
No, and the page has no field for it. Anyone who holds your secret can forge deliveries, so it should only exist on your server. Test with the throwaway secret, or call the SDK locally with your real one.
How is the Klap signature computed?
It is the hex HMAC-SHA256 of the timestamp, a dot and the raw request body, keyed with the full whsec_ secret. The header carries the timestamp and the signature as t=<seconds>,v1=<hex>.
Why does my verifier say the signature is invalid?
Almost always because it hashed a re-serialized body instead of the raw one. Check the framework’s body parser first, then trailing newlines and line endings, then the header name.
Does this page send my URL or the sample anywhere?
No. The secret, the payload and the signature are made in your browser with the Web Crypto API and stay on the page. The URL you type only ends up in the curl command. Nothing is stored.