# API keys and auth

One header on every request: `Authorization: Bearer <secret>`. The secret is an API key for your own code, or a short-lived token for someone else's.

## API keys

Make a key in the dashboard under **API keys**, or with the API (a key with `keys:manage`). A key looks like `k8c_live_…`, is shown once, and only its hash is stored. The SDKs and the CLI read `KOSA8_API_KEY`.

```
curl -s https://api.kosa8.com/v1/me -H "Authorization: Bearer $KOSA8_API_KEY"
```

## Scopes

A key holds one or more scopes. A broader scope includes the narrower one beside it, so you never need both.

| Scope | Lets the key |
|---|---|
| `sandboxes:read` | List and read sandboxes, files, logs and metrics |
| `sandboxes:write` | Everything in read, plus create, exec, fork, checkpoint, rewind, sleep and delete |
| `functions:invoke` | Call deployed functions |
| `functions:deploy` | Deploy and delete functions, and call them |
| `keys:manage` | Make, list and revoke keys |
| `audit:read` | Read the audit log |
| `admin` | Everything |

Each endpoint in the [API reference](https://docs.kosa8.com/api/) says which scope it needs. A key without it gets `403 forbidden`.

## Capability tokens, for your users

Building a product where your end users get a sandbox (a browser terminal, a notebook, a customer's agent)? Don't give them your key. Make a capability token: it starts with `k8c_tok_`, lives one hour by default (24 hours at most), holds a subset of `sandboxes:read`, `sandboxes:write` and `functions:invoke`, and can be held to some sandboxes or labels.

```
curl -s https://api.kosa8.com/v1/tokens \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"scopes":["sandboxes:write"],"sandbox_ids":["sbx_…"],"ttl_s":900}' | jq -r .token.secret
```

A token is never worth more than what made it: revoke the key and its tokens stop. A token cannot make keys or tokens, and every call made with one is in the audit log as `token:{id}`.

## The CLI's sign-in

`kosa8 login` signs the CLI in through the browser and stores the session in your home directory. For a machine with no browser, `kosa8 login --no-browser` prints a link and a code. `kosa8 cloud …` commands also work with just `KOSA8_API_KEY` set. Commands that run on this machine's own kosa8d need the browser sign-in, because an API key cannot hold a machine seat.

## Keeping keys safe

- Give each program its own key with the fewest scopes it needs, and an expiry if it's temporary.

- Never put a key inside a sandbox. Use [secrets](https://docs.kosa8.com/guides/cloud-secrets/), which are swapped in on the way out, or the [AI Gateway](https://docs.kosa8.com/guides/cloud-ai-gateway/) for model keys.

- If a key leaks, revoke it in the dashboard. It stops at once, and so do the tokens it made.

---
Source: https://docs.kosa8.com/concepts/auth/
