Collector Beacon API Reference
HTTP specification for POST /v1/collect, payload validation rules, rate/quota responses, and server-side event ingestion.
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:
POST https://collect.trimstats.com/v1/collectContent-Type: application/json # or text/plain (used by navigator.sendBeacon)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.
/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)RequiredSite public key (pk_live_...).
ustring (1..2048 chars)RequiredFull 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: 0Active engagement duration in milliseconds (used by leave events).
scinteger (0..100)OptionalDefault: 0Scroll 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>OptionalOptional flat key-value properties object.
utm{ source?, medium?, campaign?, term?, content? }OptionalOptional 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.
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
| Code | Meaning |
|---|---|
202 Accepted | Beacon validated and queued for ClickHouse batch insertion (or silently ignored as a bot). |
400 Bad Request | Payload failed Zod schema validation ({ "error": "Invalid beacon payload" }). |
403 Forbidden | Page hostname is not listed in the site Allowed hostnames allowlist. |
404 Not Found | The s site public key does not exist. |
429 Too Many Requests | Organization monthly event quota plus 20% grace buffer exceeded. |
Common technical questions
Why does the browser script send JSON inside Content-Type: text/plain?
Can backend servers send events directly to POST /v1/collect?
Related guides
Complete client-side reference for window.trimstats commands, attribution snapshots, command queueing, and browser storage keys.
Confirm events are reaching ClickHouse and diagnose common setup issues like 403 domain blocks, ad blockers, or missing data attributes.
Compare Starter, Growth, and Scale limits, see which events count toward monthly quotas, and learn how the 20% spike grace buffer works.