3 min read

Polar Revenue Webhook

Attribute Polar.sh orders, subscription renewals, and refunds using Standard Webhooks signature verification.

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

How do you connect Polar.sh webhooks to TrimStats revenue attribution?

Include window.trimstats.attribution() in Polar checkout metadata (as metadata.trimstats JSON or flat keys), register /v1/webhooks/polar/{siteId} in Polar for order.paid and order.refunded, and save your Standard Webhooks signing secret in TrimStats Site settings.

Attach attribution to Polar checkout metadata

When creating a Polar checkout session on your server, pass the browser attribution snapshot inside metadata.trimstats (or flat string metadata keys vid, uid, ref, lp, utm_source, utm_campaign).

app/api/polar-checkout/route.tsts
1import { Polar } from '@polar-sh/sdk';
2
3const polar = new Polar({ accessToken: process.env.POLAR_ACCESS_TOKEN! });
4
5export async function POST(req: Request) {
6 const { productId, userId, attribution } = await req.json();
7
8 const checkout = await polar.checkouts.create({
9 products: [productId],
10 metadata: {
11 trimstats: JSON.stringify({
12 ...(attribution ?? {}),
13 uid: userId ?? attribution?.uid ?? '',
14 }),
15 },
16 successUrl: 'https://acme.com/thank-you',
17 });
18
19 return Response.json({ url: checkout.url });
20}

Configure Polar Standard Webhooks

Site settings / Revenue Attribution Webhooks
Select Polar in Site settings -> Revenue Attribution Webhooks, paste your polar_whs_... or whsec_... secret, and save.
  1. 1

    Add a Webhook endpoint in Polar

    In your Polar organization settings under Webhooks -> Add Endpoint, set the URL to https://collect.trimstats.com/v1/webhooks/polar/{siteId} and enable `order.paid` and `order.refunded`.

  2. 2

    Save the signing secret in TrimStats

    Copy the secret (polar_whs_... or whsec_...) from Polar and save it under Site settings -> Revenue Attribution Webhooks with provider set to Polar.

Standard Webhooks verification

TrimStats validates the webhook-id, webhook-timestamp, and webhook-signature (v1,<base64>) headers over ${webhookId}.${webhookTimestamp}.${rawBody} using timing-safe comparison and a 5-minute replay tolerance.

Common technical questions

Which Polar webhook signature standard does TrimStats verify?
TrimStats validates Polar.sh Standard Webhooks headers (webhook-id, webhook-timestamp, and v1 base64 webhook-signature) over "${webhookId}.${webhookTimestamp}.${rawBody}" with timing-safe comparison and a 5-minute replay tolerance.
Can I send flat metadata keys to Polar instead of a serialized JSON string?
Yes. TrimStats parses either metadata.trimstats (JSON string) or flat string keys (vid, uid, ref, lp, utm_source, utm_campaign) directly from Polar order payloads.

Related guides