api-keys
What it does
Mints, validates, revokes, and lists API keys — the sk_… credentials that resolve to machine principals on every other endpoint. Also exposes per-key usage logs.
Route and methods
/.netlify/functions/api-keys, reachable as /api/api-keys (/api/* → /.netlify/functions/:splat). Dispatch is by ?action= plus method (?method= can override the HTTP method):
| Call | Action |
|---|---|
GET /api/api-keys | list |
GET /api/api-keys?id=<uuid> | get |
POST /api/api-keys | create |
POST /api/api-keys?action=validate | validate |
POST /api/api-keys?action=revoke | revoke |
GET /api/api-keys?action=usage-logs | listUsageLogs |
Unrecognized action values fall through to the method-based default branch, so GET/POST still dispatch; only methods other than GET, POST (or a matched action with the wrong method) reach Invalid action or method.
Authentication
Standard createHandler: x-api-key, internal secret, or Bearer <jwt>; anonymous → 401. list, create, and usage-logs additionally require ctx.accountId ("Account context required").
- System admins read via
adminDb(all accounts); other principals usectx.db— JWT-scoped (RLS) for humans, service role for machines. createis scope-gated for non-system callers (S-011): requestingis_internalor*:*throws 403, other requested scopes are clipped to the caller's own, and an omittedscopesinherits the caller's scopes.revoke/gethave no equivalent machine-scope check — a machine principal with anaccount_idrevokes within what its service-role client reaches; treat revocation as a trusted-surface operation.
Request contract
list (query): integration_id, key_type, is_active ('true'/ 'false'), expires_before, expires_after, limit (default 100), offset (default 0). Ordered created_at desc.
get (query): id.
create (body): name required. Optional: slug (defaults to a slugified name), key_type (default 'inbound'), permissions, scopes, rate_limit (default 100), expires_at, metadata, integration_id, app_id (resolved via resolveAppUuid, falling back to the x-app-id header's app), machine_type (default 'integration'), is_internal (default false). account_id and is_active: true are stamped from context; created_by is the principal id.
validate (body): key_value required; required_permissions optional array.
revoke (body): id required.
usage-logs (query): api_key_id, response_status, success, date_from, date_to, limit (default 100), offset (default 0).
Response contract
Standard { data, error, meta } envelope.
create→ the inserted row (id, name, key_type, rate_limit, expires_at, is_active, created_at) plusraw_key— the only time the secret is returned — andwarning: 'Store this key now. It will not be shown again.'Raw key format:sk_<32 hex chars>.validate→{ is_valid: true, api_key_id, integration_id, app_id, account_id, scopes, permissions }, or{ is_valid: false, error }(HTTP 200 in both cases;Insufficient permissionscarriesapi_key_id).list/get/revoke/usage-logs→ rows passed throughsanitizeRecordData+omitSecretFields, sokey_hash/key_value-type secret columns never leave the function. Onlykey_prefix(first 8 chars of the raw key) is stored in plaintext as the lookup index.
Side effects
createinserts anapi_keysrow storingkey_prefixand a PBKDF2-SHA256key_hash(the raw key is never stored).revokesetsis_active = false(keys are never hard-deleted here).emitLogwritesapi_key.created/api_key.revokedaudit entries.- Note:
api_key_usage_logsrows are written byresolvePrincipalon every authenticated request that uses a key, not by this endpoint.
Errors
| Condition | HTTP | Message |
|---|---|---|
Missing name / id / key_value | 500 | name is required / API key ID is required / key_value is required |
| No account context | 500 | Account context required |
is_internal/ *:* for non-system | 403 | Insufficient permissions to create an internal/wildcard-scoped API key |
| Unknown action/method | 500 | Invalid action or method |
| DB failures | 500 | PostgREST message |
validate failures are payload-level (is_valid: false), not HTTP errors.
Agent notes
validatechecks permissions as any-match: a key passes if at least one of itspermissionsentries appears inrequired_permissions. Wildcard*:*inscopesis honored at principal resolution, not here.- Key verification happens on prefix-matched candidates (
key_prefix= first 8 chars), then PBKDF2 verify —validatemirrors exactly whatresolvePrincipaldoes on a live request. - To call
validate/revoke/geta caller needs only to be authenticated (noctx.accountIdcheck on those paths);revokeon a machine principal runs with the service role — pin management calls to humans or*:*keys in practice. expires_atis nullable; expiry is enforced at resolution time (expires_at.is.null OR expires_at >= now).