API keys & API access
Create and revoke API keys, how authentication works, the scope allow-list, rate limiting and what the API exposes.
Overview#
API keys give you programmatic access to metricsKit's `/api/v1` endpoints outside the console. A key is created in the console; the plaintext is returned only once at creation and can never be viewed again.
- List: `GET /api/v1/api-keys` (scope `admin:api-keys`)
- Create: `POST /api/v1/api-keys`
- Revoke: `DELETE /api/v1/api-keys/:id`
Create and revoke#
The create body is `{ name, scopes?, expires_at?, rate_limit? }`, where `name` is required.
- `scopes` is validated again on the backend and only accepts the 8 allow-listed scopes (next section).
- The plaintext key is returned only in the create response; the database stores only its SHA-256 hash.
- The generated plaintext is `mk_live_` + 32 hex characters; the validation regex also accepts `mk_(live|test)_[a-f0-9]{32}`.
- `expires_at` is optional (no value means it never expires); `rate_limit` defaults to 600 (but the current rate-limit path does not read this field).
- Revocation is a soft delete: `status` is set to `revoked` and the record is kept.
Authentication#
Requests authenticate with the `Authorization` header, accepting either an API key or a Clerk session JWT; a signed-in browser can also use the Clerk session cookie `__client`.
Authorization: Bearer mk_live_<32 hex>The API-key path first runs a regex check, then looks up `api_keys` by SHA-256, requires `status='active'` and an unexpired key, then reads `scopes`, `rate_limit` and the user plan; `last_used_at` is written at most once every 5 minutes.
Scope allow-list#
Only these 8 scopes can be selected at creation:
| Scope | Purpose |
|---|---|
| read:overview | Most read endpoints: sites, overview, reports, sharing, and more |
| read:realtime | Can be created, but no route currently uses it |
| read:keywords | Keyword endpoints: /reports/keywords-tracker, /sites/:siteId/reports/keywords |
| read:notes | Read timeline notes |
| read:ai | AI Q&A |
| write:notes | Write timeline notes |
| admin:api-keys | Manage API keys themselves |
| admin:billing | Start a billing checkout |
Rate limiting#
Rate limits are designed in three tiers per plan, but the current auth pipeline enforces a flat 600 requests/minute for both API and user:
| Plan | Designed (planPolicy) | Enforced |
|---|---|---|
| free | 60/min | 600/min |
| pro | 600/min | 600/min |
| business | 3000/min | 600/min |
Related docs#
- MCP server: /mcp
- Plans & feature gating: /plans
- Data sources overview: /data-sources