Skip to content

Webhook security ​

Every webhook request is signed so you can prove it came from Kolwire and wasn't replayed.

The scheme ​

http
POST /your/webhook
Content-Type: application/json
Kolwire-Signature: t=1763892900,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
  • t — Unix timestamp (seconds) when we signed the request.
  • v1 — hex-encoded HMAC-SHA256(secret, "<t>.<raw request body>").

To verify:

  1. Read the raw body bytes. Don't parse and re-serialize JSON first — any whitespace change breaks the signature.
  2. Compute HMAC-SHA256(secret, t + "." + body) and compare it to v1 in constant time.
  3. Reject if |now − t| > 300 seconds (replay protection).

The secret is shown once when you create a webhook endpoint (vi.webhookEndpoints.create(...) returns secret).

With the SDKs ​

ts
import express from "express";
import { Kolwire, WebhookVerificationError } from "@kolwire/sdk";

const vi = new Kolwire({ webhookSecret: process.env.KOLWIRE_WEBHOOK_SECRET });
const app = express();

app.post("/webhooks", express.raw({ type: "application/json" }), async (req, res) => {
  try {
    const event = await vi.webhooks.constructEvent(req.body, req.headers);
    // …handle event…
    res.sendStatus(204);
  } catch (err) {
    if (err instanceof WebhookVerificationError) return res.sendStatus(400);
    throw err;
  }
});
ts
// Next.js route handler, Hono, Cloudflare Workers, Vercel Edge — verification uses WebCrypto.
import { Kolwire } from "@kolwire/sdk";
const vi = new Kolwire();

export async function POST(request: Request) {
  const event = await vi.webhooks.unwrap(request); // reads raw body, verifies, parses
  return new Response(null, { status: 204 });
}
python
from kolwire import Kolwire, WebhookVerificationError

client = Kolwire(webhook_secret=os.environ["KOLWIRE_WEBHOOK_SECRET"])

@app.post("/webhooks")
async def webhooks(request: Request):
    try:
        event = client.webhooks.construct_event(await request.body(), request.headers)
    except WebhookVerificationError:
        return Response(status_code=400)
    ...

Standalone helpers (no client needed): Node constructEvent(body, headers, secret), verifySignature, isValidSignature, signPayload (from @kolwire/sdk/webhooks); Python from kolwire.webhooks import construct_event, verify_signature. All accept a list of secrets, and a custom tolerance.

Without an SDK ​

js
import { createHmac, timingSafeEqual } from "node:crypto";

function verify(secret, rawBody, header, toleranceSec = 300) {
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=", 2)));
  const t = Number(parts.t);
  if (!Number.isFinite(t) || Math.abs(Date.now() / 1000 - t) > toleranceSec || !parts.v1) return false;
  const expected = createHmac("sha256", secret).update(`${t}.${rawBody}`).digest("hex");
  const a = Buffer.from(expected, "hex"), b = Buffer.from(parts.v1, "hex");
  return a.length === b.length && timingSafeEqual(a, b);
}
python
import hashlib, hmac, time

def verify(secret: str, raw_body: bytes, header: str, tolerance: int = 300) -> bool:
    parts = dict(p.split("=", 1) for p in header.split(",") if "=" in p)
    try:
        t = int(parts["t"])
    except (KeyError, ValueError):
        return False
    if abs(time.time() - t) > tolerance or "v1" not in parts:
        return False
    expected = hmac.new(secret.encode(), f"{t}.".encode() + raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, parts["v1"])

Secret rotation ​

Roll the secret on the endpoint, then accept both old and new secrets until deliveries signed with the old one stop:

ts
const vi = new Kolwire({ webhookSecret: [process.env.WEBHOOK_SECRET_NEW!, process.env.WEBHOOK_SECRET_OLD!] });
python
event = construct_event(body, headers, secret=[new_secret, old_secret])

Delivery behaviour ​

  • Respond with any 2xx within 10 seconds. For call webhooks, the body of your response is read as { commands }.
  • Non-2xx responses and timeouts are retried with exponential backoff; after the last attempt the delivery is marked dead and can be replayed from the dashboard.
  • Deliveries are at-least-once — de-duplicate on the event id.