> ## 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 Stripe revenue to CrispStats

> Connect Stripe with a restricted key: CrispStats creates its webhook, imports every past payment and ties each sale to the visit that led to it through the Checkout Session metadata.

Connect Stripe by pasting a restricted key. CrispStats creates its own webhook in your Stripe account, imports every payment made before you connected, and from then on records one-time payments, new subscriptions, renewals and refunds as they happen. Put the visitor id in the Checkout Session's metadata and each sale is tied to the visit that led to it.

| | |
| - | - |
| You paste | A restricted key, made with a link that fills in its permissions |
| New payments | Within seconds, by webhook. A check every 15 minutes catches anything the webhook missed |
| History | Every payment since your first |
| Test mode | A `rk_test_` key connects in test mode |
| Linked to a visit | When `crispstats_visitor_id` is in the Checkout Session metadata |

## What is recorded

| Stripe object | Recorded as |
| - | - |
| A paid Checkout Session or Payment Link | `one_time` (mode `payment`) or `subscription_new` (mode `subscription`). Setup-mode sessions and sessions with a total of zero are skipped. |
| A paid subscription invoice | The first (`subscription_create`) is `subscription_new`, later ones are `subscription_renewal`. Other invoices are skipped. |
| A succeeded Payment Intent outside Checkout | `one_time`, only when its own metadata carries `crispstats_visitor_id`. |
| A refund | `refund`, for the amount refunded. |

Every payment is read back from Stripe's API with your key before it counts as verified revenue. The product name comes from the Checkout Session's line items, or from the product on a renewal invoice. The customer's email is hashed and masked before it is stored. See [email hashing](https://docs.crispstats.com/revenue/refunds-and-renewals.md#customer-email-hashing-and-masking).

## Connect Stripe

You need permission to manage the site's settings in CrispStats.

**1. Open the Stripe panel**

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

**2. Create the restricted key**

Under **Stripe restricted key**, select **Create a restricted key in Stripe**. Then:

1. Stripe opens with the name and permissions filled in.
2. Scroll down and choose **Create key**. Leave the permissions as they are.
3. Copy the key and paste it here.
4. Keep the key. Deleting it stops new sales from reaching CrispStats.

The key starts with `rk_live_`.

**3. Paste the key and connect**

Paste the key into **Stripe restricted key**. CrispStats checks it with Stripe at once. Then select **Connect Stripe**.

CrispStats creates one webhook endpoint for this site in your Stripe account, saves its signing secret and your key encrypted, and starts importing past payments. The row shows **Connected. Live: sales arrive the moment Stripe sends them.**

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

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

What the panel refuses, and why:

* A secret key (`sk_live_`): "That is a secret key with full access to your Stripe account. Create a restricted key with the link instead."
* A publishable key (`pk_live_`) or a webhook signing secret (`whsec_`): the panel asks for a restricted key, which starts with `rk_`.
* A restricted key without the webhook permission: "This key cannot manage webhooks. Create the key again with the link: it fills in every permission."

## What the key can do

The link opens Stripe's **Create restricted key** form with these six permissions. Only the first can change anything:

| Resource | Permission | Why |
| - | - | - |
| Webhook Endpoints | Write | To create the endpoint, delete it when you disconnect, and read recent events as a safety net |
| Checkout Sessions | Read | A session's line items for the product name, and past Checkout payments |
| Products | Read | The product named on a renewal invoice |
| Invoices | Read | Past subscription payments |
| PaymentIntents | Read | Past payments taken without Checkout that carry the visitor id |
| Charges | Read | Refunds (Stripe lists refunds under this permission) |

Leave every other resource at **None**. The key cannot create charges, refunds, customers or payouts, and CrispStats never moves money. The key does not ask to read your account details, so the row may show **Connected** without your account's name.

The key is used only on CrispStats' servers, only with Stripe's API, and is never shown again.

> [!NOTE]
>
> The link fills in the form with parameters Stripe does not document. If the form opens without the six permissions ticked, tick them by hand from the table above.

## How fresh the numbers are

Stripe sends each payment to the webhook within seconds, and CrispStats reads it back from Stripe's API before storing it. Every 15 minutes CrispStats also asks Stripe for recent events, so a payment the webhook missed appears within 15 minutes at worst. A payment seen both ways is stored once.

## Past payments

When you connect, CrispStats imports every Checkout payment, subscription invoice, Payment Intent with a visitor id and refund since your first payment. Progress shows on the Stripe row. See [revenue history](https://docs.crispstats.com/revenue/history.md).

## Test mode

A key from a Stripe sandbox or test mode starts with `rk_test_`. The panel says "This is a test key. Test payments show with a Test tag and never count as verified revenue." and offers **Connect in test mode** or **Use a live key**. Test payments appear on the Revenue screen with a **Test** tag and are left out of every total.

## Pass the visitor id at checkout

Put the id from `crispstats.id()` in the Checkout Session's `metadata` as `crispstats_visitor_id`. For subscriptions, put it in `subscription_data.metadata` too, so renewals carry it:

```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 Session
const session = await stripe.checkout.sessions.create({
  // ...your line items and URLs
  metadata: { crispstats_visitor_id: visitorId },
  // Subscriptions only: renewals carry the subscription's metadata
  subscription_data: { metadata: { crispstats_visitor_id: visitorId } },
});
```

**Payment Links** have no metadata you can set per visitor. Add the id to the link's URL as `client_reference_id`:

```js theme={"theme":{"light":"github-light","dark":"github-dark"}}
const visitorId = await crispstats.id();
const url = new URL("https://buy.stripe.com/your-link");
if (visitorId) url.searchParams.set("client_reference_id", visitorId);
window.location.href = url.toString();
```

A `client_reference_id` that is not 32 hexadecimal characters is ignored, so your own order ids there never become visitor ids. When both are set, the metadata wins. For a Payment Intent created without Checkout, put `crispstats_visitor_id` in the Payment Intent's `metadata`.

A payment without a visitor id is still recorded and counted. It is not linked to a visit. See [pass the visitor id to your checkout](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md) for proxies, single-page apps and attributing across days.

## Disconnect

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

If Stripe does not let CrispStats delete the endpoint (the key was already deleted, for example), the message says so: delete the endpoint in Stripe under **Workbench**, then **Webhooks**.

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

## Next steps

> [card] [Pass the visitor id](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md)
>
> Proxies, single-page apps and Payment Links.

> [card] [Refunds and renewals](https://docs.crispstats.com/revenue/refunds-and-renewals.md)
>
> How each payment kind is recorded and deduplicated.

> [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.