> ## Documentation Index
> Fetch the complete documentation index at: https://docs.boat.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Create and list keys

> Create a scoped boat API key, use it, list your keys and see the usage of one key.

## Create a key

To create a key, you need one of these:

* A browser session: `boat login` with no key, or the dashboard.
* A key with the `account.admin` action, or an `admin` key. The new key cannot have more actions, more sandboxes or a later expiry than this key.

The create endpoint is `POST https://boat.dev/api/v1/api-keys/scoped`.

> [!NOTE]
>
> During a credential rollout, boat can turn off key creation for a short time. `GET /api/v1/api-keys` reports this in `catalog.scopedCreationEnabled`. While creation is off, the create endpoint returns a typed 503.

```bash theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
boat api-key create my-project --ttl 90d --preset ci --sandbox bx_123
boat api-key create readonly --ttl 30d --preset read-only
boat api-key create admin --ttl 7d --preset admin
boat api-key create custom --ttl 24h --actions sandbox.read,exec,file.read
```

| Flag | Rule |
| - | - |
| `--ttl` | The maximum is 365 days. `--ttl never` makes a key that works until you revoke it. A key that expires cannot create one that never expires. |
| `--sandbox`, `--env` | You can give each one more than one time. |
| `--actions`, `--preset` | Use one or the other, not both. |
| No `--ttl` | The key expires after 90 days. |
| No scope flags | The command fails. Pass at least one of `--actions`, `--preset`, `--sandbox` or `--env`. |

> [!WARNING]
>
> Full account access requires an explicit `--preset admin`. An admin key can also create more keys.

A key that never expires is still a scoped key. If it leaks, it works until you revoke or rotate it. Read [Store, rotate and revoke](https://docs.boat.dev/api-keys/store-rotate-revoke.md).

For the actions and presets, read [Scopes](https://docs.boat.dev/api-keys/scopes.md).

With the API:

```bash theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
curl -sS -X POST https://boat.dev/api/v1/api-keys/scoped \
  -H "Authorization: Bearer $BOAT_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name":"tenant-42","ttl":"30d","preset":"full-sandbox","sandboxIds":["bx_123"]}'
```

The body takes `name`, `ttl`, `preset` or `actions`, `sandboxIds` and `environmentIds`.
Pass at least one non-empty scope field. Full account access requires `"preset":"admin"`.

boat shows the secret one time only. In scripts, capture it like this:

```bash theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
BOAT_API_KEY="$(boat api-key create my-project --preset ci --ttl 90d --json | jq -r '.secret')"
```

You can also use the **API Keys** tab in the dashboard. It has expiry presets, action presets, a raw action picker, and sandbox and environment selectors.

## Use the key

```bash CLI theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
boat login --key-stdin --json <<< "$BOAT_API_KEY"
```

```bash curl theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
curl -sS "$BOAT_API_BASE/me" \
  -H "Authorization: Bearer $BOAT_API_KEY"
```

```ts TypeScript theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
import { BoatApi, Configuration } from "@boatdev/sdk";

const sandbox = new BoatApi(new Configuration({
  basePath: "https://boat.dev/api/v1",
  accessToken: process.env.BOAT_API_KEY!,
}));

const me = await sandbox.me();
console.log(me.user.login);
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
import os
from boat_sdk import ApiClient, Configuration
from boat_sdk.api.boat_api import BoatApi

config = Configuration(host="https://boat.dev/api/v1", access_token=os.environ["BOAT_API_KEY"])
with ApiClient(config) as client:
    sandbox = BoatApi(client)
    me = sandbox.me()
    print(me.user.login)
```

## What a key cannot do

* Create needs a browser session or an admin-scoped token. A credential that can make more credentials defeats least privilege.
* Rotate and revoke need a browser session or a key that can create keys. Such a key can rotate or revoke itself and any key that it could create.
* To approve an agent claim, you need a browser session. API keys cannot approve agent claims. This includes admin keys and legacy keys.

Use `boat api-key create|rotate|revoke`, or use the [API Keys](https://boat.dev/dashboard?tab=api-keys) tab.

## List

The CLI, the API, the SDKs and the dashboard can list keys. boat never returns the secrets.

Each row shows:

* The id, the name, the prefix and the last four characters
* The scope and the expiry
* The last use
* The request total for the last 30 days
* How many sandboxes and Agents the key created that still exist

boat labels expired keys and keys that expire soon.

A key limited with `--sandbox` or `--env` gets an empty `apiKeys` list. A browser session, a CLI session or a key with no such limit gets the full list.

```bash theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
boat api-key list
boat api-key list --all    # include per-sandbox machine keys
```

```ts TypeScript theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
const keys = await sandbox.apiKeys();
for (const key of keys.apiKeys) {
  console.log(key.id, key.name, key.usage.requests, key.resources.total);
}
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
keys = sandbox.api_keys()
for key in keys.api_keys:
    print(key.id, key.name, key.usage.requests, key.resources.total)
```

## See usage for one key

`boat api-key usage <id>` prints the 30-day request total and the count of live resources. These are the same numbers as in the list.

* Add `--verbose` to see the split between sandboxes and Agents, and the list of created resources.
* The matching API is `GET /api-keys/{id}/usage`.
* Usage still works after you revoke the key, if you still have the id.

Revoke stops the secret. It does not delete the sandboxes or Agents that the key created.

```bash theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
boat api-key usage sak_123
boat api-key usage sak_123 --verbose
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.