TrustColdDevelopers
Notifications›Verify signatures

Verify signatures

Prove a webhook came from TrustCold and was not replayed. Code for PHP, Node and Python.

Run this from your browser — add your free test keys.

Key points

  • Raw body — Hash the exact bytes received, before any JSON parsing.
  • Timestamp — Reject anything older than 5 minutes to stop replays.
  • Constant time — Compare with hash_equals / timingSafeEqual / compare_digest.
  • Dedupe — Ignore an event_id you have already processed.

Every webhook carries a signature. Check it before you trust the body.

How the signature is built

  1. Read the raw request body as bytes. Do not parse and re-serialize it.
  2. Read X-TrustCold-Signature-V2. It looks like t=1763632364,v1=9f2c….
  3. Reject the request if t is more than 300 seconds away from your clock.
  4. Compute HMAC-SHA256(secret, t + "." + rawBody) as lowercase hex.
  5. Compare with v1 in constant time. Mismatch: answer 401 and do nothing.
  6. Match: reply 200 quickly, then process. Skip an event_id you have already handled.
<?php
$secret = getenv('TRUSTCOLD_WEBHOOK_SECRET');
$raw    = file_get_contents('php://input');
$header = $_SERVER['HTTP_X_TRUSTCOLD_SIGNATURE_V2'] ?? '';

parse_str(str_replace(',', '&', $header), $parts);   // t=...&v1=...
$t  = $parts['t']  ?? '';
$v1 = $parts['v1'] ?? '';

if (!ctype_digit($t) || abs(time() - (int) $t) > 300) {
    http_response_code(400); exit;
}
$expected = hash_hmac('sha256', $t . '.' . $raw, $secret);
if (!hash_equals($expected, $v1)) {
    http_response_code(401); exit;
}

$event = json_decode($raw, true);
// if already_processed($event['event_id']) { http_response_code(200); exit; }
http_response_code(200);
// ...then update your order from $event['trip']['status']
import express from "express";
import crypto from "node:crypto";

const app = express();
const secret = process.env.TRUSTCOLD_WEBHOOK_SECRET;

// raw body: do NOT use express.json() on this route
app.post("/trustcold/hooks", express.raw({ type: "application/json" }), (req, res) => {
  const header = req.get("X-TrustCold-Signature-V2") || "";
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
  const t = parts.t || "";
  const v1 = parts.v1 || "";

  if (!/^\d+$/.test(t) || Math.abs(Date.now() / 1000 - Number(t)) > 300) {
    return res.sendStatus(400);
  }
  const expected = crypto.createHmac("sha256", secret)
    .update(t + "." + req.body.toString("utf8")).digest("hex");
  const ok = v1.length === expected.length &&
    crypto.timingSafeEqual(Buffer.from(v1), Buffer.from(expected));
  if (!ok) return res.sendStatus(401);

  const event = JSON.parse(req.body.toString("utf8"));
  res.sendStatus(200);
  // then update your order from event.trip.status
});
import hmac, hashlib, os, time, json
from flask import Flask, request, abort

app = Flask(__name__)
SECRET = os.environ["TRUSTCOLD_WEBHOOK_SECRET"].encode()

@app.post("/trustcold/hooks")
def hook():
    raw = request.get_data()                      # raw bytes
    header = request.headers.get("X-TrustCold-Signature-V2", "")
    parts = dict(p.split("=", 1) for p in header.split(",") if "=" in p)
    t, v1 = parts.get("t", ""), parts.get("v1", "")

    if not t.isdigit() or abs(time.time() - int(t)) > 300:
        abort(400)
    expected = hmac.new(SECRET, t.encode() + b"." + raw, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, v1):
        abort(401)

    event = json.loads(raw)
    # skip if event["event_id"] was already processed
    return "", 200

Test it

Register your URL, then call POST /webhook/test. A correct receiver answers 200 and the call returns "result": "delivered".

Older integrations that only check X-TrustCold-Signature keep working. Move to V2 when you can: the old header has no timestamp, so a captured request could be replayed.

Also check

  • Treat event_id as an idempotency key: the same event can arrive twice.
  • Use the trip status in the body. Do not rely on the order events arrive in.
  • Keep the secret out of your repository and logs. Rotate it if it leaks.