# Authentication for agents and CI

The CLI and the GraphQL API authenticate with an API key (`rbk_...`). A key
belongs to one organization, acts as the user who created it (same roles,
same permissions), and is shown in full only once.

## Get a key from a terminal: `spaceboy login`

```bash
spaceboy login --json
```

Prints one JSON object per line:

```json
{"event":"verification","verificationUrl":"https://spaceboy.ai/cli/login?code=XXXX-XXXX","userCode":"XXXX-XXXX","expiresAt":"2026-11-25T15:10:00Z"}
{"event":"logged_in","email":"person@example.com","accountId":"acct_...","accountName":"My Studio"}
```

A person opens `verificationUrl`, signs in or signs up, and approves; the
CLI then receives a new API key and writes it to `~/.spaceboy/config.json`
(mode 600). No account is needed beforehand. An agent relays the URL and
code to its person and waits; the command exits when the request is
approved, denied (exit 1) or expired (exit 1).

Spaceboy has no way to create an account or a key without a person
approving in a browser once.

## Use an existing key: CI, containers, sandboxes

```bash
export SPACEBOY_API_KEY=rbk_...
spaceboy whoami --json
```

| Variable | Meaning |
| --- | --- |
| `SPACEBOY_API_KEY` | The key. Overrides the config file, so CI needs no `login`. |
| `SPACEBOY_ACCOUNT` | Organization id (`acct_...`) for requests; the user must be an active member. Defaults to the key's organization. |
| `SPACEBOY_API_URL` | GraphQL endpoint. Default `https://api.spaceboy.ai/graphql`. |
| `SPACEBOY_CONFIG_PATH` | Alternative config file. |

The older `REPOBOT_API_KEY`, `REPOBOT_ACCOUNT`, `REPOBOT_API_URL` and
`REPOBOT_CONFIG_PATH` names still work. The CLI reads
`~/.repobot/config.json` when `~/.spaceboy/config.json` does not exist.

`spaceboy login --token rbk_...` stores an existing key in the config file
and checks it.

Keys are created and revoked on the console's API keys screen
(https://spaceboy.ai/console/keys) by organization owners and admins. The CLI has no
command to list or revoke keys.

## Calling the API directly

```bash
curl -s https://api.spaceboy.ai/graphql \
  -H "authorization: Bearer $SPACEBOY_API_KEY" \
  -H "content-type: application/json" \
  -d '{"query":"{ currentUser { id email currentAccount { id name planCode } } }"}'
```

Add `x-repobot-account: acct_...` to act in another organization you belong
to.

## Exit codes

| Code | Meaning |
| --- | --- |
| 0 | Success |
| 1 | Failure (the message says why) |
| 2 | Usage error (bad flags) |
| 3 | Not logged in, or the key is invalid or revoked: run `spaceboy login` |
| 4 | The role or plan does not allow it: tell your person; `error.action` often names the fix |

Under `--json`, failures print `{"error":{"message","exitCode","code"?,"action"?}}`
on stdout.

## Roles

Organization owners and admins can do everything the CLI offers. Members
cannot manage secrets, read logs, query databases or adopt code; those
commands exit with code 4 for them.
