Skip to content

Scripting and automation

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

On this page

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.

Output formats
FormatWhat you get
tableThe default: a readable table.
wideThe table with more columns, such as host ids and hardware.
jsonThe full response as JSON.
yamlThe same as YAML.
nameOne name per line, for loops and pipes.
jsonpath=<expr>The part of the JSON that the expression selects.
Shell
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:

Shell
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

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

Environment variables
VariableWhat it does
LEGION_API_KEYThe API key. It has no flag, so it stays out of the process list.
LEGION_PROFILEThe profile to use.
LEGION_API_URLThe API address. It must be https, except for localhost.
LEGION_OUTPUTThe default output format.
LEGION_CONFIG_DIRWhere legion keeps its files.
NO_COLORTurns 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.

Shell
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.