# Personal API keys

> Create xk_ keys that act as you, limited to repositories, groups and scopes, for scripts, git over HTTPS and the registry.
A personal API key (`xk_…`) is a long-lived credential that acts as
**you**. Use one where a sign-in that refreshes itself won't do: a script,
a CI system outside branchd, or a Docker or git client on a server.

A personal key:

- acts as you, and never does more than your **current** role allows;
- reaches **everything** you can reach, or only the repositories and
  groups you bind it to (at most 50; a group covers everything under it);
- carries only the [scopes](https://docs.branchd.run/branchd/access/scopes) you give it, picked by level or one
  by one;
- **expires within 365 days**. Only an organisation administrator may
  issue a key that never expires;
- stops working when you revoke it, or if your account is deactivated;
- can never manage keys, SSH keys, session credentials, bots or access
  grants.

You create a key signed in as yourself, or an agent acting for you does
(an [agent session](https://docs.branchd.run/branchd/access/cli-login#agent-sessions), or the
Xeonr MCP gateway's `create_personal_key` with _credentials_ ticked); the
key records the agent that made it. A bot, a pipeline job or another key
cannot make one. You can't give a key a scope your own sign-in doesn't hold.

The secret is shown **once**, when the key is created. branchd keeps only
its prefix.

## In the console

Open **API keys** in your settings (`/-/settings/tokens`). Choose a name,
whether it reaches everything or only some repositories and groups, a
level or individual scopes, and an expiry.

## With the CLI

```sh
# A key that pulls images from one repository, for 30 days
branchd token create ci-pull --repo acme/api --scopes forge:package:read --expires 30d

# Clone everything in a group, at the read level (the default), for 90 days (the default)
branchd token create build-clone --group acme

# List your keys, then revoke one
branchd token list
branchd token revoke 0b6c1d2e-3f40-4a5b-8c6d-7e8f90a1b2c3
```

`branchd token create <name>` takes:

| Flag                  | Meaning                                                                                                   |
| --------------------- | --------------------------------------------------------------------------------------------------------- |
| `--repo PATH`         | Bind the key to this repository. Repeat for more.                                                         |
| `--group PATH`        | Bind the key to this group and everything under it. Repeat for more.                                      |
| `--level LEVEL`       | `read` (default), `write`, `maintain` or `admin`. See [levels](https://docs.branchd.run/branchd/access/scopes#levels).            |
| `--scopes S,...`      | Exactly these scopes, instead of a level                                                                  |
| `--expires WHEN`      | `<n>d` (up to `365d`), a date `YYYY-MM-DD`, or `never` (organisation administrators only). Default `90d`. |
| `-d`, `--description` | A note for yourself                                                                                       |
| `--json`              | Print the whole response                                                                                  |

Without `--repo` or `--group`, the key reaches everything you can. Only the
secret goes to standard output, so it can be piped:

```sh
branchd token create ci-push --repo acme/api --scopes forge:package:edit --expires 30d \
  | docker login registry.branchd.run -u token --password-stdin
```

`branchd token list` shows your keys with their prefix, what they reach,
their scopes (or level), expiry, last use and state. `--all` includes
revoked ones. `branchd token revoke <id>` takes the key's id or its full
`urn:xeonr:auth:api-key:…` name. `--yes` is needed when there is no
terminal to confirm on.

## Using a key

| Where                    | How                                                                                                                                         |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- |
| API                      | `Authorization: Bearer xk_…`                                                                                                                |
| git over HTTPS           | The key as the password. The username is ignored.                                                                                           |
| Registry                 | `docker login registry.branchd.run -u token --password-stdin`                                                                               |
| Generic and npm packages | The key as the password with `curl --user`, or as `_authToken` in `.npmrc`. See [Access to packages](https://docs.branchd.run/branchd/packages/access#which-token). |

## The API

`xeonr.forge.v1.PersonalKeyService`:

| Method                  | Does                                                                                                                                                                                                                                          |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `GetPersonalKeyOptions` | The scopes you may issue, with descriptions; the levels; the maximum lifetime; whether you may omit an expiry; the maximum number of bindings                                                                                                 |
| `CreatePersonalKey`     | `name` (1–64 characters), `description`, either `everything: true` or `bindings[]` (each `{type: REPOSITORY or GROUP, path, scopes[]}`; a binding with no scopes takes the key's), `level` and/or `scopes[]`, and `expires_at` or `no_expiry` |
| `ListPersonalKeys`      | Your keys. `include_revoked` adds revoked ones.                                                                                                                                                                                               |
| `RevokePersonalKey`     | By `key` (`urn:xeonr:auth:api-key:<uuid>`). Revoking twice is not an error.                                                                                                                                                                   |

A binding to a repository or group you can't read is refused as not found.
An expiry more than 365 days away is refused, and so is `no_expiry` from
anyone but an organisation administrator.

Source: https://docs.branchd.run/branchd/access/personal-keys
