{
  "name": "app-api-keys",
  "title": "App API - Project API keys",
  "description": "Project API keys for scripts, cron jobs, devices and agents: gipity key create/list/revoke, X-Api-Key or Bearer, one key per app, scopes (functions, tables, files, IP allowlist), ctx.auth for key calls, per-key rate limits",
  "guid": "sk_plat_akey",
  "category": "App services",
  "requiredTools": [],
  "content": "# App API - Project API keys\n\nSessions are for people. A **script, cron job, or agent** authenticates with a **project API key** instead: a named, revocable secret the platform mints and hashes for you. Do **not** build an `api_keys` table, token minting, a masked preview, or a revoke endpoint in the app - it already exists.\n\n```bash\ngipity key create \"laptop-script\" --role editor   # prints the full key exactly once\ngipity key list                                   # names, prefixes, roles, scopes, last used (when + from which IP)\ngipity key revoke <ak_guid>\n```\n\nThe script then calls any `auth: user` / `auth: member` function with the key in a **header** - never in the body. `X-Api-Key: <key>` and `Authorization: Bearer <key>` are the same credential:\n\n```bash\ncurl -sX POST https://a.gipity.ai/api/<PROJECT_GUID>/fn/add-entry \\\n  -H \"X-Api-Key: $MY_KEY\" -H 'Content-Type: application/json' -d '{\"title\":\"Dune\"}'\n```\n\n**A key works only on the app (and environment) it was created for.** Using it against another app's URL is refused with 403, even another app of yours. Create one key per app or environment (`gipity key create ... --env dev` for the dev environment).\n\n## Scoped keys for devices\n\nA key with no scopes acts on the whole app at its role. A key that lives somewhere you don't fully control (a Raspberry Pi, a device at home, a CI runner) should get **scopes**, so a leaked key can only do the one job:\n\n```bash\ngipity key create \"front-door\" --role editor \\\n  --functions send-command,heartbeat \\\n  --tables commands:write,logs:read \\\n  --files read \\\n  --approvals read \\\n  --ip 203.0.113.7/32\n```\n\n- `--functions a,b` - only these functions. Each still runs with its own declared permissions.\n- `--tables t:read,u:write` - only these Records tables (`/records/<table>`, including the change stream). `write` includes read.\n- `--files read|write` - the app files routes (`/files`, `/uploads`). Without it a scoped key can't touch files.\n- `--approvals read` - read and consume [passkey approvals](app-approvals.md) (`/approvals/<id>`), so a device can check a person signed off before it acts.\n- `--ip addr[/prefix],...` - only from these IPv4/IPv6 addresses or ranges.\n\nAny of `--functions`, `--tables`, `--files` or `--approvals` makes the key resource-scoped: it may use exactly what is listed and nothing else (no other functions or tables, no app services, workflows, realtime, schema or table settings). `--ip` alone only pins where the key can be used from. Refusals are 403. Table settings (`PATCH /records/<table>/config`) need an owner-role key with no scopes.\n\n**A device without a fixed address** (a home connection, cellular) can't use `--ip`, so limit a leaked key in the other ways:\n\n- **Watch where it's used.** `gipity key list` shows when each key was last used and from which IP address. A device key showing up from an address you don't recognize is your cue to revoke it.\n- **Rotate with no downtime.** Create the new key, put it on the device, check `gipity key list` shows the new key in use, then `gipity key revoke` the old one. Both keys work during the overlap.\n- **Expire it.** `--expires-days <n>` makes the key stop working after n days, so a key you forgot about can't be used forever. Rotate before it expires.\n\nInside the handler, `ctx.auth` says who called:\n\n| `ctx.auth.via` | who | `ctx.auth.apiKeyName` |\n|---|---|---|\n| `'user'` | signed-in person (session or app token) | `null` |\n| `'api_key'` | script/agent holding a project API key | the key's name, e.g. `'laptop-script'` |\n| `'anonymous'` | no credentials (`auth: public` only) | `null` |\n\nAn `api_key` call gets a **complete `ctx.auth`**, identical in shape to a signed-in caller: `ctx.auth.userId`, `userGuid` and `displayName` are populated, resolving to the key's creator (which is the project owner). So a script writing with a key lands in the **same `ctx.auth.userGuid` scope** as the owner adding rows by hand - key your rows to `ctx.auth.userGuid` exactly as for a signed-in user and the two paths share one per-user log automatically. No need to probe empirically: `via`/`apiKeyName` and `role` (the key's, below) are the only fields that differ from a session call.\n\n`ctx.auth.role` is the caller's role on the app, the same one Records RBAC uses: `'owner'` for you, the member's role (`'editor'`/`'viewer'`) for a project member, the key's role for an API key (never above its creator's), and `null` for anyone else (signed-in non-members, app-scoped players, anonymous). Gate admin actions on it: `if (ctx.auth.role !== 'owner' && ctx.auth.role !== 'editor') throw new Error('Editors only');`.\n\nSo provenance (\"did I type this, or did my script add it?\") is one stored column: `ctx.auth.via === 'api_key' ? ctx.auth.apiKeyName : 'me'`. The name is a snapshot - it stays readable on old rows after the key is revoked. The key itself never reaches your code (the platform consumes and strips the header), so it can't leak into logs.\n\n## From a Python device\n\nFor a device or script in Python (a Raspberry Pi, a box behind NAT, a cron job), use the `gipity_device` client in the registry ([examples/python](https://github.com/GipityAI/registry/tree/main/examples/python)) instead of hand-rolled HTTP: `pip install` it from that folder, then `Client(app_guid, api_key)` gives you `fn.call`, `records.get/list/create`, `subscribe` (a Records change stream that resumes after drops and restarts and never misses an event), `upload` (any size, streamed), and `verify_approval` / `consume_approval` for passkey approvals. It sends the key as `X-Api-Key` and retries only what is safe to repeat.\n\n## Rate limits\n\nEach key has its own request budget on the app API, separate from other keys and from signed-in users, even from the same IP. See [app-development-reference](app-development-reference.md#rate-limits).\n"
}
