# Authentication

> Create an API key, send it as a bearer token, and know what a key can and can't do.

Source: https://legion.ambient.xyz/docs/api/authentication

The Legion API uses API keys. A key starts with `lgn_`.

## Create a key

1. Sign in at [legion.ambient.xyz](https://legion.ambient.xyz).
2. Open **Settings**, then **API keys**.
3. Give the key a **Name** you'll recognise, such as `training-scripts`.
4. Choose when it **Expires**: never, or in 30 days, 90 days or 1 year.
5. Choose its **Access**: **Full access**, or **Agent** for a key you give an AI agent (see below).
6. Press **Create key**.

The key is shown once. Copy it and store it somewhere safe, such as a secrets manager. If you lose it, revoke it and create a new one.

## Use a key with the CLI

The [legion command line](https://legion.ambient.xyz/docs/cli) uses the same keys. Run `legion auth login`: it signs you in through the browser and saves a key with **Full access** for you. On a remote machine, create a key here and use `legion auth login --paste`.

## Send it

Send the key as a bearer token in the `Authorization` header:

```sh
curl -s "https://api.legion.ambient.xyz/v1/me" \
  -H "Authorization: Bearer $LEGION_API_KEY"
```

Keep the key out of your code and your shell history: read it from an environment variable, as here.

A request without a valid key gets `401 unauthorized`.

## What a key can do

A key acts as you, with your permissions in your organization. If you're a member, it can rent and read billing. If you're an owner, it can also add hosts, set prices and add credit. See [Organizations and roles](https://legion.ambient.xyz/docs/account/organizations-and-roles).

A key's access (`scope` on the key) narrows that further:

- **Full access** (`full`, the default, and every key made before access existed): everything your permissions allow.
- **Agent** (`agent`): only the operations Legion's MCP server offers AI agents as tools, and the MCP server itself. It can't create keys, invite people, top up or change payouts, and it can't manage hosts unless it was created with **Can manage hosts** (`provider_tools`), which adds the provider tools to the MCP server and their API operations. Any other operation answers `403 api_key_scope`. See [Safety with AI agents](https://legion.ambient.xyz/docs/agents/safety).

`getMe` shows the key you are using: `credential.scope` and `credential.provider_tools`. Its `permissions` are yours, not the key's, so an Agent key still lists permissions it can't use.

Each person sees and revokes only their own keys.

## What needs the app

A few things need you signed in to the app, and answer `403 session_required` to a key:

- accepting the Terms;
- support requests;
- the web terminal.

## When the Terms change

When Legion publishes a new version of the Terms, your keys stop working for most requests until you accept it in the app. They answer `403 terms_not_accepted`, and the problem lists the documents to accept. Sign in to the app and accept them; your keys work again at once.

Until then, you can still read your rentals, and stop or delete them.

## Revoke a key

In **Settings**, **API keys**, press **Revoke** next to the key. Anything using it stops working immediately.
