# Run kontod

> Configure and run the kontod service: database, review key, tokens and roles, the creditor's invoicing profile, tenancy and how bookings are persisted.

kontod is konto as an HTTP service: the bill, its documents and delivery, the ledger,
collections and the account screen, for one or more creditors.

## Configure

kontod reads its configuration from the environment and refuses to start, in German, when
something is missing or invalid.

| Variable | Meaning |
|---|---|
| `KONTO_DATABASE_URL` | `sqlite://konto.db?mode=rwc` for development, `postgres://…` in production |
| `KONTO_REVIEW_KEY` | 32 bytes as 64 hex characters; signs every review |
| `KONTO_TOKENS` | JSON list of `{sha256, creditor, actor, role?}`; only the SHA-256 of each token is configured |
| `KONTO_CREDITORS` | JSON map from creditor to its invoicing profile, as invoices show it |
| `KONTO_BIND` | listen address, default `127.0.0.1:8080` |
| `KONTO_YEARS` | the years new books cover, default `2026-2030` |
| `KONTO_SIGNIN_ORIGIN` | the origin customers sign in at, `https://…` (`http://localhost` for development); customer sign-in is off without it |
| `KONTO_SIGNIN_NAME` | the name the passkey dialog shows, default „Kundenkonto“ |
| `KONTO_SIGNIN_CREDITORS` | the creditors whose customers sign in there, comma-separated; required with several creditors (see [several companies](several-creditors.md)) |
| `KONTO_SIGNIN_OIDC_ISSUER` | the operator's OpenID Connect provider, `https://…`; customers [sign in with their customer login](sign-in.md#sign-in-with-your-customer-login) |
| `KONTO_SIGNIN_OIDC_CLIENT_ID`, `KONTO_SIGNIN_OIDC_CLIENT_SECRET` | konto's confidential client at the provider; required with the issuer |
| `KONTO_SIGNIN_OIDC_NAME` | what the portal calls the login, default „Kunden-Login“ |
| `KONTO_PUBLIC_URL` | where the contract engines reach kontod, such as `https://konto.example`; an erasure then tells the engines where to confirm |

```sh
export KONTO_DATABASE_URL='sqlite://konto.db?mode=rwc'
export KONTO_REVIEW_KEY="$(openssl rand -hex 32)"
TOKEN="$(openssl rand -hex 24)"
hash() { printf %s "$1" | shasum -a 256 | cut -d' ' -f1; }
export KONTO_TOKENS="[{\"sha256\":\"$(hash "$TOKEN")\",\"creditor\":\"stadtwerke-musterstadt\",\"actor\":\"clerk:anna\"}]"
V=crates/konto-bill/tests/fixtures/household.json
export KONTO_CREDITORS="{\"stadtwerke-musterstadt\":$(jq -c .seller $V)}"
cargo run -p kontod
```

## Tokens and roles

A bearer token names exactly one creditor, one actor and one role. The role is checked
before the request body is read, so a caller learns nothing about the rules of a route it
may not use.

| Role | May |
|---|---|
| `clerk` (default) | everything for its creditor |
| `producer` | deliver charges and sections, preview an explicit bill request |
| `courier` | list deliveries, fetch a bill's PDF and XRechnung, report sent or failed |
| `portal` | read the overview, statement, items and a bill's JSON, HTML, PDF and XRechnung |

A customer who [signs in with a passkey](sign-in.md) gets a session instead of a
token, with the role `customer`: the same reads, limited to the accounts of their current
grants, and their own sharing. No token can have this role.

The [HTTP API](api.md) lists the roles of every route.

## The invoicing profile

`KONTO_CREDITORS` holds, per creditor, what its invoices state: name, address, tax number
or VAT ID, contact, electronic address, IBAN and SEPA creditor identifier. The IBAN and
creditor identifier are checked when the service starts, and every token's creditor needs
a profile. No route can change it, so a stolen token cannot redirect payments.

## Tenancy

Each creditor is a tenant with its own books, series, inbox and deliveries. The token
decides the tenant; no route takes a creditor from the caller. An account of another
creditor answers exactly like one that does not exist.

## Persistence

kontod uses portable SQL through `sqlx`: SQLite for development and tests, PostgreSQL in
production. Every booking (seal, receive, allocate) is stored as a command together with
the journal head after it, and committed in one transaction with everything it causes:
the bill, the series advance, the used review, the charges it takes from the inbox and
the bill's delivery. If the commit fails, the books in memory are rebuilt from the
database.

On first use, a creditor's books are rebuilt by replaying its commands through the same
pure functions; every journal head must equal the stored one, or the creditor answers
`503 integrity_failure`. This finds a changed or lost command. The heads are plain
digests, so a writer who rewrites a head with its command is not caught; restrict write
access to the database accordingly.

Several instances can share one database: a command's position is its primary key, so an
instance whose books are behind gets a conflict, rebuilds and can retry.

## Upgrades

The schema is versioned. At start kontod applies every migration the database lacks, in
order, each in one transaction with the record of its version in `schema_migrations`, so a
migration is applied whole or not at all. Instances starting together are safe: one
applies a migration, the others find it recorded. A database that a newer kontod has
migrated is refused at start, untouched; there is no downgrade.

Back up the database before you start a new kontod, and roll out the new version to every
instance before an old one restarts.

## Erasure

`POST /v1/accounts/{account}/erasure` with `{"basis":"request","client_reference":"…"}`
erases an account's personal data that no law requires konto to keep: the holder and the
delivery choice, mandates, the change log, unbilled charges, invitations and grants, and
the sign-in of every person left without a grant. The sealed bills, their deliveries,
the payments and the books stay. Treating an issued invoice as a Buchungsbeleg, bills are
kept eight years [AO § 147 Abs. 3 Satz 1, Abs. 1 Nr. 4] [HGB § 257 Abs. 4 Satz 1, Abs. 1
Nr. 4] and the books ten [AO § 147 Abs. 3 Satz 1, Abs. 1 Nr. 1] [HGB § 257 Abs. 4 Satz 1,
Abs. 1 Nr. 1], counted from the end of the year of the last bill or booking [AO § 147
Abs. 4] [HGB § 257 Abs. 5].

The answer is the `erasure@1` document to emit to the engines: it names every engine
contract the account had, what konto keeps and until when, and with `KONTO_PUBLIC_URL`
where to confirm. Engines confirm with `POST /v1/erasures/{id}/confirmations`; a clerk
reads them at `GET /v1/erasures/{id}`.

## One clock, for the customer's own actions

Bookings read no clock. Every date they need, issue and due dates, the day of an
allocation or assessment, comes with the request. Around midnight in Germany a server's
"today" is often wrong; a date in the request is never ambiguous and always testable.

Customers act in their own time: invitations, ceremonies, sessions and delegations expire,
a mandate given online is dated today, consent to e-mail too, and every change to access,
payment and delivery is logged with its time. kontod reads one clock for that, and for
nothing else. `AppState::with_clock`
takes another clock, so tests move time instead of waiting for it.

## Limits

| Limit | Value |
|---|---|
| request body | 16 MiB |
| charges per delivery | 1,000 |
| charges per bill | 10,000 |
| deliveries per list | 500 |
