# API keys

> Create a workspace API key for a script, a CI job or an integration, choose what it may do, rotate it and revoke it.

Source: https://docs.go-lumen.ai/api/api-keys

An API key lets a script, a CI job or another system use your workspace with no person signed in. Each key is confined to one workspace and to the scopes you give it, and every key expires.

-   5 minutes
-   Workspace admins

Only a workspace admin, signed in as a person, can create or revoke keys. A key can never create another key.

## Create a key

### In the console

1.  Open **Settings** and choose the **API keys** tab. Each key is a row: its status, its name, the start of the key, what it may do, who created it, when it was last used and when it expires.
2.  Click **Create key**.
3.  Under **Name**, say what will use the key, such as `CI deploy` (up to 64 characters).
4.  Under **Scope**, tick at least one of **Read**, **Run**, **Data**, **Build** or **Admin**. [Scopes](#scopes) explains each.
5.  Optional: open **Limit to** and pick the flows or tables the key may reach.
6.  Under **Expires**, choose **30 days**, **90 days** or **365 days**. The default is 90 days.
7.  Click **Create key**.
8.  Copy the key with **Copy the key** and store it where the job will read it, such as your CI's secret store. Then click **Done**.

![Settings with the API keys tab selected. The subtitle reads: Keys let scripts, the CLI and other systems start your flows and read their runs, without a person signing in. A switch labeled Allow API keys is on, next to a Create key button. A table lists six keys, each with a status icon, a name, the start of the key, scope icons, who created it, the creation date and the last use. The last key, Old laptop, has a struck-through status icon.](https://docs.go-lumen.ai/img/api/api-keys-01-list.png)

*Settings, then API keys. The icons next to a key's name are its scopes, and a filter icon marks a key limited to some flows or tables.*

![The Create an API key dialog. A Name field with the placeholder CI deploy. Five scope cards: Read, Run (ticked), Data, Build and Admin, each with a one-line description. A collapsed Limit to section. An Expires list set to 90 days. Cancel and Create key buttons.](https://docs.go-lumen.ai/img/api/api-keys-02-create.png)

*Pick the smallest set of scopes the job needs. Run is ticked by default.*

![The Your new API key dialog. A warning reads: Copy this key now. You won't be able to see it again. The full key is shown in a field with a copy button, and below it two example commands that use it with curl and with lumen run.](https://docs.go-lumen.ai/img/api/api-keys-03-new-key.png)

*The only time the key is shown. Lumen keeps a hash of it and cannot show it again.*

> **Shown once:**
> 
> Lumen stores only a SHA-256 hash of the key. If you lose it, revoke it and create a new one.

A workspace can have at most 10 active keys at a time.

### Through the API

An admin signed in as a person can also create keys over HTTP. `expiresInDays` is required and is `30`, `90` or `365`.

```bash
curl -X POST "$LUMEN_API/apis/runtime.lumen.ai/v1alpha1/namespaces/tenant-acme/apikeys" \
  -H "Authorization: Bearer $PERSON_TOKEN" -H "Content-Type: application/json" \
  -d '{"name": "GitHub Actions deploy", "scopes": ["run"], "flows": ["invoices"], "expiresInDays": 90}'
```

The answer holds the key once, in `key`. `GET .../apikeys` lists the workspace's keys without their secrets.

## Scopes

A key has one or more scopes, and each request uses the broadest one that allows it.

| Scope | What the key may do | Limit it to |
| --- | --- | --- |
| **Read** | Read flows, steps, tables, connections (never their secrets), forms, models and other configuration. Read runs, their steps and files. Read table rows. | Flows, tables |
| **Run** | Start a flow's manual run and read the runs this key started. Nothing else. | Flows |
| **Data** | Read, add, change and delete table rows, and read files. | Tables |
| **Build** | Create, change and delete flows, steps, tables, connections (never their credentials) and similar configuration. For CI, Terraform and the CLI. | Never limited |
| **Admin** | Everything the other four do, plus rerunning and cancelling runs, testing steps and connections, agent memory and similar. | Never limited |

A key that only starts runs should have **Run** and nothing else. A job that applies flows from a repository needs **Build**.

No key, whatever its scopes, can reach: API keys, members and roles, single sign-on, plans, budgets and credits, a connection's credentials, secrets, approvals (a person's decision), the audit log, or anything outside its own workspace. Those answer `403`. A key is never a person, so it never signs in as one.

### Limit a key

By default a key reaches the whole workspace. **Limit to** narrows it:

-   **Flows** limits what **Run** and **Read** reach, to the flows you name.
-   **Tables** limits what **Data** and **Read** reach, to the tables you name.

You can name up to 50 of each, and they must exist when you create the key. **Build** and **Admin** are never limited, and the console tells you so. A key limited to some flows cannot start, list or read any other flow or its runs, and a run key reads only the runs it started.

## Use a key

Send the key as a bearer token. There is no separate setting for keys anywhere.

**CLI:**

```bash
export LUMEN_TOKEN=lmn_eu_...
lumen run invoices -f march.pdf --wait
lumen apply -f flows/
```

The CLI also reads `--token`. It asks the server which workspace the key belongs to, so you do not pass `-w`. See [Install the CLI](https://docs.go-lumen.ai/api/install-the-cli.md).

**HTTP:**

```bash
curl "$LUMEN_API/apis/runtime.lumen.ai/v1alpha1/namespaces/tenant-acme/runs/$RUN" \
  -H "Authorization: Bearer $LUMEN_TOKEN"
```

See the [Runtime API](https://docs.go-lumen.ai/api/runtime-api.md) for starting runs and downloading results.

**Terraform:**

```hcl
provider "lumen" {
  token     = var.lumen_api_key
  namespace = "tenant-acme"
}
```

See the [Terraform provider](https://docs.go-lumen.ai/api/terraform-provider.md).

In the [audit log](https://docs.go-lumen.ai/operate/audit-log.md) a key's actions show as a machine, `apikey:` followed by the key's id, and a run a key started records which key did it.

## Rotate a key

Keys expire, so plan to replace them. Rotation has no downtime if you do it in this order:

1.  Create a new key with the same scopes and limits.
2.  Put it where the old one was, and deploy.
3.  Check that the old key's **Last used** stops moving.
4.  Revoke the old key.

## Revoke a key

1.  In **Settings**, **API keys**, find the key and click its revoke icon (**Revoke the key**, with the name).
2.  Type the key's name to confirm, then click **Revoke**.

Scripts that use the key stop working. Revoking takes effect at once on the server that handled it and within 15 seconds everywhere, and it cannot be undone. A key you revoked shows as **Revoked**; one past its date shows as **Expired**.

Keys follow their creator: when the admin who made a key is removed from the workspace, or is no longer an admin, the keys they made are revoked at once.

If a key leaks, revoke it first and ask questions after.

## Turn API keys off

The switch **Allow API keys** at the top of the tab is on by default. Turn it off, for example in a workspace that requires single sign-on, and no key of the workspace works and none can be created. The keys are kept and work again when you turn the switch back on. Allow up to 15 seconds for the change to reach every server. The change is recorded in the audit log.

## What a key looks like

```text
lmn_<region>_<id>_<secret>
```

A key starts with `lmn_`, then the region it works in (for example `eu`), a public 12-character id that names the key in the list and in the audit log, and the secret with a checksum. The checksum lets a scanner or Lumen reject a typo without a lookup.

To find leaked keys in a repository, scan for this pattern, for example as a [gitleaks](https://github.com/gitleaks/gitleaks) rule with the keyword `lmn_`:

```text
\blmn_[a-z0-9-]{2,20}_[0-9A-Za-z]{12}_[0-9A-Za-z]{49}\b
```

A key works only in its workspace's region. Sent to another region's API it is answered `421 Misdirected Request`, with the right address.

Lumen refuses, with `401`, a token that looks like it carries a key but is not exactly one: a key in quotes, after other text, or cut short.

## Limits

-   10 active keys per workspace.
-   30, 90 or 365 days of life, 365 at most.
-   After repeated failed attempts from one address in a minute, that address is answered `429` for the rest of the minute.

## Next steps

[Runtime APIStart runs and read results with a key.](https://docs.go-lumen.ai/api/runtime-api.md)[Flows as YAMLApply flows from CI with a Build key.](https://docs.go-lumen.ai/api/flows-as-yaml.md)[Workspaces and accessWho can create keys and what roles can do.](https://docs.go-lumen.ai/concepts/workspaces-and-access.md)[Audit logSee what a key did.](https://docs.go-lumen.ai/operate/audit-log.md)
