# Self-hosting

Kokumi is MIT open source and runs on Cloudflare Workers plus D1. It never auto-upgrades your Cloudflare plan and surfaces free-tier limits loudly via `kokumi health`.

## 1. Create D1 and set Worker secrets

Create a D1 database, apply `schema.sql`, and generate two random secrets: `READ_TOKEN` (protects `/api/v1/*`) and `VISITOR_SALT` (rotates daily SHA-256 visitor keys).

```sh
wrangler d1 create kokumi
wrangler d1 execute kokumi --remote --file schema.sql
openssl rand -hex 32 | wrangler secret put READ_TOKEN
openssl rand -hex 32 | wrangler secret put VISITOR_SALT
wrangler deploy
```

> Scope: MUTATE | Expect: exit 0 (Worker URL printed and D1 schema applied)

## 2. Cloudflare vs external CMS (progressive enhancement)

Putting Kokumi on a Cloudflare route in front of your origin unlocks edge superpowers: verified AI crawler detection, `.md` twin counts, `llms.txt` tracking, and same-origin `<script src="/visit.js" defer></script>`.

On non-Cloudflare hosts (Shopify, Squarespace, Wix, WordPress), serve `/visit.js` from your Worker with `data-endpoint`. CORS headers default to `*` or your configured `ALLOWED_ORIGINS` allowlist.

```html
<script src="https://analytics.example.com/visit.js" data-endpoint="https://analytics.example.com" defer></script>
```

> Scope: EMBED | Vars: analytics.example.com | Expect: HTTP 204 (Beacon hits /api/hit and /api/engaged)

## 3. Initialize the CLI and monitor free limits

Use `kokumi init`, `kokumi snippet`, and `kokumi verify` to confirm live beacons. If daily D1 writes or Workers requests pass 75% or 95% of Cloudflare's free plan (100k writes/day, 100k requests/day, 500 MB DB), `kokumi health` exits with code `5`.

## Upgrading an existing database

All migrations in `migrations/` are idempotent. If you are bringing Umami or Plausible history with export drift, see `SCHEMA-UPDATE.md` in the repository.

## Related

- [Quickstart](/docs/quickstart)
- [CLI reference](/docs/cli)
- [Worker API reference](/docs/api)