# API keys

Create, use, rotate and revoke the keys that call the onshore.sh API.

An API key lets `ons`, the E2B SDKs and your own code call the onshore.sh API for one organization. It looks like `msb_<id>_<secret>`. Only the prefix `msb_<id>` is ever shown again after creation.

## Get a key

### ons login (your machine)

```bash
ons login
```

`ons login` opens a page in your browser and prints a code (`XXXX-XXXX`). Sign in, type the code, and `ons` saves a new key named `msb login · <host>`. `<host>` is your machine's name.

- The key is saved with its domain in `~/.config/onshore/config.toml`, readable only by you.
- If you belong to several organizations, pick one with `ons login --org <organization-id>`.
- When the browser runs on another machine, add `--paste` and paste the address the browser ends on.
- A new `ons login` from the same machine revokes the key of the previous one, unless that key still owns live miniVMs or AI accounts. `ons` tells you which keys it kept.

### Console (CI, servers)

1. Open [onshore.sh/app](https://onshore.sh/app/) → **API keys**.
2. Give the key a name (1 to 64 characters), such as the job or service that uses it.
3. Copy the key. **It is shown once.**

Store it in your CI's or platform's secret store, never in a repository.

### AI agents

```bash
ons login --agent
```

An AI agent signs in with its own agent identity in its own browser. The key lasts 1 hour and belongs to its owner's organization. When calls start answering `401`, the agent runs `ons login --agent` again. The owner must have signed in to the console once.

## Use a key

| Where    | How                                                                                          |
| -------- | -------------------------------------------------------------------------------------------- |
| `ons`    | `ons login` saves it. In CI, set `ONS_API_KEY` instead; it wins over the saved login.        |
| E2B SDKs | Set `E2B_API_KEY=<your-key>` and `E2B_DOMAIN=onshore.sh`. `ons` ignores these two variables. |
| REST API | Send the header `X-API-Key: <your-key>` to `https://api.onshore.sh`.                         |

`<your-key>` is the full key, `msb_…`.

To pass a key to `ons login` from a script, pipe it on stdin:

```bash
printf '%s\n' "$ONS_API_KEY" | ons login --key
```

> **Warning:** Never put a key on a command line, in a file you commit, or in a prompt to an AI agent. `ons` has no `--api-key` flag on purpose.

## What a key can see

- A key sees and acts on **the miniVMs it created**. Another key of the same organization does not see them through the API.
- In the console, every member sees all the organization's miniVMs (shape and who started them, never their environment, metadata or tokens). Members pause, resume and kill the miniVMs of their own keys; owners act on any. See [Organizations](https://onshore.sh/docs/account/organizations#roles).
- A key calls the miniVM and template API. It cannot create keys, manage members or change billing: that is done in the console.

## Revoke a key

Open **API keys** in the console and revoke the key. Members revoke the keys they created; owners revoke any key.

- Revocation takes effect within 5 seconds and is final.
- The miniVMs the key created keep running, but nothing can act on them through that key anymore. Kill them from **Fleet** in the console, or let them reach their timeout. A miniVM of a revoked key cannot be resumed.

## Rotate a key

1. Create a new key.
2. Deploy it where the old one was used.
3. Kill or let end the miniVMs the old key created: the new key cannot reach them.
4. Revoke the old key.

If a key leaked, revoke it at once, then rotate.

## Limits

- An organization holds at most 100 active keys.
- An organization creates at most 200 keys per 24 hours, revoked ones included.
- Repeated calls with a wrong key are rate-limited per client address.

## Related

- [Organizations](https://onshore.sh/docs/account/organizations)
- [Quotas and limits](https://onshore.sh/docs/account/quotas-and-limits)
- [CLI reference](https://onshore.sh/docs/reference/cli)

Markdown version: https://onshore.sh/docs/account/api-keys.md
