API keys are used to authenticate API requests. They are different from session cookies used in the dashboard.
- Self-hosted: Keys use prefix
spk_selfhosted_ - Cloud: Keys use prefix
spk_live_
List API keys
Query Parameters
Number of keys to return (1-100)
Number of keys to skip (for pagination)
Response (200 OK)
Array of API key objects
Response Example
Create an API key
Request Body
Display name for the key (must not be empty)Best practices:
- Use descriptive names (e.g., “Production API”, “CI/CD Pipeline”)
- Include environment or service name
- Avoid generic names like “Key 1”
Response (201 Created)
Response Example
Error Responses
400 Bad Request
Delete (revoke) an API key
Path Parameters
API key ID (format:
key_*)Response (204 No Content)
Empty response body on successful revocation.Error Responses
404 Not Found
Using API keys
Authentication Header
Include the API key in theAuthorization header with the Bearer scheme:
Security Best Practices
1. Store keys securely
1. Store keys securely
- Never commit keys to version control (add to
.gitignore,.env.exampleshould use placeholders) - Use environment variables or secret management tools (e.g., AWS Secrets Manager, HashiCorp Vault)
- Rotate keys periodically
2. Use minimal permissions
2. Use minimal permissions
- Create separate keys for different environments (dev, staging, production)
- Revoke unused keys immediately
3. Monitor key usage
3. Monitor key usage
- Check
last_used_attimestamps regularly - Revoke keys with suspicious activity or long inactivity
4. Handle key exposure
4. Handle key exposure
If a key is exposed (e.g., committed to a public repo):
- Revoke the key immediately via the dashboard or DELETE endpoint
- Create a new key
- Update your application with the new key
- Rotate any other potentially exposed secrets
Key Format
Self-Hosted Mode
spk_selfhosted_abc123def456789012345678901234
- Total length: 47 characters
- Prefix stored: First 20 chars (
spk_selfhosted_abc12) - Hash stored: SHA-256 of full key (64 hex chars)
Cloud Mode
spk_live_xyz789abc123def456789012345678
- Total length: 41 characters
- Prefix stored: First 20 chars (
spk_live_xyz789abc123) - Hash stored: SHA-256 of full key (64 hex chars)
Authentication Flow
When you make an API request with an API key:- Header parsing: Sparklytics extracts the key from
Authorization: Bearer <key> - Hashing: The key is hashed using SHA-256
- Database lookup: The hash is looked up in the
local_api_keystable (self-hosted) orapi_keystable (cloud) - Validation: The key must:
- Exist in the database
- Not be revoked (
revoked_atmust benull)
- Last-used tracking: On successful authentication,
last_used_atis updated asynchronously (fire-and-forget)
Error Responses
401 Unauthorized
Returned when:
- No
Authorizationheader provided - Key is invalid (not found in database)
- Key has been revoked
FAQ
Can I use API keys to access the dashboard?
Can I use API keys to access the dashboard?
No. The dashboard requires cookie-based session authentication. API keys are only for programmatic API access (e.g., creating websites, fetching analytics via custom scripts).
What happens if I delete a key?
What happens if I delete a key?
The key is marked as revoked (
revoked_at timestamp is set). All future requests using that key will immediately fail with 401 Unauthorized. The key cannot be restored — you must create a new one.Can I retrieve a key after creation?
Can I retrieve a key after creation?
No. The full key is returned only once in the creation response. Only the SHA-256 hash is stored in the database. If you lose the key, you must revoke it and create a new one.
How many API keys can I create?
How many API keys can I create?
There is no hard limit, but best practice is to create one key per service/environment and revoke unused keys.
What's the difference between spk_selfhosted_ and spk_live_?
What's the difference between spk_selfhosted_ and spk_live_?
spk_selfhosted_*keys are generated in self-hosted mode (single-tenant, DuckDB)spk_live_*keys are generated in cloud mode (multi-tenant, PostgreSQL + ClickHouse)