API and Architecture
Understand Bunko's Next.js 16 and SQLite WAL/FTS5 architecture, central domain registries, Bearer token automation, and core HTTP/SSE endpoints.
Bunko is a single-process Next.js 16 (App Router) service running on Node.js 22 with SQLite (WAL + FTS5) persistence and an embedded durable background worker.
Runtime architecture
- Server-only integrations — All route handlers execute in the Node.js runtime (
src/app/api/**). The browser communicates exclusively throughsrc/lib/client/api.tsusing the browser-safe contract insrc/types/shared.ts; upstream clients and credentials never enter the client bundle. - SQLite WAL + FTS5 —
/data/accounts.dbstores global account metadata, permission restrictions, media-root reservations, and shared catalog keys. Each user gets an isolated SQLite database under/data/users/<id>/usingWAL,synchronous=NORMAL, a 16 MiB page cache (cache_size=-16384),temp_store=MEMORY, a 5-second busy timeout, andFTS5full-text search (current tenant schemav35). - Tenant scoping —
api.route()resolves the authenticated user, enters anAsyncLocalStoragetenant context, and executes the handler. Helpers such asdb()anddataDir()throw outside a valid tenant context. - Durable background worker — Started via
instrumentation.ts(disable withBUNKO_WORKER=false; configure idle interval withBUNKO_WORKER_IDLE_MS, default30000ms). Uses per-tenant SQLite leases (10-second renewal, 90-second expiry) to process CBZ imports, Kavita/Jellyfin/Stash index syncs, TraDoc translation polling, cover synchronization, and release checks across up to two tenants concurrently.
Central domain registries
Views and backend pipelines never hard-code lists of media kinds, providers, or services — they derive everything from three registries:
| Registry | File | What it defines |
|---|---|---|
| Kinds | src/domain/kinds.ts | All 13 media kinds (KIND_IDS), family (reading/video), progress unit, catalog/release sources, reader direction, and inKindScope() visibility logic. |
| Providers | src/domain/providers.ts | All 11 catalog providers + 5 server providers (PROVIDERS), <provider>:<key> regex validation, and ext:<addon>:<meta> extension IDs. |
| Integrations | src/domain/integrations.ts + src/lib/integrations.ts | All 9 integrations (suwayomi, kavita, jellyfin, stash, sonarr, radarr, prowlarr, qbittorrent, tradoc), categories, settings pages, ownership marks, and status checks. |
Automation with Bearer tokens
Scripts and external tools authenticate using the server's 0600 Bearer token stored at /data/access.token (bound to account 1 by default):
TOKEN="$(docker compose exec -T bunko cat /data/access.token)"
curl -fsS -H "Authorization: Bearer $TOKEN" http://127.0.0.1:2512/api/trackedAPI rules to keep in mind:
- Responses return the typed JSON payload directly (no outer
{ data: ... }wrapper). - Errors return
{ "error": { "code": "...", "message": "..." } }. - Mutating endpoints (
POST,PUT,PATCH,DELETE) requireContent-Type: application/json(1 MiB max body size). Never pass tokens in query strings. - Pass
Accept-Language: en|fr|es|de|iton HTTP calls (or?uiLanguage=on SSEEventSourceURLs) to control foreground source ordering.
Core HTTP and SSE endpoints
| Method & path | Auth | Purpose |
|---|---|---|
GET /api/health | Public | Health check returning { status: "ok", version: "1.0.0" } (503 on SQLite failure). |
GET/POST/DELETE /api/auth | Public / Session | Session state, login, and logout (POST /api/auth/setup for first start; POST /api/auth/password for password change). |
GET/POST/PATCH/DELETE /api/users | Admin | Multi-user account and permission management. |
GET/PATCH /api/settings | Auth | Read and update tenant connections, library targets, and preferences. |
GET /api/status | Auth | Live connection health and version checks for configured integrations. |
GET /api/search · GET /api/search/stream | Auth | Progressive catalog search (fast=1) and live SSE deep search across Suwayomi sources. |
GET /api/discover · GET /api/home · GET /api/planning · GET /api/news | Auth | Filtered catalog discovery, home shelves, weekly calendar, and news/RSS feed. |
GET/POST/PATCH/DELETE /api/tracked | Auth | Personal library CRUD, ratings, status, manual links, display title, and volume declarations. |
GET/POST/PATCH/DELETE /api/lists · GET /api/rankings | Auth | Custom lists, ranked lists, tier lists, ordered favorites, and automatic rankings. |
GET /api/reader/chapter · GET /api/reader/page · PUT /api/reader/progress | Auth | Built-in CBZ chapter/volume reader and reading position sync. |
GET /api/books · GET /api/books/manifest · PUT /api/books/progress | Auth | Built-in EPUB and PDF book reader endpoints. |
GET /api/downloads · GET /api/downloads/stream · POST /api/downloads/enqueue | Auth | Local CBZ download queue snapshots, SSE updates, batch enqueue, and controls. |
GET/PATCH/POST /api/unlinked | Auth | Library › Unlinked inbox for Kavita, Jellyfin/Emby, and Stash. |
GET/POST /api/translations · POST /api/translations/control | Auth | TraDoc book translation jobs and lifecycle actions. |
POST /api/import/yamtrack | Auth | Asynchronous Yamtrack CSV library import. |
Full contract in the product repository
Every request schema and response type is validated with Zod (src/lib/validation.ts) and documented alongside the source code in github.com/lucas-lepajollec/bunko.