# API keys

URL: https://skywake.dev/docs/security/api-keys

Give agents, the SDKs, the MCP server and the CLI their own key, limited by environment, role and name.

Every call to the API needs a key. A key belongs to one organization and one environment, and has a role. Give each agent or tool its own key, with only what it needs, so you can see what it did and revoke it on its own.

```ts tab="TypeScript"
import { PermissionError, Skywake } from "skywake";

const sky = new Skywake();
await sky.createDisk("app");

const made = await sky.createKey({
  name: "agent-1",
  role: "write",
  environment: "default",
  only: ["app"],
  expiresInDays: 30,
});

const agent = new Skywake({ key: made.key });
await agent.disk("app").write("notes.md", "hello");

try {
  await agent.disk("billing").write("notes.md", "hello");
} catch (error) {
  if (!(error instanceof PermissionError)) throw error;
  console.log("billing:", error.message);
}

for await (const key of sky.listKeys()) {
  console.log(key.id, key.name, key.role, key.environment ?? "every", key.only.join(","));
}

await sky.revokeKey(made.id);
```
```py tab="Python"
from skywake import PermissionError, Skywake

sky = Skywake()
sky.create_disk("app")

made = sky.create_key(
    "agent-1",
    "write",
    environment="default",
    only=["app"],
    expires_in_days=30,
)

agent = Skywake(key=made["key"])
agent.disk("app").write("notes.md", "hello")

try:
    agent.disk("billing").write("notes.md", "hello")
except PermissionError as error:
    print("billing:", error.message)

for key in sky.list_keys():
    print(key["id"], key["name"], key["role"], key["environment"] or "every", ",".join(key["only"]))

sky.revoke_key(made["id"])
```
```bash tab="CLI"
skywake create disk app
made=$(skywake keys create --role write --only app --env default --name agent-1 --json)
key=$(jq -r .key <<<"$made")

echo hello | SKYWAKE_API_KEY=$key skywake write app/notes.md
SKYWAKE_API_KEY=$key skywake whoami

skywake keys
skywake keys revoke "$(jq -r .id <<<"$made")" --yes
```

The key is shown only once, so keep it right away. Pass it as `SKYWAKE_API_KEY`, as `key` in an SDK, or as a bearer token: `Authorization: Bearer sw_...`. You can also make one in the console under **Settings → API keys**.

## Sign in from the CLI [#sign-in-from-the-cli]

On your own machine, you don't need to copy a key at all:

```bash norun
skywake login
```

The CLI shows a code and opens the console at `/cli/CODE`. Check that the browser shows the same code, then allow it. The CLI makes the key itself and sends only its id, so the key never passes through the browser. It saves the key in `~/.config/skywake/credentials.toml`.

- Any member can allow a key for one environment, with a role up to their own.
- A key for every environment needs an organization admin.
- `skywake login --paste` saves a key you paste instead. `--profile NAME` saves it under another profile, and `--url` points that profile at another server, such as [your own](../self-hosting/local-api.mdx).

## Roles [#roles]

Each role can do everything the role before it can, and more.

| Role    | Disks                                                                                                                                 |
| ------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `read`  | Read, list, grep, find and query files; read history and diffs.                                                                       |
| `write` | Also write, delete and move files; fork, merge, revert and restore.                                                                   |
| `admin` | Also create, configure and destroy disks, including turning search on; merge into [protected branches](../filesystem/protection.mdx). |

Managing keys and reading the [audit log](./audit-log.mdx) need an organization admin.

If a key lacks what a call needs, the call fails with `PermissionError` (`403`), and the message says which role it needs. Creating a disk, including by the first write to a new name, needs `admin`. That is why the example creates `app` with the admin key before the agent writes to it.

## Environments and names [#environments-and-names]

A key works in one environment, such as `prod` or `staging`. Requests with it go to that environment, and names like `app` mean the disk in that environment.

`only` narrows a key to some disks in its environment: names, or prefixes that end in `*`, such as `["app", "docs-*"]`. The default is every name (`["*"]`). A key can't reach more than the key that makes it.

An organization admin can make an **all-environments** key with `environment: null` (CLI `--all-environments`). Requests with it name the environment: `new Skywake({ environment: "staging" })`, the `SKYWAKE_ENV` variable, `--env` in the CLI, or the `Skywake-Environment` header. With none of these, they go to `default`.

<Callout type="warn">
  When an all-environments key makes a key without naming an environment, the new key is also an
  all-environments key, and `only` is ignored. Name the environment, as the example does.
</Callout>

## Fields [#fields]

| Field                                      | Meaning                                                                              |
| ------------------------------------------ | ------------------------------------------------------------------------------------ |
| `name`                                     | 1 to 100 characters.                                                                 |
| `role`                                     | `read`, `write` or `admin`.                                                          |
| `environment`                              | The environment it works in. Null for every environment. The maker's own by default. |
| `only`                                     | Up to 50 names or prefixes. `["*"]` by default.                                      |
| `expiresInDays` (Python `expires_in_days`) | 1 to 3650. Without it, the key does not expire.                                      |

A key made with another key never outlives the key that made it.

A key belongs to the person who made it in the console. When that person leaves the organization or is removed from it, their keys are revoked, and so are any keys made with those keys.

## List and revoke [#list-and-revoke]

Listing keys never returns the keys themselves. It returns each key's `id`, `name`, `role`, `environment`, `only`, `expires`, `last_used`, `created`, and who made it:

- `created_by` is `user:ID` for a person in the console, or `key:ID` for a key made with another key.
- `created_by_email` is that person's email.
- Both are empty for the first key of an organization.

Revoking a key takes effect at once. Over HTTP, keys are `GET /v2/keys`, `POST /v2/keys` and `DELETE /v2/keys/{id}`, and `GET /v2/whoami` shows what the current key may do.

<Callout type="info">
  Keys look like `sw_...` and end in a checksum, so secret scanners can spot a leaked one. Store
  credentials made with a key keep working until they expire (900 seconds by default), even after
  you revoke the key. See [scoped credentials](./scoped-credentials.mdx).
</Callout>

Making and revoking keys is recorded in the [audit log](./audit-log.mdx).

## Next [#next]

<Cards>
  <Card href="./scoped-credentials.mdx" />
  <Card href="../agents/mcp.mdx" />
  <Card href="/reference/cli/keys" />
</Cards>