4 min read

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.

Imtiaz Rayhan
Imtiaz RayhanCo-Founder & Lead Engineer, DotCamp & TrimStats
Verified against live TrimStats APIs
Quick reference

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:

Site settings / Revenue Attribution Webhooks
Select Custom HMAC, paste your generated shared secret, and point your checkout backend at /v1/webhooks/custom/{siteId}.
HTTP
POST https://collect.trimstats.com/v1/webhooks/custom/{siteId}
Content-Type: application/json
x-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_idstringRequired

Unique transaction or order identifier (id is also accepted). Used for idempotent deduplication in ClickHouse.

amount_centsinteger (non-zero)Required

Transaction 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 time

Optional original transaction timestamp.

attributionobjectOptional

Snapshot 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';
2
3const secret = process.env.TRIMSTATS_WEBHOOK_SECRET!;
4const siteId = process.env.TRIMSTATS_SITE_ID!;
5
6const 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});
21
22const signature = crypto
23 .createHmac('sha256', secret)
24 .update(body, 'utf8')
25 .digest('hex');
26
27const 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});
35
36console.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?
Send tx_type: "refund" with a negative integer amount_cents (or a distinct refund external_tx_id such as refund:ord_123) so ClickHouse deduplicates retries and accurately subtracts refunded amounts from net revenue.
What happens if my backend retries the same custom webhook event twice?
TrimStats deduplicates custom revenue rows by external_tx_id in ClickHouse, making network retries and queue redeliveries safely idempotent.

Related guides