4 min read

Stripe Revenue Webhook

Step-by-step guide to forwarding Stripe checkout sessions, subscription invoices, and refunds to TrimStats with HMAC verification.

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

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 eventTrimStats `tx_type`Notes
checkout.session.completedone_time or subscription_newReads amount_total, currency, metadata, client_reference_id, and customer country.
invoice.paidsubscription_renewal or subscription_newClassified as subscription_renewal when billing_reason === "subscription_cycle".
charge.refundedrefund (negative amount)Subtracts amount_refunded from net revenue using transaction ID refund:<charge_id>.
Other Stripe events are safely ignored

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

Site settings / Revenue Attribution Webhooks
Copy your site Stripe endpoint URL here and paste the whsec_... signing secret from the Stripe Dashboard to activate verified ingestion.
  1. 1

    Pass attribution metadata when creating a Checkout Session

    Include metadata.trimstats (JSON-stringified snapshot from window.trimstats?.attribution?.()) and optionally client_reference_id for logged-in users.

    app/api/checkout/route.tsts
    1import Stripe from 'stripe';
    2
    3const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
    4
    5export async function POST(req: Request) {
    6 const { priceId, userId, attribution } = await req.json();
    7
    8 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 });
    23
    24 return Response.json({ url: session.url });
    25}
  2. 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 to checkout.session.completed, invoice.paid, and charge.refunded.

  3. 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 the stripe-signature HMAC-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?
Stripe copies subscription_data.metadata onto the Subscription object so every future monthly or annual invoice.paid renewal webhook carries your original acquisition attribution alongside ClickHouse journey lookup.
How does TrimStats verify incoming Stripe webhook authenticity?
The collector validates the stripe-signature HMAC-SHA256 header against the exact raw HTTP request body using your encrypted whsec_ secret with a 5-minute timestamp replay window.

Related guides