# API keys

Use an API key to manage servers from scripts and CI. Send it as `Authorization: Bearer <key>` to the public API. Every endpoint is described in the [API reference](/docs/api-reference).

## Create a key

Owners and admins can create keys.

1. Go to **API keys** and select **Create key**.
2. Give it a name that says where it will be used, for example "CI pipeline".
3. Choose the permissions ([see below](#permissions)) and, if you like, when the key expires.
4. Copy the key at once. It is shown only one time. We store only a hash, so we cannot show it again.

An organization can hold up to 20 keys. The list shows each key's start, its permissions, when it expires, when it was created and by whom, and when it was last used.

For last use the list also shows the network it came from and how many calls the key has made. For privacy we keep only the network, never the full address: the first three parts of an IPv4 address (`203.0.113.0/24`) or the first 48 bits of an IPv6 address (`2001:db8:1::/48`). Every accepted call is counted; refused calls (a wrong key, an expired key, an address outside the allowlist) are not. Through the API, `GET /api/v1/api-keys` returns `lastUsedNetwork` and `callCount`; `apidly api-keys` shows them in the FROM and CALLS columns.

## Keep keys safe

- Do not put keys in code that you share or in public repositories.
- Store them in an environment variable or a secrets manager.
- Use one key per system.

## Edit a key

Select **Edit key** next to the key. You can rename it, lower its scope, take permissions away (untick them under **Permissions**), make it expire sooner and change the addresses it accepts calls from. A key can only be narrowed this way: a higher scope, a later expiry or more permissions answer `403 key_change_widens`; create a new key for more access. Letting a key be called from more addresses needs a member (or a calling key) who can already do everything the key can. The key itself does not change.

Through the API, send `PATCH /api/v1/api-keys/{id}` with any of `name`, `scope`, `permissions` (a subset of the key's list), `expiresAt` (an earlier time) and `allowedCidrs` (an empty list removes the allowlist). From the command line: `apidly api-keys update ID --name NAME --scope read`.

## Rotate a key

Rotating gives you a new key with the same name, scope, permissions, address allowlist, expiry and endpoint binding. Select **Rotate key** next to the key and choose how long the old key keeps working: not at all, 15 minutes, 1 hour or 24 hours. The time lets your programs switch to the new key; until it ends both keys count toward the limit of 20. The new key is shown once, like a new key.

The new key has a new id. Only a member (or a calling key) who can already do everything the key can may rotate it, so rotating never hands out more access than you have. An expired key cannot be rotated; create a new one.

Through the API, send `POST /api/v1/api-keys/{id}/rotate` with `graceMinutes` from 0 (the default, the old key stops at once) to 1440. From the command line: `apidly api-keys rotate ID --grace 1h`.

## Monthly spending limit

A key can carry a monthly spending limit. It counts the usage charges (compute and storage) of the servers and endpoints launched with the key in the current UTC month. Set it under **Monthly spending limit (USD, optional)** when you create the key, or use the edit button in the **This month** column, which shows what the key has used so far. Leave the field empty for no limit.

When the limit is reached, the key shows **Limit reached** and new launches and starts with it are refused with `403 key_limit_reached`. Servers already running are not stopped and keep being charged, and the owner gets one email that month. Rotating a key does not reset its month: what the old key launched counts toward the new one.

Through the API, send `PUT /api/v1/api-keys/{id}/spending-limit` with `monthlyLimitCents` (whole US cents), or `null` to remove the limit; this needs the `full` scope, and a key that has a limit of its own cannot change limits. From the command line: `apidly api-keys limit ID --monthly-limit 50` (`none` removes it), or `--monthly-limit 50` on `apidly api-keys create`.

## Revoke a key

Select **Revoke** next to the key and confirm. It stops working at once and cannot be undone. If a key may be exposed, rotate it with no overlap, or revoke it and create a new one.

## Permissions

Every key has one of three scopes. A scope only limits a key; inside it, the key still acts with the access of the person who created it, read at the time of each call.

| Scope | What the key can do |
| --- | --- |
| Read only (`read`) | Read servers, plans, templates, usage, billing and activity (`GET` calls). Not API keys, team invitations, roles, webhooks or secret names. It cannot change anything. |
| Servers (`servers`) | Read, and request, start, stop, restart, rename and delete servers, set or clear their automation rules, pause and resume server groups, cancel requests that wait for approval, list secret names, save and delete plan filters, and watch plans. It cannot add credit, or change SSH keys, templates, webhooks, or create, change or delete groups. |
| Full access (`full`) | Everything the creator's role allows, including deleting servers, adding credit (hosted checkout), and managing SSH keys, templates and server groups. |

A key can also carry its own permission list, chosen when you create it. A key with a list can do only what is on it, and calling a serverless endpoint (a request to its address, or an async job) needs **Call endpoints** (`endpoints.invoke`), because it can start paid workers. Owners, admins and members hold it by default. A key without a list, including every key created before the list existed, can call endpoints only while the member who created it holds **Call endpoints**; every built-in role does, so such a key stops only if that member moves to a custom role without it. Cancelling a job through the API needs the same permission. A key limited to a list that lacks **Call endpoints**, for example one limited to viewing servers, now answers `403 forbidden` on endpoint calls; create a new key that includes it.

Keys created before scopes existed have full access. A call the scope does not allow answers `403 insufficient_scope`.

Other rules:

- Deleting a server needs a key created by an owner or admin, with the `servers` or `full` scope. Creating or changing templates, webhooks and groups needs such a key with the `full` scope. If that person has since been removed or demoted, the key falls back to member access.
- A key works only in the organization it was created in. If the organization is suspended, every call is refused.
- A key with an expiry date stops working at that moment and answers `401`. Create a new key before it expires; an expired key cannot be extended.

Each key allows 120 read requests and 20 write requests per minute. Every change made through a key is recorded in the audit log with the key as the actor.

## Try it

```bash
export APIDLY_API_KEY="apc_live_..."
curl https://YOUR-DOMAIN/api/v1/servers \
  -H "Authorization: Bearer $APIDLY_API_KEY"
```

Replace `YOUR-DOMAIN` with the address you use to open the console. The API keys page shows the same command with your address filled in, and shows when each key was last used.
