---
title: Automate with API tokens
description: Give CI, scripts and platforms an API token that holds only the scopes, project and environments they need, then rotate or revoke it.
related:
  - /concepts/keys-and-tokens
  - /develop/deploy-from-ci
  - /cli/tokens
---

A deploy from CI or a script that adds tenants has no person to log in. An API token lets the program act for you, with only the access you give it.

## Create a token

[`luna tokens create`](/cli/tokens) makes a token for one project, with the scopes you name. It prints the token once.

```bash
luna tokens create --name ci --scope deploy:write --project <ORG>/<PROJECT>
```

The token starts with `lt_`. Luna keeps only a hash of it, so store it where the program reads secrets, such as your CI's secret store.

In the Console, go to **API Keys** and select **New API key** to do the same.

## Use the token

The `luna` CLI reads the token from `LUNA_TOKEN`, and acts with the token instead of your login.

```bash
LUNA_TOKEN=<TOKEN> luna deploy --env staging
```

A request to the control API sends it as `Authorization: Bearer <TOKEN>`. [Deploy from CI](/develop/deploy-from-ci) shows a whole workflow.

## Choose its scopes

Repeat `--scope` for each thing the token may do. Where a scope ends in `:write` and there is a `:read` beside it, write includes read.

| Scope | Lets the token |
| --- | --- |
| `deploy:write` | Deploy code to an environment |
| `environments:write` | Create environments and manage their domains |
| `environments:delete` | Delete environments |
| `secrets:read` | List secrets by name. Their values are never read back |
| `secrets:write` | Set and remove secrets |
| `keys:read` | List an environment's publishable keys and secret tokens |
| `keys:write` | Create, rotate and revoke those keys |
| `data:read` | Export and browse lunas, and read a tenant's API |
| `data:write` | Import, restore, fork and edit lunas, and stop sandboxes |
| `tenants:write` | Create, deploy to, configure and delete added tenants |
| `logs:read` | Read logs |
| `traces:read` | Read traces |
| `metrics:read` | Read metrics |
| `telemetry:read` | Read logs, traces and metrics together |
| `usage:read` | Read usage and what it cost |
| `billing:read` | Read the plan and what is spent, for an organisation's token only |
| `audit:read` | Read the organisation's audit log |
| `tokens:write` | Create, list, rotate and revoke tokens within its own reach |
| `projects:write` | Create projects, for an organisation's token only |
| `projects:delete` | Delete projects, for an organisation's token only |

You can give a token only what your role lets you do. Owners and admins can give every scope. Developers can give all but `audit:read`, `billing:read` and `projects:delete`. Billing members can make an organisation's token that holds only `billing:read`, and viewers cannot make tokens. No token can manage members or change billing.

## Limit it to some environments

A token reaches every environment of its project unless you name some. Repeat `--env` for each one.

```bash
luna tokens create --name preview --scope deploy:write --project <ORG>/<PROJECT> --env staging --env preview
```

A token for an environment also reaches the tenants added to it.

## Reach a whole organisation

Pass `--org` instead of `--project` for a token that reaches every project of an organisation, including projects made later. Only such a token can hold `projects:write` and `projects:delete`.

```bash
luna tokens create --name setup --scope projects:write --scope deploy:write --org <ORG>
```

A person makes an organisation's token. A token cannot make one.

## Make it expire

`--expires-in` ends the token after a time, written as `90m`, `30d` or a number of seconds. Without it, the token works until you revoke it.

```bash
luna tokens create --name audit --scope logs:read --project <ORG>/<PROJECT> --expires-in 30d
```

A token with `tokens:write` can make more tokens. Each one holds at most its maker's scopes and reach, and expires no later than its maker.

## List the tokens

[`luna tokens list`](/cli/tokens) shows the tokens you look after, with their id, name, reach, scopes and last use. Owners and admins see every token of the project, and developers see the tokens they made.

```bash
luna tokens list
```

## Rotate a token

`luna tokens rotate` makes a new token with the same scopes and reach. The old one keeps working for an hour, so that you can move your programs over. Set `--overlap` to change that, or `0` to end it at once.

```bash
luna tokens rotate <ID> --overlap 2d
```

## Revoke a token

`luna tokens revoke` stops a token at once.

```bash
luna tokens revoke <ID>
```

The organisation's audit log records each token that was created, rotated and revoked, and who did it.

## Next steps

- [Keys and tokens](/concepts/keys-and-tokens) compares API tokens with app keys and account keys.
- [Deploy from CI](/develop/deploy-from-ci) uses a token in a GitHub Actions workflow.
- [Read the audit log](/organisation/read-the-audit-log) shows what each token did.
