# API keys

Built in modules/identity. Agents can request an initial key with an email address; authenticated keys and web sessions can mint additional keys up to their own scopes.

## Scopes

Give each key the narrowest scopes it needs. Leaving scopes out when creating a key requests all scopes available to the caller. A key can never mint a new key with a scope it does not hold itself.

API key scopes

| Scope | Allows | Extra rule |
| --- | --- | --- |
| documents | Answer questions, manage profiles, generate and read documents | None |
| sites:write | Create sites, verify domains, edit and publish banner configs, run verify | None |
| evidence:read | Read consent events and build evidence packs | Requires a verified account (403 account_unverified otherwise) |
| keys | List, create and revoke keys on the account | Cannot keep pre-claim keys (session only) |

## POST /v1/keys (without Authorization: agent bootstrap)

Request a key for an email address with no Authorization header. A new email and an existing email get an answer with the same fields.

```bash
curl -X POST https://api.policygrain.com/v1/keys \
  -H "Content-Type: application/json" \
  -d '{"email": "owner@example.in", "scopes": ["documents", "sites:write"]}'
```

- **New email**: Creates an unverified account and returns key (shown once), key_id and scopes. A verification email with no secret in it is sent when an email sender is set up.
- **Existing email**: Returns key: null, key_id: null and scopes: null, and asks WorkOS to email the owner a six-digit Magic Auth sign-in code (or a sign-in link when Magic Auth is not configured).
- **email_queued and fix (both answers)**: email_queued is true only when an email was actually handed to a sender; fix is then null. When it is false, fix tells the agent to have the address's owner sign in at policygrain.com/sign-in, which verifies the account. Until an email vendor is added, a new email's answer has email_queued: false.

## POST /v1/keys (with Authorization: mint another key)

With a valid Bearer token (a session or a key holding the keys scope), omit email and pass optional scopes. Sending email together with an Authorization header returns 400 email_with_key. Returns HTTP 201 with the one-time key string and the key metadata.

```json
{
  "key": "<pgk_ key, shown once>",
  "id": "0193a8f2-6d0b-7c11-8e4f-1a2b3c4d5e6f",
  "prefix": "<pgk_ and 16 hex characters>",
  "scopes": ["documents"],
  "created_at": "2026-11-19T05:00:00.000Z",
  "revoked_at": null,
  "made_before_claim": false,
  "kept_at": null,
  "suspended": false
}
```

## List, keep and revoke keys

Key management endpoints

| Endpoint | Auth required | Returns |
| --- | --- | --- |
| GET /v1/keys | Bearer key (scope keys) or session | 200 { keys: [...] }; never returns secret values, only id and prefix. |
| POST /v1/keys/{id}/keep | Signed-in web session only (API keys get 403 keep_needs_person) | 200 key object with kept_at set and suspended: false. |
| DELETE /v1/keys/{id} | Bearer key (scope keys) or session | 200 key object with revoked_at set. Revoked keys fail immediately with 401 key_revoked. |
| GET /v1/account | Any valid API key or session | 200 { id, email, email_verified_at, claimed, keys_waiting, auth }. |

Account deletion (DELETE /v1/account, session only) is specified as gap G17 and will be documented here once implemented across modules.
