> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crispstats.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> CrispStats is a hosted, privacy-first web analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing, no mobile SDKs and no data import except a site's history from a Plausible export (Settings, Import and export).
> The script global is crispstats(...), site keys start with cs_pk_ and secret API keys start with cs_sk_. The script is served from https://in.crispstats.com/d.js and the REST API lives at https://crispstats.com/api/v1.
> There is no npm package to install. Integrations copy the script tag, the queue stub, crispstats.d.ts and proxy code from these docs or from Settings, Tracking code in the dashboard. A first-party proxy forwards one path of the site to https://in.crispstats.com/p/ and forwards only content-type, user-agent, accept-language, referer, origin, if-none-match and x-forwarded-for, never cookies.

# Connect Polar revenue to CrispStats

> Connect Polar with an organization access token: CrispStats creates its webhook, imports past orders and refunds, and ties each order to a visit through the checkout metadata.

Connect Polar by pasting an organization access token. CrispStats imports your past orders and refunds, creates its own webhook when the token allows it, and records one-time purchases, new subscriptions, renewals and refunds from then on. Put the visitor id in the checkout's metadata and each order is tied to the visit that led to it.

| | |
| - | - |
| You paste | An organization access token (`polar_oat_`) |
| New payments | Within seconds by webhook when the token has `webhooks:write`, otherwise within a minute |
| History | Every order since your first |
| Test mode | A token from Polar's sandbox connects in test mode |
| Linked to a visit | When `crispstats_visitor_id` is in the checkout metadata |

## What is recorded

| Polar object | Recorded as |
| - | - |
| A paid order with a total above zero | `billing_reason` sets the kind: `purchase` is `one_time`, `subscription_create` is `subscription_new`, `subscription_cycle`, `subscription_update` and `subscription_meter_cycle` are `subscription_renewal`. |
| A refund | `refund`, for the amount refunded, dated when Polar made it. |

Every order is read back from Polar's API with your token before it counts as verified revenue. Each payment carries the product name, the currency, the order's time and the customer's email, hashed and masked before storage.

## Connect Polar

**1. Open the Polar panel**

In CrispStats, open the site's **Settings**, then **Integrations**. Select **Connect a payment provider** and choose **Polar**.

**2. Create the token**

Under **Polar organization access token**, select **Create an access token in Polar**. Then:

1. Open **Settings**, then **Access tokens**, and choose **Create token**.
2. Name it CrispStats and choose **No expiration**.
3. Tick `orders:read`, `refunds:read`, `organizations:read` and `webhooks:write`.
4. Create the token and paste it here.

**3. Paste the token and connect**

Paste the token. CrispStats checks it with Polar at once and shows your organization's name. Select **Connect Polar**.

With `webhooks:write`, CrispStats creates one webhook endpoint for this site and the row shows **Connected to** your organization. **Live.** Without it, the row says "Updates every minute. Add webhooks:write to the token to see sales the moment they happen."

**4. Pass the visitor id at checkout**

See [pass the visitor id at checkout](#pass-the-visitor-id-at-checkout) below.

A personal access token (`polar_pat_`) is refused: "That is a personal access token for every organization you belong to. Create an organization access token instead." Polar tokens cannot be edited once made, so a token that lacks a scope is refused with the scope it needs: create a new token with it ticked.

## What the token can do

| Scope | Why |
| - | - |
| `orders:read` | Read each order, past and new, to verify it and import history |
| `refunds:read` | Read refunds with their real dates, including refunds of older orders |
| `organizations:read` | Read your organization's id and name, so one organization is not connected to two workspaces |
| `webhooks:write` | Create the webhook endpoint, and delete it when you disconnect. Optional: without it CrispStats checks every minute |

CrispStats does not ask for `subscriptions:read` (orders say whether they are new or a renewal), customer scopes, or any scope that changes products, checkouts or refunds. It never moves money.

## How fresh the numbers are

With `webhooks:write`, Polar sends each order within seconds and CrispStats checks every 15 minutes as a safety net. Without it, CrispStats reads new orders and refunds every minute, so a sale appears within a minute. An order seen both ways is stored once.

## Past payments

When you connect, CrispStats imports every paid order and refund since your first. See [revenue history](https://docs.crispstats.com/revenue/history.md).

## Test mode

Tokens made in Polar's sandbox (`sandbox.polar.sh`) connect to the sandbox. The panel says "This is a test token. Test payments show with a Test tag and never count as verified revenue." and offers **Connect in test mode** or **Use a live key**. Sandbox orders show with a **Test** tag and are left out of every total.

## Pass the visitor id at checkout

Add the id from `crispstats.id()` to the checkout's `metadata` as `crispstats_visitor_id`:

```js theme={"theme":{"light":"github-light","dark":"github-dark"}}
// In the browser, before you send the visitor to checkout
const visitorId = await crispstats.id();

// On your server, when you create the checkout
const checkout = await polar.checkouts.create({
  // ...your products
  metadata: { crispstats_visitor_id: visitorId },
});
```

The order's metadata is merged over the customer's metadata, so an id on the customer also counts. The customer's `external_id` is used as your own user id when the metadata has none. An order without a visitor id is still recorded and counted, without a visit.

## Disconnect

Select **Disconnect** on the Polar row. CrispStats deletes its webhook endpoint in Polar and destroys the saved token and signing secret. Payments already recorded stay. You can then delete the token in Polar.

> [!NOTE]
>
> Connections made earlier by pasting a webhook secret keep working. They cannot import history or verify payments: connect again with a token to add both.

## Next steps

> [card] [Pass the visitor id](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md)
>
> When there is no id, and attributing across days.

> [card] [Refunds and renewals](https://docs.crispstats.com/revenue/refunds-and-renewals.md)
>
> How payment kinds are recorded across providers.

> [card] [Revenue history](https://docs.crispstats.com/revenue/history.md)
>
> What the import reads and how it avoids counting twice.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.