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

# Authentication and tokens

> Protect a KiwiFS server with a global API key, scoped per-space tokens, or OIDC, and manage tokens from the CLI.

KiwiFS has no authentication by default, which suits a laptop or a trusted network. Before you expose a server, pick an auth mode in `[auth]` of `.kiwi/config.toml`. Every mode uses the same header:

```bash Terminal theme={null}
curl http://localhost:3333/api/kiwi/tree \
  -H "Authorization: Bearer $KIWI_TOKEN"
```

## Auth modes

> [tab] none (default)
>
> No authentication.
>
> ```toml .kiwi/config.toml theme={null}
> [auth]
> type = "none"
> ```

> [tab] apikey
>
> One shared key for every request. Use `${ENV_VAR}` to keep the secret out of the file.
>
> ```toml .kiwi/config.toml theme={null}
> [auth]
> type = "apikey"
> api_key = "${KIWI_API_KEY}"
> ```
>
> Or from the command line: `kiwifs serve --auth apikey --api-key "$KIWI_API_KEY"`.

> [tab] perspace
>
> Many keys, each with its own scope, space, path prefix, and actor name.
>
> ```toml .kiwi/config.toml theme={null}
> [auth]
> type = "perspace"
>
> [[auth.api_keys]]
> key = "kiwi_ro_..."
> actor = "ci-reader"
> scope = "read"
>
> [[auth.api_keys]]
> key = "kiwi_rw_..."
> actor = "onboarding-bot"
> scope = "write"
> prefix = "onboarding/"
>
> [[auth.api_keys]]
> key = "kiwi_adm_..."
> actor = "admin"
> scope = "admin"
> ```
>
> Create keys with [`kiwifs token create`](#manage-tokens-with-the-cli) instead of writing them by hand.

> [tab] oidc
>
> Accept ID tokens from your identity provider.
>
> ```toml .kiwi/config.toml theme={null}
> [auth]
> type = "oidc"
>
> [auth.oidc]
> issuer = "https://accounts.google.com"
> client_id = "your-client-id"
> ```
>
> Clients send the provider's JWT as the bearer token. KiwiFS records the token's `email` claim (or `sub`) as the actor in git commits. Command-line equivalent: `--auth oidc --oidc-issuer ... --oidc-client-id ...`.

A request without a valid token returns `401`. A valid token used outside its scope, space, or prefix returns `403`.

## Per-space token fields

Each `[[auth.api_keys]]` entry supports:

| Field | Default | Description |
| - | - | - |
| `key` | — | The secret the client sends as a bearer token |
| `scope` | `admin` | `read`, `write`, or `admin` |
| `space` | all spaces | In [multi-space](https://docs.kiwifs.com/concepts/multi-space.md) mode, the only space that accepts this key |
| `prefix` | none | Restrict the key to paths under this prefix |
| `actor` | — | Name recorded as `X-Actor` in git commits and the audit log |

> [!NOTE]
>
> A key with no `scope` gets `admin`. `kiwifs token create` defaults to `read`, so set `scope` explicitly when you write entries by hand.

### Scopes

| Scope | Can do |
| - | - |
| `read` | `GET` and `HEAD` requests only. Any other method returns `403`. |
| `write` | All read and write requests |
| `admin` | Everything |

### Path prefixes

A `prefix` limits every request that names a `path`, including each file in a `POST /api/kiwi/bulk` body:

```toml .kiwi/config.toml theme={null}
[[auth.api_keys]]
key = "kiwi_rw_..."
actor = "onboarding-bot"
scope = "write"
prefix = "onboarding/"
```

This key can read and write `onboarding/welcome.md`. A request for `?path=pages/secret.md` returns `403 path outside token prefix scope`.

## Manage tokens with the CLI

`kiwifs token` edits `[[auth.api_keys]]` in `.kiwi/config.toml`. If `[auth] type` is unset or `none`, `token create` switches it to `perspace`.

**1. Create a token**

```bash Terminal theme={null}
kiwifs token create --root ./knowledge --scope write --prefix docs/ --actor docs-bot
```

```text Output theme={null}
Token created successfully. Save this key — it will not be shown again.

  Key:    kiwi_rw_3f9c...
  Scope:  write
  Prefix: docs/
  Actor:  docs-bot

Use: Authorization: Bearer kiwi_rw_3f9c...

Reload the server (SIGHUP or restart) for the new token to take effect.
```

Keys start with `kiwi_ro_`, `kiwi_rw_`, or `kiwi_adm_` depending on scope. Flags: `--space`, `--scope` (default `read`), `--prefix`, `--actor` (default `<scope>-token`), `--root`.

**2. Reload the server**

```bash Terminal theme={null}
kill -HUP $(pgrep -f "kiwifs serve")
```

`SIGHUP` reloads `[auth]` from disk without dropping in-flight requests. Port and storage changes still need a restart.

**3. List and revoke**

```bash Terminal theme={null}
kiwifs token list --root ./knowledge
kiwifs token revoke kiwi_rw_3f9c --root ./knowledge
```

`list` shows the first 12 characters of each key with its scope, space, actor, and prefix. `revoke` removes every key that starts with the prefix you give. Send `SIGHUP` again to make the revocation take effect.

> [!WARNING]
>
> KiwiFS doesn't watch `config.toml` for auth changes. Created and revoked tokens only take effect after `SIGHUP` or a restart.

## Public and unlisted spaces

Set `[space] visibility` to open read access without a token:

| Visibility | Unauthenticated access |
| - | - |
| `private` (default) | None |
| `unlisted` | `GET` of a single file by path (`/file`, `/peek`, `/section`). Tree, search, and listings still need a token. |
| `public` | All `GET` requests |

Writes always need a token. Change visibility at runtime with `PUT /api/kiwi/space/visibility`.

## Raw files and llms.txt

Two routes sit outside `/api/kiwi` so browsers and crawlers can reach them. When auth is on:

* **`/raw/{path}`** serves a file only with an `Authorization` header (checked against the key's space and prefix), a signed URL, or when the file is already public: a page with `visibility: public` or `published: true`, an asset next to such a page, or the configured logo and favicon.
* **`/llms.txt`** and **`/llms-full.txt`** list only public or published pages, unless the whole space is `public`.

`<img>` and `<video>` tags can't send headers, so get a signed URL instead. The web UI does this automatically.

```bash Terminal theme={null}
curl 'http://localhost:3333/api/kiwi/raw-sign?path=assets/diagram.png' \
  -H "Authorization: Bearer $KIWI_TOKEN"
```

```json Response theme={null}
{
  "path": "assets/diagram.png",
  "url": "/raw/assets/diagram.png?exp=1792400400&sig=...",
  "expires": "2026-10-19T15:00:00Z"
}
```

Signed URLs last 30 to 60 minutes and stop working when the server restarts.

## Other protocols

REST honors every auth mode. The other surfaces check only the global `api_key`:

| Surface | Auth |
| - | - |
| REST `/api/kiwi/*` | All modes |
| MCP over HTTP (`/mcp`, or `kiwifs mcp --http`) | Bearer `api_key` in `apikey` mode |
| S3, WebDAV | Bearer (WebDAV also Basic) `api_key` in `apikey` mode |
| NFS | IP allowlist only (`--nfs-allow`) |

> [!WARNING]
>
> With `perspace` or `oidc`, the MCP HTTP endpoint, S3, and WebDAV don't require a token. Don't enable `--s3` or `--webdav` on a public network in those modes, and keep `/mcp` behind a proxy that authenticates.

To point the MCP bridge or a FUSE mount at a protected server, pass a token:

```bash Terminal theme={null}
kiwifs mcp --remote https://wiki.example.com --api-key "$KIWI_TOKEN"
kiwifs mount --remote https://wiki.example.com --bearer "$KIWI_TOKEN" ~/wiki
```

## CORS

With auth off, the API only answers cross-origin browser requests from `localhost`. With auth on, any origin is allowed until you list the ones you trust:

```toml .kiwi/config.toml theme={null}
[server.cors]
allowed_origins = ["https://app.example.com"]
```

## Related

> [card] [Configuration](https://docs.kiwifs.com/configuration.md)
>
> Every `config.toml` key.

> [card] [API overview](https://docs.kiwifs.com/api/overview.md)
>
> Headers and error codes.

> [card] [Multi-space](https://docs.kiwifs.com/concepts/multi-space.md)
>
> Serve several knowledge bases from one server.

> [card] [Publishing](https://docs.kiwifs.com/concepts/publishing.md)
>
> Make individual pages public.


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