# Create an API key

> POST /v1/api-keys. Creates an API key for the caller in the caller's organization.

Source: https://legion.ambient.xyz/docs/api/identity/create-api-key

`POST https://api.legion.ambient.xyz/v1/api-keys`

Authenticate with an API key: `Authorization: Bearer $LEGION_API_KEY`.

Creates an API key for the caller in the caller's organization. The token is returned once and never again.

`scope` limits what the key may do. `full`, the default, is everything the caller's role
allows. `agent` is for AI agents: only the renter tools Legion's MCP server offers (operations marked
`x-legion-mcp` without `audience: provider`) and the MCP endpoint itself; it cannot create keys, invite
members, manage hosts, payouts or top-ups. `provider_tools` true (`agent` keys only, else 400
`invalid_request`) adds the provider tools: list and read the organization's hosts and earnings, set a
host's price and listing, rename and remove a host (with an owner's key: `hosts.manage`); such a key
still cannot create enrollment tokens, change a host's blocked networks, create keys, invite members,
payouts or top-ups. No `agent` key can call this operation (403 `api_key_scope`), so none can mint a
`full` key.

## Request body

- `name`, string, required
- `expires_at`, string (date-time)
- `scope`, ApiKeyScope: What an API key may do. `full`: every operation its creator's role allows (keys made before scopes existed are `full`, as is a key created without one). `agent`: only the operations marked `x-legion-mcp` without `audience: provider` (the renter tools) and the MCP endpoint (`mcpMessage`), plus the provider tools when the key has `provider_tools`. Every other operation answers 403 `api_key_scope`.
- `provider_tools`, boolean: Only with `scope` `agent` (else 400 `invalid_request`): the key may also use the MCP server's provider tools (`x-legion-mcp` `audience: provider`): read the organization's hosts and earnings, set a host's price and listing, rename and remove a host.

Example:

```json
{
  "name": "string",
  "expires_at": "2026-10-15T12:00:00Z",
  "scope": "full",
  "provider_tools": false
}
```

## Responses

### 201

The new key and its token.

- `api_key`, ApiKey, required
  - `id`, string (uuid), required
  - `name`, string, required
  - `prefix`, string, required: The first characters of the token, for recognizing it.
  - `created_at`, string (date-time), required
  - `expires_at`, string (date-time)
  - `last_used_at`, string (date-time)
  - `scope`, ApiKeyScope, required: What an API key may do. `full`: every operation its creator's role allows (keys made before scopes existed are `full`, as is a key created without one). `agent`: only the operations marked `x-legion-mcp` without `audience: provider` (the renter tools) and the MCP endpoint (`mcpMessage`), plus the provider tools when the key has `provider_tools`. Every other operation answers 403 `api_key_scope`.
  - `provider_tools`, boolean, required: Whether an `agent` key may also use the MCP server's provider tools. Always false for a `full` key, which is not limited to tools.
- `token`, string, required: The bearer token. Shown once.

Example:

```json
{
  "api_key": {
    "id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
    "name": "string",
    "prefix": "lgn_3Fq9",
    "created_at": "2026-10-15T12:00:00Z",
    "expires_at": "2026-10-15T12:00:00Z",
    "last_used_at": "2026-10-15T12:00:00Z",
    "scope": "full",
    "provider_tools": false
  },
  "token": "string"
}
```

### 400

The request is malformed or fails validation. An image reference that
is not a container image reference answers `invalid_image_reference`.

### 401

Missing, unknown, expired or revoked credentials.

### 403

The caller lacks the permission this operation requires (`forbidden`),
or signed in without having joined an organization yet
(`no_organization`; `getOnboarding` and the invitation and
organization operations under it work), or could not sign in
(`email_unverified`, `account_conflict`, and `domain_not_allowed`). `not_invited` is no longer returned to
session tokens.

`access_not_approved`: a signed-in person who is not approved
for the beta yet. Every operation that takes a credential answers it to
them, except `getMe` and `joinMyWaitlist`; the problem carries
`access` (`waitlisted` or `not_requested`, and the email). Approved
means a member of an organization, holding a pending invitation, or Legion; an API key always belongs to a member.

`oauth_token_mcp_only`: the caller sent an OAuth access token (`lgo_`), which is accepted
only by `mcpMessage` and the tool calls it dispatches. Every other operation answers it, whatever
the token's scope. Use an API key or the Legion web app.

`org_suspended`: the caller's organization is suspended. Every operation that takes a
credential answers it, by session or API key, except `getMe` (which shows `org.suspended_at` and
`org.suspension_reason`) and the support operations under `/v1/support/`.

`terms_not_accepted`: an approved caller who has not accepted
the current version of the Terms of Use and the Acceptable Use Policy. Every operation that takes a credential answers it, except `getMe`,
`acceptLegalDocuments`, `joinMyWaitlist` and the wind-down operations
(`listRentals`, `stopRental`, `deleteRental`, `listHosts`,
`updateListing` with `listed` false, `removeHost`); the problem
carries `legal`. An API key acts for the person who created it, so it
is refused until that person accepts. Terminal
operations also return
`session_required` (called with an API key) and `step_up_required`
(neither a sign-in nor a step-up challenge within the step-up window).

### 500

An unexpected server error. Safe to retry.

## Errors

- 403 [`no_organization`](https://legion.ambient.xyz/docs/errors#no_organization)

## Example request

```sh
curl -s -X POST "https://api.legion.ambient.xyz/v1/api-keys" \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "string",
  "expires_at": "2026-10-15T12:00:00Z",
  "scope": "full",
  "provider_tools": false
}'
```
