Introduction
The Sparklytics API provides programmatic access to analytics data and event ingestion. The API is built with Rust (Axum 0.8) and serves both the analytics dashboard and public endpoints.Base URL
All API endpoints are relative to your Sparklytics installation:SPARKLYTICS_PUBLIC_URL environment variable (defaults to http://localhost:3000).
Authentication
The Sparklytics API uses two authentication methods depending on the endpoint:- Session cookies - Dashboard endpoints require JWT session cookies obtained via login
- API keys - Programmatic access to query endpoints (format:
spk_selfhosted_*for self-hosted)
Rate Limits
Event Collection Endpoint
ThePOST /api/collect endpoint enforces rate limiting per client IP address:
Rate limit applied per IP address on the event ingestion endpoint
429
Login Endpoint
ThePOST /api/auth/login endpoint enforces stricter rate limiting:
- 5 failed attempts per 15 minutes per IP address
- Returns
429 Too Many RequestswithRetry-Afterheader (900 seconds)
Request Format
Content Type
All POST/PUT requests must include:Body Size Limits
Maximum body size for
POST /api/collect: 100 KB (102,400 bytes)Maximum size for a single event’s
event_data field: 4 KB (4,096 bytes)Maximum events per batch request: 50 events
Example Request
Response Format
Success Responses
Most endpoints return JSON with adata wrapper:
Event Collection Response
ThePOST /api/collect endpoint returns a minimal acknowledgment:
202 Accepted
- Every 5 seconds (default flush interval)
- When 100 events accumulate (max buffer size)
Error Responses
Errors follow a consistent format:HTTP Status Codes
| Status | Code | Description |
|---|---|---|
| 400 | bad_request | Invalid request parameters |
| 400 | batch_too_large | Batch exceeds 50 events |
| 401 | unauthorized | Missing or invalid authentication |
| 404 | not_found | Resource not found (e.g., unknown website_id) |
| 410 | gone | Setup already complete (setup endpoint) |
| 413 | payload_too_large | Event data exceeds 4 KB limit |
| 429 | rate_limited | Rate limit exceeded |
| 500 | internal_server_error | Server error |
CORS Configuration
Configure allowed origins for cross-origin requests:IP Address Extraction
The API extracts client IP addresses for:- Rate limiting
- Geo-location enrichment
- Visitor ID computation
Trusted Proxy Support
Configure trusted proxy CIDRs:X-Forwarded-For header. Otherwise, it uses the direct socket IP address.
Data Enrichment
Events submitted toPOST /api/collect are automatically enriched with:
Visitor ID
Server-computed (default):- Salt rotates daily at midnight UTC (
salt_epoch = unix_timestamp / 86400) - Results in 16 hex characters
- Provide
visitor_idfield in the request (max 64 characters) - Stored in
localStorageassparklytics_visitor_idby SDK
Geographic Data
Extracted from client IP using MaxMind-compatible MMDB database:ISO country code (e.g.,
US, GB)State/region name (e.g.,
California)City name (e.g.,
San Francisco)NULL geo fields (non-fatal).
User Agent Parsing
Extracted via the woothee library:Browser name (e.g.,
Chrome, Firefox)Browser version (e.g.,
120.0.0)Operating system (e.g.,
Windows, macOS)OS version (e.g.,
10.15.7)Device category:
desktop, mobile, or tabletUTM Parameters
Extracted from URL query string as fallback:utm_sourceutm_mediumutm_campaignutm_termutm_content
Referrer Domain
Parsed from thereferrer URL field:
Storage Backend
Sparklytics uses different storage backends based on deployment mode:Self-Hosted Mode
DuckDB (embedded, single binary):- No separate database process
- Data stored in
./datadirectory (configurable viaSPARKLYTICS_DATA_DIR) - Memory limit: configurable via
SPARKLYTICS_DUCKDB_MEMORY(default:1GB) - Recommended for 4-32 GB VPS: set to
2GB-8GBfor better performance
Cloud Mode
ClickHouse for analytics data:- Horizontal scaling for high-volume workloads
- 10-68x faster at 100k events, 47-239x at 1M events (vs DuckDB)
- Storage: ~48 MB per 1M events (5.8x more efficient than DuckDB)
- Multi-tenancy support (
tenant_idcolumn) - User/website/API key management
Data Retention
Configure retention period:Health Check
Monitor service health:200 OK when the service is operational.