# API keys and scopes

> Give each flockvm API key only what it needs: presets and actions, environments and VMs, IP allowlists, expiry, rotation with grace, the key policy.

- URL: https://flockvm.com/docs/api/api-keys
- Section: API
- Last updated: 2026-11-07

An API key lets a script, a CI job or an agent call the [flockvm API](https://flockvm.com/docs/api/overview) and the [MCP server](https://flockvm.com/docs/mcp). Each key has a **scope**: the actions it may take, the environments and VMs it reaches, and the IP addresses it may be used from. Give each job a key with only what it needs: a dashboard reader gets **Read only**, a CI pipeline **Create and use VMs**, and no key a power it never uses.

A key belongs to one organization and to the user who created it. What it can do at each request is **its actions and that user's role at that moment**, both: a member's full-access key still cannot change environments or reveal their values, because a member cannot. See [Members and roles](https://flockvm.com/docs/team/members#permissions).

## Create a key

1. **Open API keys**

   Go to **Settings → API keys**, [flockvm.com/dashboard/settings/api-keys](https://flockvm.com/dashboard/settings/api-keys), in the organization the key is for.

2. **Name it, describe it, pick an expiry**

   A **name** that says what uses it (`ci-deploy`, `grafana`, `agent-batch`), up to 60 characters, and an optional description, up to 200. The expiry: 1 day, 7 days, 30 days, 90 days (the default), 1 year, never, or a date up to a year ahead.

3. **Pick what the key can do**

   One of the four presets, or **Custom** to tick actions one by one. The form starts on **Create and use VMs**, the usual choice for CI, SDKs and agents.

4. **Restrict it, if you want**

   To some environments or VMs, and to some IP addresses. Leave them empty and the key reaches every VM of the organization, from anywhere.

5. **Create and copy it**

   Click **Create key**. The secret is shown once: copy it into a password manager or your CI's secrets.

![Settings → API keys, with a key just created](https://flockvm.com/docs/screenshots/api-key-created.webp "The secret is shown once: flockvm keeps only a hash of it.")

The scope, the restrictions and the IP list never change once the key exists: to change them, create a new key, swap it in, and revoke the old one. Only the name and the description can be edited.

A key with `api_key.write` can create keys over the API too: see [Manage keys over the API](#manage-keys-over-the-api).

## Presets

| Preset                               | `preset`    | Actions                                                                                                                                                      |
| ------------------------------------ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **Read only**                        | `read-only` | `account.read` `vm.read` `snapshot.read` `environment.read` `ssh_key.read` `ci_cache.read`                                                                   |
| **Use existing VMs**                 | `operate`   | `account.read` `vm.read` `vm.logs` `vm.power` `snapshot.read` `snapshot.write` `environment.read` `ssh_key.read` `ci_cache.read`                             |
| **Create and use VMs** (recommended) | `ci`        | `account.read` `vm.read` `vm.logs` `vm.create` `vm.power` `vm.delete-own` `snapshot.read` `snapshot.write` `environment.read` `ssh_key.read` `ci_cache.read` |
| **Full access**                      | `full`      | `*`                                                                                                                                                          |

- **Read only** lists and inspects VMs, environments, SSH keys and CI caches. It runs nothing and creates nothing: right for a dashboard or a monitoring job.
- **Use existing VMs** adds starting, stopping, pausing and resuming VMs, reading their console logs and taking snapshots. It cannot create a VM, nor delete one.
- **Create and use VMs** adds creating VMs and deleting the VMs this key created. Another key's or the dashboard's VMs stay out of its reach for deletion.
- **Full access** is `*`: every action, including the ones flockvm adds later. Keep it for the scripts that really manage the organization.
- **Custom** is any set of the actions below.

## Actions

| Action               | What it allows                                                                                                             | Routes                                                                                                                                                                                                                                            |
| -------------------- | -------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `account.read`       | **Read the account and catalog.** Who the key acts as (GET /me), and the regions, sizes and images on offer.               | `GET /me`, `GET /regions`, `GET /sizes`, `GET /images`                                                                                                                                                                                            |
| `vm.read`            | **List and inspect VMs.** List VMs and read their details. Not their console logs.                                         | `GET /vms`, `GET /vms/{id}`                                                                                                                                                                                                                       |
| `vm.logs`            | **Read VM logs.** Read a VM's console log, which can hold secrets a program printed.                                       | `GET /vms/{id}/logs`                                                                                                                                                                                                                              |
| `vm.create`          | **Create VMs.** Create VMs, from an image or a snapshot, billed to the organization.                                       | `POST /vms`                                                                                                                                                                                                                                       |
| `vm.power`           | **Start, stop, pause and resume VMs.** Drive the VMs the key reaches. Nothing is created or deleted.                       | `POST /vms/{id}/start`, `POST /vms/{id}/stop`, `POST /vms/{id}/pause`, `POST /vms/{id}/resume`                                                                                                                                                    |
| `vm.delete`          | **Delete any VM.** Delete any VM the key reaches, whoever created it.                                                      | `DELETE /vms/{id}`                                                                                                                                                                                                                                |
| `vm.delete-own`      | **Delete VMs this key created.** Delete only the VMs created with this key. Included in Delete any VM.                     | `DELETE /vms/{id}`                                                                                                                                                                                                                                |
| `snapshot.read`      | **List and inspect snapshots.** List snapshots and read their details. Also needed to create a VM from one.                | Snapshot and CI cache routes, in early access: not in these docs yet.                                                                                                                                                                             |
| `snapshot.write`     | **Take, rename and delete snapshots.** Snapshot the VMs the key reaches, rename, change the expiry of or delete snapshots. | Snapshot and CI cache routes, in early access: not in these docs yet.                                                                                                                                                                             |
| `environment.read`   | **List and inspect environments.** Names, variable keys with their last 4 characters, file paths. Never a value.           | `GET /environments`, `GET /environments/{ref}`                                                                                                                                                                                                    |
| `environment.write`  | **Change environments.** Create, update and delete environments, their variables and secret files.                         | `POST /environments`, `PATCH /environments/{ref}`, `DELETE /environments/{ref}`, `PUT /environments/{ref}/vars/{KEY}`, `DELETE /environments/{ref}/vars/{KEY}`, `PUT /environments/{ref}/files/{path}`, `DELETE /environments/{ref}/files/{path}` |
| `environment.reveal` | **Reveal environment values.** Read the secret values of an environment's variables and files.                             | `POST /environments/{ref}/reveal`                                                                                                                                                                                                                 |
| `ssh_key.read`       | **List SSH keys.** The public keys saved in the account of the key's user.                                                 | `GET /ssh-keys`                                                                                                                                                                                                                                   |
| `ssh_key.write`      | **Add and remove SSH keys.** Save or remove public keys in that account (POST /vms with save_key too).                     | `POST /ssh-keys`, `DELETE /ssh-keys/{id}`, and `POST /vms` with `save_key`                                                                                                                                                                        |
| `ci_cache.read`      | **List CI caches.** List the organization's GitHub Actions cache disks.                                                    | Snapshot and CI cache routes, in early access: not in these docs yet.                                                                                                                                                                             |
| `ci_cache.write`     | **Delete CI caches.** Delete a GitHub Actions cache disk.                                                                  | Snapshot and CI cache routes, in early access: not in these docs yet.                                                                                                                                                                             |
| `api_key.read`       | **List API keys.** List the keys this key could create, itself included.                                                   | `GET /api-keys`, `GET /api-keys/{id}`                                                                                                                                                                                                             |
| `api_key.write`      | **Create, rotate and revoke API keys.** Only keys with the same or fewer actions, resources, expiry and IP addresses.      | `POST /api-keys`, `PATCH /api-keys/{id}`, `DELETE /api-keys/{id}`, `POST /api-keys/{id}/rotate`                                                                                                                                                   |
| `*`                  | **Do everything.** Every action, including billing, members and those added later.                                         | Every route, today's and those added later.                                                                                                                                                                                                       |

Each page of the API reference gives the action of each route, next to it. A request the key's actions do not cover is refused with `403 insufficient_scope`, naming the action it needs: see [Errors](#errors).

The [MCP server](https://flockvm.com/docs/mcp) follows the same table: each tool needs the action of the route it calls, and a client only sees the tools its key can use.

### Keys created before scopes

Keys created before scopes existed keep working as they did: full access, every environment and VM, from any address. They show as **Full access (created before scopes)**, and the API marks them `"grandfathered": true` with `"lane": "legacy"`. Nothing changes their power: replace them with scoped keys when you can, then revoke them.

## Restrict a key to environments or VMs

By default a key reaches every VM of the organization. Two restrictions narrow that:

- **Environments**: the key reaches only the VMs created in these [environments](https://flockvm.com/docs/vms/environments), lists and reads only these environments, and creates VMs only in one of them. Creating a VM without one of its environments is refused with `403`.
- **VMs**: the key reaches only these VMs, by id. Or **only its own**, `"vms": "own"`: the VMs this key created and nothing else.

Both add up: a key with environments `staging` and the VM `vm_prod01` reaches the VMs of `staging` and `vm_prod01`. A key always reaches the VMs it created itself.

Anything out of reach answers `404`, exactly like another organization's VM: the key cannot even tell it exists. Lists leave it out. An environment out of reach and one that does not exist get the same answer, `404` with `No environment with this id or name.`; creating a VM with an environment outside the key's list is always the key's own `403`, whether that environment exists, is reserved to owners and admins, or does not exist.

## IP allowlist

A key can be limited to up to 20 IPv4 or IPv6 addresses and CIDR ranges, such as `203.0.113.7`, `203.0.113.0/24` or `2001:db8::/32`. From any other address the key is refused with `403 ip_not_allowed`. An empty list allows any address.

The address that counts is the one flockvm sees: the public address of your machine or of your CI runner, after any NAT. A CIDR with bits set after its prefix is refused, with the range it probably means (`10.0.0.1/8`: did you mean `10.0.0.0/8`?).

## Expiry and reminders

A key expires after 1 day, 7 days, 30 days, 90 days (the default) or 1 year, at a date you pick up to 365 days ahead, or never. An expired key is refused with `401` from the next request on, and stays listed, marked expired, until it is revoked or 30 days pass.

A key that expires within 7 days is flagged as expiring soon (`expiring_soon` in the API). Its user gets a mail about a week before (7 to 8 days ahead) and again a day or two before (24 to 48 hours ahead). If the daily send is delayed, the mail still goes, a little later: down to 6 days and 12 hours ahead. Each mail goes once, saying how long is really left and the exact expiry, with a link to **Settings → API keys**. A key created inside one of these windows gets no mail for it: a key created for 7 days gets only the second one.

The expiry never changes. To keep a job running, create a new key, put it where the old one is used, then revoke the old one.

## Rotate a key

Rotating gives a key a new secret, with the same scope, restrictions and expiry. The old secret can keep working for a grace period, so a job can switch without a failed request:

| Grace          | The old secret           |
| -------------- | ------------------------ |
| None (default) | Stops at once.           |
| 1 hour         | Works for 1 more hour.   |
| 24 hours       | Works for 24 more hours. |

Only the secret right before has a grace: rotating again without one ends it. An expired key cannot be rotated (`409 api_key_expired`): create a new one.

Only the key's own user rotates it, renames it or changes its description: a new secret acts as that user, so nobody else may get one. Owners and admins see every key of the organization and can **revoke** anyone's; rotating or editing someone else's key answers `403 forbidden` (`Only the key's owner can rotate or edit it; you can revoke it.`).

## Organization key policy

Enterprise Owners and admins of an Enterprise organization set rules for every key of the organization, in **Settings → API keys**. Other organizations see the policy, read-only.

| Rule                                | Default      | What it does                                                                                                                                                                                    |
| ----------------------------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Who can create keys                 | Every member | Owners and admins only, when set.                                                                                                                                                               |
| Longest lifetime                    | None         | 1 to 365 days: a key that never expires, or expires later, is refused.                                                                                                                          |
| Members may create full-access keys | Yes          | When off, a member picks actions: **Full access** is refused for them, and so is a key with every action ticked one by one (the same power), or with `api_key.write` (a key that creates keys). |
| IP allowlist                        | Any address  | Every key of the organization is refused from elsewhere, on top of its own list.                                                                                                                |

The first three apply to keys created afterwards; the IP allowlist applies at once, to every request of every key. Each change is in the [audit log](https://flockvm.com/docs/team/audit-log) as `api_key.policy_updated`.

## Manage keys over the API

| Method   | Path                    | What it does                                                        |
| -------- | ----------------------- | ------------------------------------------------------------------- |
| `GET`    | `/api-keys`             | The keys this key may see, with the catalog of actions and presets. |
| `POST`   | `/api-keys`             | Create a key. The secret is in the answer, once.                    |
| `GET`    | `/api-keys/{id}`        | One key, with the VMs it created that still exist.                  |
| `PATCH`  | `/api-keys/{id}`        | Change its name or description.                                     |
| `DELETE` | `/api-keys/{id}`        | Revoke it.                                                          |
| `POST`   | `/api-keys/{id}/rotate` | Give it a new secret.                                               |

A key manages only itself and the keys it could create: the same or fewer actions (`*` covers every one), the same or fewer environments and VMs, an expiry no later than its own, and an IP list at least as narrow. Any other key answers `404`, as if it did not exist. Then the user's role applies, as in the dashboard: owners and admins see and revoke every key of the organization, a member only their own (someone else's answers `404`, as a key that does not exist). Renaming and rotating stay the key's own user's, even for owners and admins. A key restricted to some environments or VMs lists no keys (`[]`), but reads, rotates and revokes itself by id.

### The API key object

```json
{
  "id": "ak_717jfgkcbs63",
  "name": "ci-deploy",
  "description": "GitHub Actions: preview VMs for each pull request.",
  "prefix": "flk_deknvy",
  "last4": "m2q9",
  "lane": "scoped-v1",
  "grandfathered": false,
  "preset": "ci",
  "scope": {
    "actions": ["account.read", "vm.read", "vm.logs", "vm.create", "vm.power", "vm.delete-own", "snapshot.read", "snapshot.write", "environment.read", "ssh_key.read", "ci_cache.read"],
    "environments": "*",
    "vms": "*",
    "ip_allowlist": []
  },
  "created_at": "2026-10-18T09:12:40.118Z",
  "expires_at": "2027-01-16T09:12:40.118Z",
  "expired": false,
  "expiring_soon": false,
  "last_used_at": "2026-11-07T10:02:11.540Z",
  "last_used_ip": "203.0.113.10",
  "rotated_at": null,
  "previous_secret_expires_at": null,
  "created_by": { "id": "usr_ada", "name": "Ada Martin", "email": "ada@acme.dev" },
  "vms_created": 12
}
```

`prefix` (string): The secret's first 10 characters, with `last4` its last 4: what tells keys apart. The secret itself is never shown again.

`lane` (string): `scoped-v1` for keys with a scope, `legacy` for keys created before scopes (then `grandfathered` is `true` and the scope is full access).

`preset` (string): The preset the actions are exactly: `read-only`, `operate`, `ci`, `full`, or `custom`.

`scope` (object): `actions`; `environments`, `"*"` or a list of environment ids; `vms`, `"*"`, `"own"` or a list of VM ids; `ip_allowlist`, addresses and CIDRs (`[]`: any).

`last_used_at` (string or null): When the key was last used, to within 10 minutes, and `last_used_ip` the address it came from.

`previous_secret_expires_at` (string or null): After a rotation with a grace period: until when the old secret still works.

`vms_created` (integer): How many VMs this key created, deleted ones included.

### List keys

`GET /api-keys` · Action: `api_key.read`

```bash
curl https://flockvm.com/api/v1/api-keys \
  -H "Authorization: Bearer $FLOCKVM_API_KEY"
```

```jsonc
{
  "api_keys": [
    { "id": "ak_717jfgkcbs63", "name": "ci-deploy", /* … the API key object */ }
  ],
  "catalog": {
    "actions": [
      { "name": "account.read", "label": "Read the account and catalog", "description": "…", "group": "Account" }
      // … every action
    ],
    "presets": [
      { "id": "ci", "label": "Create and use VMs", "description": "…", "actions": ["account.read", "…"], "creates_vms": true, "recommended": true }
      // … read-only, operate, full, and custom (its actions null)
    ],
    "default_preset": "ci",
    "grandfathered_label": "Full access (created before scopes)",
    "expiries": ["1d", "7d", "30d", "90d", "365d", "never"],
    "default_ttl": "90d",
    "max_ttl": "365d",
    "expiring_soon_days": 7,
    "graces": ["none", "1h", "24h"],
    "max_ip_allowlist": 20
  },
  "policy": { "enterprise": true, "who_can_create": "members", "max_lifetime_days": 90, "members_may_create_full_access": false, "ip_allowlist": [] }
}
```

Newest first. Revoked keys are not listed; expired ones are, with `"expired": true`. `catalog` is what a form or a CLI needs to offer the choices; `policy` is the organization's key policy, read-only here.

### Create a key

`POST /api-keys` · Action: `api_key.write`

```bash
curl https://flockvm.com/api/v1/api-keys \
  -H "Authorization: Bearer $FLOCKVM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "preview-envs", "preset": "ci", "environments": ["env_vu796tb9"], "expires_in": "30d", "ip_allowlist": ["203.0.113.0/24"]}'
```

```jsonc
// HTTP/1.1 201 Created
{
  "api_key": { "id": "ak_c41n2mzq8x0d", "name": "preview-envs", /* … the API key object */ },
  "secret": "flk_…"
}
```

`secret` is in this answer only: store it now.

`name` (string, required): 1 to 60 characters.

`description` (string): Up to 200 characters.

`preset` (string): `read-only`, `operate`, `ci` or `full`. Send `preset` or `actions`, one of them.

`actions` (array of strings): Action names from the [table above](#actions). An unknown name is refused (`400`, `field` `actions`).

`environments` (string or array, default `*`): `"*"` or environment ids (`env_…`), up to 50.

`vms` (string or array, default `*`): `"*"`, `"own"`, or VM ids (`vm_…`), up to 100.

`expires_in` (string, default `90d`): `1d`, `7d`, `30d`, `90d`, `365d` or `never`. With neither `expires_in` nor `expires_at`, the new key expires in 90 days, or with this key if it expires sooner (a 90-day key can create keys without giving an expiry). An expiry given explicitly, later than this key's, is refused.

`expires_at` (string): Instead of `expires_in`: an ISO 8601 date or date-time, from 1 hour to 365 days ahead.

`ip_allowlist` (array of strings, default `[]`): Up to 20 IPv4 or IPv6 addresses and CIDRs.

Any other field is refused with `400` and `field` set to it, so a misspelled scope never creates a full-access key. Within the [policy](#organization-key-policy), the new key must be one this key could create; else `403 forbidden` with `field` naming what goes beyond (`actions`, `environments`, `vms`, `expires_at` or `ip_allowlist`). An organization has 50 live keys at most (`409 conflict`).

### Get a key

`GET /api-keys/{id}` · Action: `api_key.read`

```bash
curl https://flockvm.com/api/v1/api-keys/ak_717jfgkcbs63 \
  -H "Authorization: Bearer $FLOCKVM_API_KEY"
```

The API key object, plus `vms`: the VMs this key created that still exist, newest first, each `{"id", "name", "status", "region", "environment_id", "created_at"}`. `vms` is `null` when flockvm could not reach its hosts just now; a VM on a host that does not answer is left out.

### Rename or describe a key

`PATCH /api-keys/{id}` · Action: `api_key.write`

```bash
curl -X PATCH https://flockvm.com/api/v1/api-keys/ak_717jfgkcbs63 \
  -H "Authorization: Bearer $FLOCKVM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "ci-website", "description": "Preview VMs of the website repository."}'
```

Answers the API key object. Only `name` and `description` (`""` or `null` for none). A scope field is refused with `400`: create a new key instead. Only the key's own user may: someone else's key answers `403 forbidden` to an owner or admin, `404` to a member.

### Rotate a key

`POST /api-keys/{id}/rotate` · Action: `api_key.write`

```bash
curl -X POST https://flockvm.com/api/v1/api-keys/ak_717jfgkcbs63/rotate \
  -H "Authorization: Bearer $FLOCKVM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"grace": "1h"}'
```

```jsonc
{
  "api_key": { "id": "ak_717jfgkcbs63", "prefix": "flk_9hd2kq", "previous_secret_expires_at": "2026-11-07T11:00:00.000Z", /* … */ },
  "secret": "flk_…"
}
```

`grace`: `none` (the default), `1h` or `24h`. A key may rotate itself: with a grace, the secret it used keeps working until `previous_secret_expires_at`. Only the key's own user may rotate it: someone else's key answers `403 forbidden` to an owner or admin (they can revoke it), `404` to a member.

### Revoke a key

`DELETE /api-keys/{id}` · Action: `api_key.write`

```bash
curl -X DELETE https://flockvm.com/api/v1/api-keys/ak_717jfgkcbs63 \
  -H "Authorization: Bearer $FLOCKVM_API_KEY"
```

```text
HTTP/1.1 204 No Content
```

The key, and a rotation's old secret, are refused from the next request on. A key may revoke itself.

## Errors

A request the key's actions do not cover answers `403 insufficient_scope`. `field` is the action it needs, and the `WWW-Authenticate` header says it too ([RFC 6750](https://www.rfc-editor.org/rfc/rfc6750#section-3.1)):

```text
HTTP/1.1 403 Forbidden
www-authenticate: Bearer realm="flockvm", error="insufficient_scope", scope="vm.create"

{"error":{"code":"insufficient_scope","message":"This API key cannot create VMs: it needs vm.create.","field":"vm.create"}}
```

| Status | `code`               | When                                                                                                                                                                                                 |
| ------ | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 400    | `bad_request`        | An unknown field, action or preset, a bad environment or VM id, IP address or expiry. `field` names it.                                                                                              |
| 401    | `unauthorized`       | No key, an unknown, revoked or expired key, or a rotated key's old secret after its grace.                                                                                                           |
| 403    | `insufficient_scope` | The key does not have the action the route needs. `field` is that action.                                                                                                                            |
| 403    | `ip_not_allowed`     | The request comes from an address the key's IP allowlist, or the organization's, does not include.                                                                                                   |
| 403    | `forbidden`          | The user's role does not allow it, the organization's policy refuses the new key, the new key would go beyond this one (`field` says how), or an owner or admin rotates or edits someone else's key. |
| 404    | `not_found`          | No such key, a revoked one, another member's (for a member), or a key this key could not create.                                                                                                     |
| 409    | `api_key_expired`    | Rotating an expired key. Create a new one.                                                                                                                                                           |
| 409    | `conflict`           | The organization already has 50 live keys.                                                                                                                                                           |

## Audit log

Every change to a key is in the organization's [audit log](https://flockvm.com/docs/team/audit-log): `api_key.created` (with its actions, environments, VMs, IP allowlist and expiry), `api_key.updated`, `api_key.rotated` (with the grace), `api_key.revoked`, and `api_key.policy_updated` for the policy. A change made through a key names that key. Refused requests are not recorded.

## Next steps

- [API overview](https://flockvm.com/docs/api/overview): Base URL, authentication, errors.
- [VMs](https://flockvm.com/docs/api/vms): Create, drive and delete VMs with a key.
