Stripe Revenue Webhook
Step-by-step guide to forwarding Stripe checkout sessions, subscription invoices, and refunds to TrimStats with HMAC verification.
How do you connect Stripe webhooks to TrimStats for SaaS revenue attribution?
Pass window.trimstats.attribution() inside metadata.trimstats and subscription_data.metadata when creating Stripe Checkout Sessions, point a Stripe webhook endpoint to /v1/webhooks/stripe/{siteId} for checkout.session.completed, invoice.paid, and charge.refunded, and save your whsec_ signing secret in TrimStats.
Supported Stripe webhook events
| Stripe event | TrimStats `tx_type` | Notes |
|---|---|---|
checkout.session.completed | one_time or subscription_new | Reads amount_total, currency, metadata, client_reference_id, and customer country. |
invoice.paid | subscription_renewal or subscription_new | Classified as subscription_renewal when billing_reason === "subscription_cycle". |
charge.refunded | refund (negative amount) | Subtracts amount_refunded from net revenue using transaction ID refund:<charge_id>. |
Any other event type sent to your TrimStats Stripe webhook returns 200 OK with { "ignored": true } so Stripe never marks the endpoint as failing.
Configure Stripe in three steps
whsec_... signing secret from the Stripe Dashboard to activate verified ingestion.- 1
Pass attribution metadata when creating a Checkout Session
Include
metadata.trimstats(JSON-stringified snapshot fromwindow.trimstats?.attribution?.()) and optionallyclient_reference_idfor logged-in users.app/api/checkout/route.tsts1import Stripe from 'stripe';23const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);45export async function POST(req: Request) {6 const { priceId, userId, attribution } = await req.json();78 const session = await stripe.checkout.sessions.create({9 mode: 'subscription',10 line_items: [{ price: priceId, quantity: 1 }],11 client_reference_id: userId,12 metadata: {13 trimstats: JSON.stringify(attribution ?? {}),14 },15 subscription_data: {16 metadata: {17 trimstats: JSON.stringify(attribution ?? {}),18 },19 },20 success_url: 'https://acme.com/thank-you',21 cancel_url: 'https://acme.com/pricing',22 });2324 return Response.json({ url: session.url });25} - 2
Create the webhook endpoint in Stripe Dashboard
In Stripe under Developers -> Webhooks -> Add endpoint, paste your site webhook URL from Site settings:
https://collect.trimstats.com/v1/webhooks/stripe/{siteId}. Subscribe tocheckout.session.completed,invoice.paid, andcharge.refunded. - 3
Save the signing secret in TrimStats
Copy the
whsec_...signing secret from Stripe, select Stripe under Site settings -> Revenue Attribution Webhooks, paste the secret, and click Save webhook. TrimStats verifies thestripe-signatureHMAC-SHA256 header on raw bytes with a 5-minute replay tolerance.
Common technical questions
Why should I copy metadata.trimstats into subscription_data.metadata in Stripe?
How does TrimStats verify incoming Stripe webhook authenticity?
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.
Choose per site between zero-storage cookie-less analytics and persistent first-party visitor IDs, plus optional logged-in user identification.
Connect Paddle Billing webhooks and custom_data attribution to track SaaS subscriptions and one-time payments in TrimStats.