# App API - Project API keys

Sessions 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.

```bash
gipity key create "laptop-script" --role editor   # prints the full key exactly once
gipity key list                                   # names, prefixes, roles, scopes, last used (when + from which IP)
gipity key revoke <ak_guid>
```

The 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:

```bash
curl -sX POST https://a.gipity.ai/api/<PROJECT_GUID>/fn/add-entry \
  -H "X-Api-Key: $MY_KEY" -H 'Content-Type: application/json' -d '{"title":"Dune"}'
```

**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).

## Scoped keys for devices

A 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:

```bash
gipity key create "front-door" --role editor \
  --functions send-command,heartbeat \
  --tables commands:write,logs:read \
  --files read \
  --approvals read \
  --ip 203.0.113.7/32
```

- `--functions a,b` - only these functions. Each still runs with its own declared permissions.
- `--tables t:read,u:write` - only these Records tables (`/records/<table>`, including the change stream). `write` includes read.
- `--files read|write` - the app files routes (`/files`, `/uploads`). Without it a scoped key can't touch files.
- `--approvals read` - read and consume [passkey approvals](app-approvals.md) (`/approvals/<id>`), so a device can check a person signed off before it acts.
- `--ip addr[/prefix],...` - only from these IPv4/IPv6 addresses or ranges.

Any 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.

**A device without a fixed address** (a home connection, cellular) can't use `--ip`, so limit a leaked key in the other ways:

- **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.
- **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.
- **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.

Inside the handler, `ctx.auth` says who called:

| `ctx.auth.via` | who | `ctx.auth.apiKeyName` |
|---|---|---|
| `'user'` | signed-in person (session or app token) | `null` |
| `'api_key'` | script/agent holding a project API key | the key's name, e.g. `'laptop-script'` |
| `'anonymous'` | no credentials (`auth: public` only) | `null` |

An `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.

`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');`.

So 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.

## From a Python device

For 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.

## Rate limits

Each 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).

