6 min read

Public Stats REST API & API Keys

Authenticate with workspace or single-site Bearer API keys to query overview KPIs, live visitors, revenue, funnels, cohorts, UI friction, insights, and AI traffic from the collector.

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

How do I query TrimStats analytics programmatically with API keys?

Create a read-only ts_live_ API key under Workspace -> API keys (workspace-wide or locked to one site) and send it in Authorization: Bearer to https://collect.trimstats.com/v1/sites and per-site endpoints for overview, live visitors, revenue, funnels, cohorts, UI friction, insights, and AI traffic.

Authentication & key scoping

Create read-only API keys (ts_live_...) under Workspace -> API keys (owners and admins only). Each key can access all sites in your workspace or be locked to a single site (following least-privilege scoping so an agency client dashboard or MCP bot only sees one domain). Only the SHA-256 hash is stored in Postgres; plaintext keys are displayed once on creation.

Send your key in the Authorization: Bearer header on every request to https://collect.trimstats.com/v1/.... Never place secrets in URL query parameters (?api_key=, ?token=, ?key=): URLs leak into reverse-proxy access logs, browser histories, and Referer headers, so TrimStats explicitly rejects query-string keys with 400 Bad Request.

Workspace / API keys
Scope each read-only key to a single domain or all workspace sites, set optional expiration windows, and revoke keys with instant collector cache invalidation.
List sites and fetch 7-day overviewbash
1# 1. List sites accessible to this API key
2curl -sS "https://collect.trimstats.com/v1/sites" \
3 -H "Authorization: Bearer ts_live_your_api_key"
4
5# 2. Query 7-day overview KPIs, timeseries, breakdowns, and goals
6curl -sS "https://collect.trimstats.com/v1/sites/<site_id>/overview?from=2026-11-24&to=2026-11-30" \
7 -H "Authorization: Bearer ts_live_your_api_key"
Server-side only (no wildcard CORS)

Stats API endpoints intentionally omit permissive CORS headers and return Cache-Control: private, no-store. Call them from your backend, scheduled jobs, BI pipelines, or MCP tools so your ts_live_ key is never exposed in client-side browser bundles.

Available v1 endpoints

Method & PathKey Query ParametersReturns
GET /v1/sitesNoneSites accessible to the key (id, name, domain, trackingMode, frictionEnabled, timezone).
GET /v1/sites/:siteId/overviewfrom, to, compare, country, device, browser, os, pathname, referrer, utmSource, utmMedium, utmCampaign, utmTerm, utmContentKPIs, prior-period comparison, live count, timeseries, breakdowns, custom events, and goals.
GET /v1/sites/:siteId/liveNoneReal-time visitor count active in the last 5 minutes (liveVisitors).
GET /v1/sites/:siteId/events/propertieseventName (required), from, to, plus overview segment filtersTop property keys and values for a custom event.
GET /v1/sites/:siteId/revenuefrom, to, attribution (first_touch or last_touch)Attributed revenue totals, daily series, and referrer/UTM/landing-page/country breakdowns.
GET /v1/sites/:siteId/insightsincludeDetails (true or false)Active rolling 7-day anomalies and optional daily trends, drivers, and 30-day history.
GET /v1/sites/:siteId/frictionfrom, toRage clicks, dead clicks, 90% scroll reach, and top friction pages and CSS selectors.
GET /v1/sites/:siteId/funnelsNoneSaved funnel definitions (id, name, steps).
GET /v1/sites/:siteId/funnels/:funnelIdfrom, to, windowSecondsStep-by-step windowFunnel() conversion and drop-off report for a saved funnel.
POST /v1/sites/:siteId/funnels/evaluateJSON body: { from, to, windowSeconds, steps }Ad-hoc 2 to 8 step funnel evaluation.
GET /v1/sites/:siteId/cohortsweeks (2 to 12, default 8)Weekly retention cohort matrix for persistent visitors.
GET /v1/sites/:siteId/ai-crawlersfrom, to, category (ai_answers, indexing, training), crawler, pathnameAI crawler hit totals, breakdown, pages, and daily series.
GET /v1/sites/:siteId/ai-referralsfrom, to, provider, pathnameHuman visits, revenue, goals, and #:~:text= cited passages from AI assistants.

Rate limits & response headers

Requests are rate-limited atomically in Valkey at 60 requests/minute per API key and 120 requests/minute per workspace. Every authenticated response includes standard rate-limit headers:

HeaderMeaning
X-RateLimit-LimitMaximum requests permitted in the current 60-second window.
X-RateLimit-RemainingRemaining requests available before throttling.
X-RateLimit-ResetSeconds until the current rate-limit window resets.
Retry-AfterIncluded on 429 Too Many Requests responses indicating seconds to wait.
  • 401 Unauthorized: Missing, malformed, expired, or revoked Authorization: Bearer token.
  • 403 Forbidden: Workspace does not have an active subscription.
  • 404 Not Found: Site or funnel does not exist or is outside the API key scope.
  • 429 Too Many Requests: Key/workspace rate limit or repeated invalid auth threshold exceeded.

Common technical questions

What rate limits apply to the TrimStats Stats REST API?
TrimStats enforces 60 requests per minute per API key and 120 requests per minute per workspace in Valkey, returning X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, and Retry-After headers on 429 responses.
Can I restrict a TrimStats API key to a single website?
Yes. When creating an API key in Workspace -> API keys, leave the scope set to All sites in workspace or lock it to one site. Requests to any other site ID return 404 Not Found.

Related guides