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

# Report payments to CrispStats with the payment API

> Report payments from any provider or your own billing with a server-side revenue event, trackServer, or crispstats.revenue() in the browser.

If your payment provider is not Stripe, Polar, Paddle, Dodo Payments, Creem, Whop, Razorpay, Lemon Squeezy, Shopify, RevenueCat, Superwall, the App Store or the Shopify App Store, or you run your own billing, report each payment to CrispStats yourself. Send a `revenue` event from your server with a secret API key and the visitor id you saved at checkout. The payment is recorded and attributed exactly like a webhook payment, with provider `api`.

Amounts are integers in minor units (`4900` is 49.00) with an ISO 4217 currency code. The same instructions are in **Settings**, **Integrations**, under **Payment API**, **View instructions**.

## Report a payment from your server

You need:

* A secret API key (`cs_sk_…`) with the `events` scope. Create one in **Settings**, **API keys**. See [API keys](https://docs.crispstats.com/account/api-keys.md). Never ship it to a browser.
* The site's public key (`cs_pk_…`).
* The visitor id from [`crispstats.id()`](https://docs.crispstats.com/revenue/visitor-id-at-checkout), saved with the order when the visitor checked out.

Send the event when the payment succeeds, for example in your provider's webhook handler:

```bash curl theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST https://in.crispstats.com/e \
  -H "authorization: Bearer cs_sk_..." \
  -H "content-type: application/json" \
  -d '{"k":"cs_pk_xxxxxxxx","t":"revenue","a":4900,"c":"USD","u":"","vid":"<visitor id>"}'
```

```ts Node.js theme={"theme":{"light":"github-light","dark":"github-dark"}}
// Runs on your server, never in a browser: it sends your secret key.
const response = await fetch("https://in.crispstats.com/e", {
  method: "POST",
  headers: {
    authorization: `Bearer ${process.env.CRISPSTATS_API_KEY}`, // cs_sk_…
    "content-type": "application/json",
  },
  body: JSON.stringify({
    k: "cs_pk_xxxxxxxx",
    t: "revenue",
    a: 4900, // minor units: 49.00
    c: "USD",
    u: "",
    vid: order.visitorId, // from crispstats.id(), saved at checkout
  }),
});
const accepted = response.status === 202;
```

The `authorization` header must start with `Bearer cs_sk_`. Without it the request is handled as a browser event: it gets an empty `202` without any checks and is then dropped, because it does not come from a browser.

A `202` response with an empty body means the payment was accepted. It appears under **Payments** on the Revenue screen within a few seconds. The example uses the global `fetch`, so it runs on Node.js 18 and later, Cloudflare Workers, Vercel Edge, Deno and Bun.

### Request body

**k** `string` *required*

The site's public key, `cs_pk_…`.

**t** `string` *required*

`revenue`.

**u** `string` *required*

The page URL the payment relates to, or `""`.

**a** `number` *required*

The amount as an integer in minor units. `4900` is 49.00. A number with decimals fails with `400`. A negative amount is recorded as a refund (only with a secret key, as here), and `0` records no payment.

**c** `string`

The three-letter ISO 4217 currency code, such as `USD`. It must be exactly three characters, or the request fails with `400`. When it is missing, or its three characters are not all letters, the site's currency is used.

**vid** `string`

The visitor id from `crispstats.id()`, 32 lowercase hex characters. It ties the payment to that visitor's visits. A value of another shape is ignored, and one over 64 characters fails with `400`. Without a visitor id or a user id (`i`) the payment is recorded but not tied to a visit.

**i** `string`

Your own user id, as passed to `crispstats.identify()`, up to 200 characters. Attribution then uses that user's sessions. `trackServer` sends its `userId` option here.

**n** `string`

An event name, up to 120 characters. Shown as the payment's product when `p.product` is not set. `trackServer` sends its `name` option here.

**p** `object`

Flat properties with keys up to 60 characters: strings (up to 500 characters), numbers, booleans or `null`. Set `product` to name the product in the payments table. `trackServer` sends its `props` option here.

The whole body may be at most 16 KB. The payment's time is the time the collector receives it, so you cannot backdate payments. A `ts` field in the body is not used for the time.

### Responses

| Status | Body | Meaning |
| - | - | - |
| `202` | Empty | Accepted. |
| `400` | `{"error":"invalid_payload","fields":["a"]}` | A field is missing or has the wrong type. `fields` names up to 10 of them. |
| `400` | `{"error":"invalid_json"}` | The body is not a JSON object. |
| `401` | `{"error":"invalid_api_key"}` | The key is unknown, revoked, expired or malformed. |
| `403` | `{"error":"key_not_valid_for_site"}` | The public key is unknown, the API key does not cover this site, or it has none of the `events`, `write` or `admin` scopes. |
| `413` | `{"error":"payload_too_large"}` | The body is over 16 KB. |
| `429` | `{"error":"rate_limited"}` | More than 6,000 server-side events a minute for this site, counted per collector location. Retry later. |
| `503` | `{"error":"temporarily_unavailable"}` | The key could not be checked. Retry later. |

> [!WARNING]
>
> Each accepted request is recorded as a separate payment. A retry after a timeout can record the same payment twice. Retry only on `429`, `503` or a network error, and keep your own record of which payments you have reported.

## How the payment is recorded

The payment is stored with provider `api` and kind `one_time`, or `refund` for a negative amount. The API does not record `subscription_new` or `subscription_renewal`: use a provider integration for those. See [payment types](https://docs.crispstats.com/revenue/refunds-and-renewals.md).

When the request carries `vid` and that visitor is still on your site, the payment is attached to their open session and appears in the [realtime](https://docs.crispstats.com/dashboard/realtime.md) feed. Either way, first-touch attribution then runs as described in [how revenue attribution works](https://docs.crispstats.com/revenue/overview.md#first-touch-attribution).

## Report a payment from the browser

The script can also report a payment from the page the buyer lands on after paying:

```js theme={"theme":{"light":"github-light","dark":"github-dark"}}
// After a payment succeeds, on the page the buyer lands on.
// The amount is in minor units: 4900 is 49.00.
crispstats.revenue(4900, "USD", { product: "Pro plan" });
```

The payment is tied to the current visit with no visitor id to pass, and is recorded with provider `api` like a server-side payment.

It needs no API key, but it has trade-offs. Prefer the server for anything you rely on:

* **It can be blocked.** Ad blockers that block the script block the payment too.
* **Anyone can send it.** The public key is in your page, so a visitor can call `crispstats.revenue()` with any amount.
* **Every call counts.** Reloading or revisiting the thank-you page records the payment again.
* **It cannot record a refund.** Refunds are accepted only from a secret key (the server-side event above) or a verified payment webhook. A negative amount sent from the browser with the public key is dropped like any other invalid browser input.
* **It is skipped when the browser is not tracked**, for example on an excluded path or with `localStorage.crispstats_ignore` set.
* **Errors are silent.** The collector answers `202` to browser events even when the payload is invalid.

To call `crispstats.revenue()` before the script has loaded, add the queue stub for `identify` and `revenue` from [custom events](https://docs.crispstats.com/track/custom-events.md).

## Next steps

> [card] [Pass the visitor id](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md)
>
> Get the id at checkout and store it with the order.

> [card] [Server-side events](https://docs.crispstats.com/track/server-side-events.md)
>
> The same endpoint for custom events and identify.

> [card] [API keys](https://docs.crispstats.com/account/api-keys.md)
>
> Create a key with the events scope.


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