4 min read

Collector Beacon API Reference

HTTP specification for POST /v1/collect, payload validation rules, rate/quota responses, and server-side event ingestion.

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

How does the TrimStats POST /v1/collect HTTP ingestion API work?

TrimStats ingests browser and server beacons at POST https://collect.trimstats.com/v1/collect accepting text/plain or application/json payloads containing site key (s), URL (u), event type (t), optional name (n), referrer (r), and flat properties (p). Valid beacons return HTTP 202 Accepted immediately.

Endpoint & content types

Both the browser tracker (t.js) and server-side integrations submit events to the Fastify collector endpoint:

HTTP
POST https://collect.trimstats.com/v1/collect
Content-Type: application/json # or text/plain (used by navigator.sendBeacon)
Why t.js sends text/plain (skipping CORS preflight)

In browsers, sending cross-origin application/json triggers a hidden extra OPTIONS preflight round trip before the real POST. Sending JSON inside text/plain via navigator.sendBeacon qualifies as a CORS simple request, cutting network latency in half while surviving tab closes. The collector parses JSON bodies for both application/json and text/plain.

Site analytics / Overview
Accepted /v1/collect beacons increment live Valkey counts immediately and batch-insert into ClickHouse for Overview KPIs and breakdowns.

Request body schema

sstring (6..64 chars)Required

Site public key (pk_live_...).

ustring (1..2048 chars)Required

Full page URL (for example https://acme.com/pricing?utm_source=blog) or pathname.

t"pageview" | "leave" | "custom" | "rage_click" | "dead_click" | "scroll" | "outbound" | "download"OptionalDefault: "pageview"

Event type discriminator.

nstring (max 120 chars)OptionalDefault: ""

Event name (custom event name, outbound hostname, or download pathname).

rstring (max 2048 chars)OptionalDefault: ""

Referrer URL (document.referrer). Self-referrals are automatically stripped.

vidstring (max 64 chars)OptionalDefault: ""

Optional first-party client visitor ID (honored only when First-party ID mode is enabled on the site).

uidstring (max 128 chars)OptionalDefault: ""

Optional external user ID from identify().

dinteger (0..86,400,000)OptionalDefault: 0

Active engagement duration in milliseconds (used by leave events).

scinteger (0..100)OptionalDefault: 0

Scroll depth percentage (used by scroll events).

elstring (max 256 chars)OptionalDefault: ""

Target CSS selector hint (used by rage_click and dead_click).

pRecord<string, string | number | boolean | null>Optional

Optional flat key-value properties object.

utm{ source?, medium?, campaign?, term?, content? }Optional

Optional UTM overrides (each max 128 chars). Automatically extracted from u if omitted.

Server-side event example

When posting events from a backend worker, always send a realistic non-bot User-Agent header (default HTTP clients like curl/ or python-requests are filtered as bots) and pass the user uid so the event links cleanly to their timeline.

server-track.tsts
1await fetch('https://collect.trimstats.com/v1/collect', {
2 method: 'POST',
3 headers: {
4 'content-type': 'application/json',
5 'user-agent': 'TrimStatsServer/1.0 (Node.js)',
6 },
7 body: JSON.stringify({
8 s: 'pk_live_your_key',
9 t: 'custom',
10 n: 'api_key_created',
11 u: 'https://acme.com/settings/api',
12 uid: 'user_12345',
13 p: { scope: 'read_only' },
14 }),
15});

HTTP response codes

CodeMeaning
202 AcceptedBeacon validated and queued for ClickHouse batch insertion (or silently ignored as a bot).
400 Bad RequestPayload failed Zod schema validation ({ "error": "Invalid beacon payload" }).
403 ForbiddenPage hostname is not listed in the site Allowed hostnames allowlist.
404 Not FoundThe s site public key does not exist.
429 Too Many RequestsOrganization monthly event quota plus 20% grace buffer exceeded.

Common technical questions

Why does the browser script send JSON inside Content-Type: text/plain?
Using navigator.sendBeacon with a text/plain Blob makes the cross-origin POST a CORS simple request, eliminating an extra OPTIONS preflight network round trip on every pageview and exit beacon.
Can backend servers send events directly to POST /v1/collect?
Yes. Server-side jobs can POST application/json beacons with s (site key), u (URL or pathname), t ("event"), n (event name), and optional uid or vid fields directly to /v1/collect.

Related guides