# Scripting and automation

> Machine-readable output, confirmations without a terminal, exit codes, environment variables, precedence and profiles for the legion CLI.

Source: https://legion.ambient.xyz/docs/cli/scripting

After this page you can drive `legion` from a shell script, a CI job or an AI agent, and know what each exit code means.

## Output formats

Choose a format with `-o` (or `--output`) on any command.

| Format | What you get |
| --- | --- |
| `table` | The default: a readable table. |
| `wide` | The table with more columns, such as host ids and hardware. |
| `json` | The full response as JSON. |
| `yaml` | The same as YAML. |
| `name` | One name per line, for loops and pipes. |
| `jsonpath=<expr>` | The part of the JSON that the expression selects. |

```sh
legion rental list -o json | jq '.[].name'
legion rental list -o 'jsonpath=$[?(@.state=="running")].name'
legion rental get quiet-river-7k2f -o jsonpath=$.ssh.host
```

Results go to standard output. Progress, prompts and hints about what to do next go to standard error, and progress only shows on a terminal, so `json` output stays clean to pipe. Add `-q` (`--quiet`) to print results only. Colour turns off with `--no-color`, `NO_COLOR`, or when output isn't a terminal. Money is in US dollars.

## Confirmations

Commands that spend money or destroy data ask you to confirm. With no terminal and no `--yes` they refuse and exit with code 2 instead of guessing. Pass `-y` (`--yes`) to answer yes.

To create a rental in a script, give `--offer`, an image (`--image` or `--catalog`) and `--yes`:

```sh
legion rental create --offer amber-falcon --catalog pytorch --yes -o json
```

Run it with `--dry-run` first to see the preview without renting. `legion rental delete <rental> --yes` destroys the workspace, so use it with care.

## Exit codes

| Code | Meaning |
| --- | --- |
| 0 | Success. |
| 1 | Error: the API or network failed, your organization is suspended, or a feature is turned off for it. |
| 2 | Usage error: a bad flag or argument, a request the API rejected as invalid, or nothing to confirm with and no `--yes`. |
| 3 | Authentication failed: not logged in, or the key is invalid, expired or lacks scope. |
| 4 | Not found: a rental, offer, key or backup. |
| 5 | Conflict or price changed: the host's price rose above `--max-price`, or the GPUs were just taken. |
| 6 | Insufficient credit. Check with `legion billing balance`. |
| 130 | Interrupted with Ctrl-C. |

A failure prints `Error`, `Code` (the API's stable [error code](https://legion.ambient.xyz/docs/errors)) and `Hint` (the next command to try) on standard error.

## Environment variables

| Variable | What it does |
| --- | --- |
| `LEGION_API_KEY` | The API key. It has no flag, so it stays out of the process list. |
| `LEGION_PROFILE` | The profile to use. |
| `LEGION_API_URL` | The API address. It must be https, except for localhost. |
| `LEGION_OUTPUT` | The default output format. |
| `LEGION_CONFIG_DIR` | Where legion keeps its files. |
| `NO_COLOR` | Turns colour off. |

## Precedence

A setting comes from the first of these that has a value:

1. The command-line flag.
2. The environment variable.
3. The selected profile.
4. The built-in default.

Run `legion auth status` to see what's in effect, and `legion help environment` for the full table.

## Profiles

A profile bundles an API address, an API key and a default output format, so you can switch between accounts. Without one, legion uses the profile named `default`.

```sh
legion profile create work -o wide
legion auth login -p work
legion profile use work
legion offer list -p default
```

Settings live in `config.yaml`, and keys in `credentials.yaml`, which only you can read. Both are in `LEGION_CONFIG_DIR`, or else your user config folder, such as `~/.config/legion`. Change a setting with `legion config set`.

## AI agents

An agent can drive the CLI with `-o json --yes`. For agents that use tools instead of a shell, see [Use Legion with AI agents](https://legion.ambient.xyz/docs/agents).
