Scripting and automation
Machine-readable output, confirmations without a terminal, exit codes, environment variables, precedence and profiles for the legion CLI.
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. |
legion rental list -o json | jq '.[].name'
legion rental list -o 'jsonpath=$[?(@.state=="running")].name'
legion rental get quiet-river-7k2f -o jsonpath=$.ssh.hostResults 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_, 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:
legion rental create --offer amber-falcon --catalog pytorch --yes -o jsonRun 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) and Hint (the next command to try) on standard error.
Environment variables
| Variable | What it does |
|---|---|
LEGION_ | The API key. It has no flag, so it stays out of the process list. |
LEGION_ | The profile to use. |
LEGION_ | The API address. It must be https, except for localhost. |
LEGION_ | The default output format. |
LEGION_ | Where legion keeps its files. |
NO_ | Turns colour off. |
Precedence
A setting comes from the first of these that has a value:
The command-line flag.
The environment variable.
The selected profile.
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.
legion profile create work -o wide
legion auth login -p work
legion profile use work
legion offer list -p defaultSettings live in config., and keys in credentials., which only you can read. Both are in LEGION_, or else your user config folder, such as ~/. 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.