Custom HMAC Webhooks
Send revenue events from any payment processor, e-commerce store, or internal billing service using a simple HMAC-SHA256 signed JSON webhook.
How do you send custom HMAC-signed revenue webhooks to TrimStats?
POST a JSON transaction payload (external_tx_id, amount_cents, currency, tx_type, and optional attribution fields) to /v1/webhooks/custom/{siteId} signed with an X-TrimStats-Signature HMAC-SHA256 hex digest using your site custom webhook secret. Use this endpoint for Shopify, WooCommerce, Lemon Squeezy, or internal billing.
Endpoint & HMAC-SHA256 signature
Because webhook URLs live on the public internet, an unsigned endpoint would let anyone forge fake revenue events. HMAC-SHA256 solves this by hashing the exact raw JSON request body with a shared secret key that only your server and TrimStats know: if a single byte of the amount or transaction ID is tampered with in transit, the signature check rejects the request with 401 Unauthorized.
Generate any strong random shared secret (for example openssl rand -hex 32), save it under Site settings -> Revenue Attribution Webhooks with provider Custom HMAC, and POST JSON payloads to:
/v1/webhooks/custom/{siteId}.POST https://collect.trimstats.com/v1/webhooks/custom/{siteId}Content-Type: application/jsonx-trimstats-signature: sha256=<hex_hmac_sha256_of_raw_body>Compute HMAC-SHA256(secret, rawRequestBody) as a lowercase hex digest and send it in the x-trimstats-signature header (with or without the sha256= prefix).
JSON payload schema
tx_idstringRequiredUnique transaction or order identifier (id is also accepted). Used for idempotent deduplication in ClickHouse.
amount_centsinteger (non-zero)RequiredTransaction amount in minor currency units (for example 4900 for $49.00, or -4900 for a refund).
currencystring (3-letter ISO)OptionalDefault: "USD"ISO 4217 currency code such as USD, EUR, GBP, or JPY.
tx_type"one_time" | "subscription_new" | "subscription_renewal" | "refund"OptionalDefault: "one_time" (or "refund" if amount < 0)Classifies the payment event in revenue reporting and visitor timelines.
countrystring (2-letter ISO)OptionalDefault: "XX" (falls back to ClickHouse visitor country)Optional customer billing country code.
timestampISO 8601 stringOptionalDefault: Current server timeOptional original transaction timestamp.
attributionobjectOptionalSnapshot returned by window.trimstats?.attribution?.() containing any of vid, uid, ref, lp, utm_source, utm_campaign.
Node.js & cURL examples
1import crypto from 'node:crypto';23const secret = process.env.TRIMSTATS_WEBHOOK_SECRET!;4const siteId = process.env.TRIMSTATS_SITE_ID!;56const body = JSON.stringify({7 tx_id: 'ord_2026_00481',8 amount_cents: 2900,9 currency: 'USD',10 tx_type: 'subscription_new',11 country: 'US',12 attribution: {13 vid: 'f4c9a1e2-8b3d-4c11-9d2e-1a2b3c4d5e6f',14 uid: 'user_12345',15 ref: 'https://news.ycombinator.com/',16 lp: '/pricing',17 utm_source: 'launch',18 utm_campaign: 'q4',19 },20});2122const signature = crypto23 .createHmac('sha256', secret)24 .update(body, 'utf8')25 .digest('hex');2627const res = await fetch(`https://collect.trimstats.com/v1/webhooks/custom/${siteId}`, {28 method: 'POST',29 headers: {30 'content-type': 'application/json',31 'x-trimstats-signature': `sha256=${signature}`,32 },33 body,34});3536console.log(res.status, await res.json()); // 202 { recorded: true, txId: 'ord_2026_00481' }Common technical questions
How should refunds be formatted on the Custom HMAC webhook endpoint?
What happens if my backend retries the same custom webhook event twice?
Related guides
Connect payment provider webhooks directly to TrimStats to attribute one-time orders, subscriptions, renewals, and refunds to referrers, UTM campaigns, and landing pages.
HTTP specification for POST /v1/collect, payload validation rules, rate/quota responses, and server-side event ingestion.