
# Scripts and agents

Run `tdx` from CI jobs, scripts and coding agents: find its commands, read its output, and recover from errors without repeating a change.

You need: `tdx` connected to your gateway, as in the [CLI quickstart](/cli/quickstart), and an admin key whose preset covers the commands you run.

## Discover commands

```bash
tdx schema --json                    # every command, flag, output field and exit code
tdx api-keys create --help --json    # one command
```

A command's `--help --json` lists its flags, the rules between them, its output fields and its `operation`, which says whether it reads or writes and which gateway route it calls:

```json
"operation": {
  "id": "api-keys.create",
  "effect": "write",
  "target": "http",
  "availability": "implemented",
  "http": { "kind": "fixed", "routes": ["POST /control/v1/inference-keys"] }
}
```

Point an agent at `tdx schema --json` rather than a copied list of commands, so what it reads always matches the installed version.

## Read the output

A command that succeeds prints one JSON object to stdout and exits with 0. `data` holds the result:

```bash
tdx auth status --json=principal
```

```json
{
  "schemaVersion": "tokedex.cli.v1",
  "command": "auth status",
  "data": {
    "principal": { "kind": "management_key", "id": "01M83N4WA8WZ46MMMFY2HNK818", "name": "cli-quickstart" }
  }
}
```

- **`schemaVersion`** names the output format. Check it before you read `data`, so a script stops rather than misreads a different format.
- **`--json=<fields>`** keeps only the named fields of `data`, as `--json=principal` does above.
- **`--jq '<filter>'`** runs your installed `jq` on the whole object, and needs `--json`: `tdx workspaces list --json --jq '.data.workspaces[].slug'`.
- **`tdx audit stream`** prints one object per line as events arrive. Pipe it to `jq -c`; it refuses `--jq`.

## Handle errors

A command that fails prints an error to stderr and exits with a code from 1 to 7. Branch on `code` and the exit code; `hint` and `fix` are written for a person.

```json
{"error":{"code":"FORBIDDEN","hint":"The gateway denied this request.","fix":"Use an authorized credential. Session-only and step-up requirements cannot be bypassed by this CLI."}}
```

| Exit code | Meaning | What to do |
|---|---|---|
| 0 | Success | Read stdout |
| 1 | An internal error, or a change the gateway applied before a later step failed | Read stdout and the error code before you run anything again |
| 2 | Usage | Fix the command against its `--help --json` |
| 3 | Authentication | Check `TOKEDEX_TOKEN`, `TOKEDEX_HOST` and the profile. A token from browser sign-in expires, so a person signs in again with `tdx auth login` |
| 4 | Forbidden | Use an admin key whose preset covers the command |
| 5 | Not found | Check the ID |
| 6 | Conflict | Read the current state, then decide whether to try again |
| 7 | Unavailable: the gateway, the network or `jq`, or a task that needs the dashboard (`SESSION_REQUIRED`) | Retry a read after a pause. Before you retry a write, read the state, since a timeout leaves the outcome unknown |

## Preview a write

Add `--dry-run` to a command that changes something, to see the request without sending it:

```bash
tdx api-keys create --name ci-deploy --all-models --dry-run
```

```json
{
  "schemaVersion": "tokedex.cli.v1",
  "command": "api-keys create",
  "data": {
    "preview": {
      "applied": false,
      "authoritative": false,
      "basis": "unavailable",
      "request": {
        "action": "create-inference-key",
        "name": "ci-deploy",
        "allowedModels": null,
        "secretless": false,
        "profile": "default",
        "origin": "http://127.0.0.1:4000"
      }
    }
  }
}
```

`"applied": false` confirms nothing changed. Have an agent show this preview before it runs a write, so a person approves the exact request. The preview checks neither permissions nor current state, and a command that reads still calls the gateway, so check that `operation.effect` in its `--help --json` is not `read` before you rely on one.

## Check before you retry a write

A write can succeed on the gateway and still exit with 1. Here the change applies, then the `--jq` filter fails:

```bash
tdx api-keys disable --id 01M83N53FRJSNFAN03N49RBKSZ --json --jq '.data.key | error("unexpected shape")'
```

stdout has the unfiltered result, with the key's `"status": "disabled"`, and stderr has the error:

```json
{"error":{"code":"CHANGE_APPLIED_JQ_FAILED","hint":"The command completed and its changes were applied, but jq output failed.","fix":"The unfiltered safe JSON outcome is on stdout. Inspect it before repeating any write; correct the filter separately."}}
```

The key is disabled anyway. `CHANGE_APPLIED_RESPONSE_INVALID`, `KEY_CREATED_DELIVERY_FAILED` and `KEY_ROTATED_DELIVERY_FAILED` also mean the change was made. After exit code 1, or a timed-out write, read the current state, such as `tdx api-keys list`, before you run the write again: repeating `api-keys create` makes a second key. A filter that `jq` cannot compile is refused with exit code 2 before anything is sent.

If browser sign-in exits 7 with `SIGN_IN_DELIVERY_UNCONFIRMED`, the gateway may have issued a personal access token. Revoke any token issued by that attempt under **Account → Access tokens** before signing in again.

## Choose how to pass credentials

| Source | Use it for | How it behaves |
|---|---|---|
| `TOKEDEX_TOKEN` holding an admin key, with `--host` or `TOKEDEX_HOST` | CI jobs and agents that run on their own | Read for one run and never written to disk |
| A profile saved by browser sign-in, `tdx auth login` | A person at a terminal | A personal access token that acts as you for 30 days, kept in a file only your user can read under `~/.config/tokedex/`, and sent only to the address it was saved with |
| A profile saved by `tdx auth login --method token` | A machine you run commands on yourself, with a key you minted | The token you supply, kept like the one above; `tdx auth logout` deletes the profile but leaves the key valid |

`TOKEDEX_TOKEN` wins when both are present. In CI, pass the admin key from the job's secret store as `TOKEDEX_TOKEN`, so no copy outlives the job, and give each script its own key, so revoking one stops nothing else. Choose a profile with `--profile` or `TOKEDEX_PROFILE`; used with another address, it fails with `PROFILE_HOST_MISMATCH` and the key is not sent.

Keep browser sign-in for yourself. Its token can do everything your role can, and it stops when you sign out everywhere or change your password, which breaks any job that depends on it. A coding agent you run in your own terminal uses your profile, so it acts as you; to give it less, pass it an admin key with a narrow preset through `TOKEDEX_TOKEN`.

## Issue an API key from a script

`tdx api-keys create` never prints a new key. It writes the key to a new file that only your user can read:

```bash
tdx api-keys create --name ci-deploy --all-models --token-file ./ci-deploy.key
```

```json
{
  "schemaVersion": "tokedex.cli.v1",
  "command": "api-keys create",
  "data": {
    "key": {
      "id": "01M83N53FRJSNFAN03N49RBKSZ",
      "name": "ci-deploy",
      "status": "active",
      "createdAt": "2026-11-23T07:33:00.561Z",
      "expiresAt": null
    },
    "credential": { "delivered": true, "destination": "token-file" },
    "preview": null
  }
}
```

The file must not exist yet, so a run never overwrites a key. `tdx api-keys rotate --token-file` delivers a rotated key the same way, and `--secretless` with `--owner` offers someone a personal key that they pick up in the dashboard. `tdx api` masks secrets in every reply. Creating keys needs the `key-manager` preset, or `workspace-key-manager` in one workspace.

Next: the [Command reference](/cli/reference), [API keys](/gateway/api-keys) or [Errors, limits and request IDs](/gateway/errors-and-limits) in the gateway docs.
