Skip to content

Authentication

All endpoints (except /api/health and /api/docs) require a credential.

Three credential shapes

The Authorization header accepts any of three token shapes, dispatched on the prefix:

Prefix Kind Minted by
am_k1_ API key (operator) appmecca api-key create (an administrator operation), or the environment's root key
am_u1_ Personal API key mecca login --with-api-key in a terminal (or mecca login --keep), through POST /api/auth/keys from a session whose CLI sign-in was approved for a key
ses_ Session token mecca login (also what the dashboard's session cookie translates to)
am_k1_a3b7c9d2e5f8g1h4j6k8m0n3p5q7r9s2t4
am_u1_0123456789abcdef0123456789abcdef0123456789abcdef
ses_KZ1q8vN2p...

A session token carries the signed-in user's identity, so it also resolves group membership and roles; an API key resolves to whichever group (if any) it is bound to. A personal key is its owner: it acts through the owner's group memberships only, never with platform-admin authority (whoami reports is_platform_admin: false for it), and it can do what its owner can do in those groups, including the actions that need a person rather than an operator key (the typed confirmation those routes ask for still applies). It cannot mint another credential, sign anyone in to a CLI or suite app, or change the account (profile, sessions, linked accounts): those answer 403, never 401.

Personal API keys — /api/auth/keys

Method Path Caller What
POST /api/auth/keys a session whose CLI sign-in was approved for a personal key only (403 for any key, a disabled account, a read_only account, and every other session: a browser session, a machine-token session, a plain mecca login) Mint. Body {"name"?: string (1-64 printable; must equal the approvedkey_name, which it defaults to; 422 otherwise), "expires_in"?: seconds (at most 3650 days), "replaces"?: the 13-character prefix of one of your keys (am_u1_+ 7 hex; 422 otherwise)}. 201 with the raw api_key once, plus key_prefix, name, created_at, expires_at and replaced_prefix (the key replaces revoked, or null). 409 when an active key of yours already has the name and replaces does not retire it: "you already have an active key named '<name>' (<prefix>); pass replaces to replace it". Only the SHA-256 is stored.
GET /api/auth/keys session or personal key Your own keys: key_prefix, name, created_at, last_used_at, last_used_ip, expires_at, revoked_at, is_current.
DELETE /api/auth/keys/{key_prefix} a session (any of its owner's keys) or a personal key (only itself, else 403) Revoke. 404 with one body for another person's prefix and an unknown one.

Only a sign-in approved for a key can mint one. POST /api/auth/keys finds the caller's CLI sign-in request by its session (the row /cli/token stamps) and mints only when that request's purpose is personal_key, and only a key named its key_name (POST /api/auth/cli/authorize, Sign-in). Otherwise it answers 403 a personal API key can only be created by a sign-in that was approved for one — run `mecca login --with-api-key` in a terminal (or `mecca login --keep`). A second key from one approval is the 409 above, unless replaces retires the first.

replaces is one transaction. The server revokes the named key and inserts the new one together, so the new key may take the same name, and a failure leaves the old key working and no new key. replaces naming another person's key, or no key, answers 404 with the same body as DELETE /api/auth/keys/{key_prefix}; naming one of your own keys that is already revoked mints with replaced_prefix: null. Both audit rows are written: api_key_revoke (detail {name, replaced_by}) and api_key_create (detail {name, expires_at, replaces}).

A personal key never expires on its own unless expires_in was given. It ends when it is revoked (here, or on the dashboard's "Profile & security" page, under "Personal API keys"), when it expires, when the account is locked or disabled (both revoke every personal key in the same transaction as the sessions; an unlock or an enable restores nothing), or when the person is deleted.

The dashboard's "Personal API keys" card lists the keys (revoked ones stay listed) with no control to create one: a key is created only by a sign-in approved for a key. Its caption reads "Keys end when you revoke them here, when they expire, or if your account is locked or disabled. After that, each machine needs mecca login --keep again." Each row shows the name and prefix, then "created … · last used … from · expires · revoked UTC" as they apply ("never used" when it has not been). A live row has a "Revoke…" button, and a live row also carries a "This key" badge when the API marks it as the key the request was made with (is_current; shown only when the list is read with that key, which the dashboard does not do); a revoked row has a "Revoked" badge instead. The confirm dialog is titled "Revoke the key ?" with the text "The machine holding it stops working at once, and needs mecca login --keep to sign in again." and the button "Revoke key"; on success a "Key revoked" toast appears.

Three 401 details are specific to personal keys:

detail When
API key revoked — run `mecca login --keep` again the key was revoked (by the person, a lock or a disable), and the account is neither locked nor disabled
API key expired — run `mecca login --keep` again the key's expires_at has passed
Account disabled — contact your platform operator the owner is disabled (checked before the key's own state; like Account locked — contact your platform operator, deliberately not "log in again")

whoami for a personal key returns source: "user_key", the owner's user and memberships, and api_key: {prefix, name}.

Sending the credential

Include it in the Authorization header:

# With Bearer prefix (recommended)
curl -H "Authorization: Bearer am_k1_abc123..." https://api-url/api/apps

# Without Bearer prefix (also accepted)
curl -H "Authorization: am_k1_abc123..." https://api-url/api/apps

# A session token works the same way
curl -H "Authorization: Bearer ses_abc123..." https://api-url/api/apps

Selecting a group

Most app-scoped routes resolve which group you are acting as, in this order:

  1. ?group=<slug> query parameter
  2. X-AppMecca-Group: <slug> header
  3. The group an API key is bound to
  4. Your personal group

A request for an app you cannot reach returns 404, never 403 — the API never confirms that another tenant's app exists.

Scope Hierarchy

Each key has a scope that determines what it can access:

Scope Level Can Do
admin 3 Everything — create keys, manage infrastructure, all operations
deploy 2 Trigger builds, deploy apps, manage services, set env vars
read 1 Read-only — list apps, view builds, check status

Scopes are hierarchical: an admin key can do everything a deploy key can, and a deploy key can do everything a read key can.

Error Responses

401 Unauthorized

Returned when:

  • No Authorization header is present
  • The key doesn't have the am_k1_ prefix
  • The key hash doesn't match any active record in the database
  • The key has been revoked (revoked_at IS NOT NULL)
{
    "detail": "Invalid or revoked API key"
}

403 Forbidden

Returned when the key is valid but the scope is insufficient:

{
    "detail": "Scope 'deploy' required, you have 'read'"
}

Key Storage

Keys are stored as SHA-256 hashes in the api_keys table; session tokens are hashed into the sessions table. The raw credential is never stored — only the prefix (for identification) and the hash (for validation).

The last_used_at timestamp is updated on every successful authentication.

Root Key Seeding

Each environment's root API key lives in the platform secret store. On first deployment it can be seeded into the api_keys table automatically.

WebSocket Tickets

A browser cannot attach an Authorization header (or read an HttpOnly session cookie) when opening a WebSocket. To authenticate a socket without exposing a credential to browser JavaScript, the API issues short-lived, single-use tickets. Every ticket is stored with a 60-second TTL and consumed atomically on first use, so a captured ticket is dead the moment it is redeemed.

There are two ticket flavours, for two different sockets.

POST /api/auth/deploy-ws-ticket — tenant deploy + preview events

Mints a ticket for your group's live deploy feed. Any authenticated member of the selected group may call it; the group is taken from the membership-checked resolution path (?group=<slug> 404s a non-member) and baked into the ticket at mint time, so a tenant can never subscribe to another group's channel.

{
  "ticket": "<url-safe token>",
  "ws_url": "wss://api.<env>.appmecca.net/api/ws/deploys",
  "expires_in": 60
}
Status Cause
400 No deliberate group selected — the channel is per-group, so pass ?group=<slug> for a group you belong to
401 Anonymous
503 Live updates unavailable; clients fall back to polling

Connect with GET /api/ws/deploys?ticket=<ticket>. The handler re-validates the channel, group and environment bound into the ticket before subscribing.

POST /api/auth/ws-ticket — platform notifications (operators)

Mints a ticket for the platform-wide notifications socket. Platform-operator gated — an ordinary tenant gets 403 before any ticket is minted, mirroring GET /api/notifications.

{
  "ticket": "<url-safe token>",
  "ws_url": "wss://api.<env>.appmecca.net/api/ws/notifications",
  "expires_in": 60
}
  • ws_url is empty when the API has no domain configured (local dev); the client then derives the URL from PUBLIC_API_URL.
  • Returns 503 when the live-update backend is unavailable — clients fall back to REST polling.

GET /api/ws/notifications

Accepts two credentials:

  • ?ticket=<ticket> — consumed atomically, so a captured ticket is dead after first use and expires after 60 s regardless. The bound payload must carry scope == "admin" and match the API's environment, or the socket closes with code 4403.
  • ?api_key=<admin key> — the admin API-key path, used by the monitor SPA and admin tooling.

Missing both → close 4401. Invalid/expired/replayed ticket or non-admin → close 4403.

On connect the server sends {"event": "ready"}, then {"event": "ping"} every 30 seconds as keepalive. The protocol is server → client only.