# Sign-up and keys for agents

A coding agent gets a key from its owner with a code that the owner confirms in the browser. The agent never sees a password, a browser or the owner's session. The service address is https://app.thaloz.ai.

## The code-confirmation flow

This is the one sign-up flow an agent may use.

1. The agent starts a claim. It names itself and the preset it asks for.
2. The service returns a link to the `/device` page, https://app.thaloz.ai/device, and an 8-character code.
3. The agent shows its owner the link and the code.
4. The owner opens `/device` and signs in with GitHub. A new owner enters an invite code, which makes their account.
5. The owner enters the code. The page names the agent and what its key may do. The owner chooses **Approve** or **Refuse**.
6. On **Approve**, the agent takes a key with the preset it asked for. The key works for 30 days and shows on the owner's API keys page, named after the agent: `build bot` makes `build-bot`. The owner can revoke it there.

A code works for 15 minutes and once. Only a person signed in on the dashboard can approve a code, so an agent cannot approve its own claim.

## Presets an agent can ask for

| Preset | What the key may do |
|---|---|
| `machines` | Everything `read-only` may do, and create, run commands in, open a shell in, copy files to and from, stop, resume, extend and delete machines. This is the default. |
| `read-only` | List and read machines, limits, usage and the wallet. |

No key can create or revoke keys, or change the plan or the wallet. Those need the owner.

## With thz

```sh
thz login --agent --name "build bot" --no-wait --json
thz login --agent --json
thz whoami --json
```

The first command prints `verificationUrl`, `userCode`, `expiresInSeconds` and `intervalSeconds`, and keeps the claim in `~/.config/thz/pending-agent-login` (`%APPDATA%\thz\pending-agent-login` on Windows). Pass `--preset read-only` to ask for a read-only key. The second command carries on with the kept claim, waits for the owner, and saves the key in `~/.config/thz/credentials` (`%APPDATA%\thz\credentials` on Windows), readable by you only. It prints the account, never the key. Without `--no-wait`, the first command also waits.

| Exit code | Error code | What it means |
|---|---|---|
| 0 | | The key is saved. |
| 2 | | A new claim needs `--name`. |
| 3 | `claim_refused` | The owner chose **Refuse**. No key was made. |
| 3 | `claim_expired` | The code ran out or was used. Start a new claim. |

## Over HTTP

The same flow without `thz`. Neither call takes a key.

1. Start the claim:

   ```sh
   curl -sS -X POST https://app.thaloz.ai/v1/device/code -d '{"agentName":"build bot","preset":"machines"}'
   ```

   The reply is `201` with `deviceCode`, `userCode`, `verificationUrl`, `intervalSeconds` and `expiresInSeconds: 900`. Keep `deviceCode` to yourself. Show the owner `verificationUrl` and `userCode`. `preset` is optional and defaults to `machines`. An empty body starts a person's `thz login` instead, which is not for agents.
2. Every `intervalSeconds`, poll for the key:

   ```sh
   curl -sS -X POST https://app.thaloz.ai/v1/device/token -d '{"deviceCode":"..."}'
   ```

   - `202` with `state: "pending"`: nobody has decided yet. Poll again.
   - `200` with `state: "approved"` and `secret`: the key, which starts `thz_`. It is sent once. Keep it secret.
   - `403` with `claim_refused`: the owner refused.
   - `403` with `claim_expired`: the code ran out, was used, or never existed.
3. Send the key as `Authorization: Bearer thz_...` on every `/v1` call. `GET /v1/account` returns `login`, `state` and `keyPreset`.

Errors are `{"error":{"code","message","retryable","retryAfterSeconds"}}`. See [Errors](/docs/errors.md).

## Without the flow

The owner can create a key on the dashboard's API keys page and give it to the agent. The agent sets `THZ_API_KEY` to it. `THZ_API_KEY` wins over a key that `thz login --agent` saved.
