Developers

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 through src/lib/client/api.ts using the browser-safe contract in src/types/shared.ts; upstream clients and credentials never enter the client bundle.
  • SQLite WAL + FTS5 — /data/accounts.db stores global account metadata, permission restrictions, media-root reservations, and shared catalog keys. Each user gets an isolated SQLite database under /data/users/<id>/ using WAL, synchronous=NORMAL, a 16 MiB page cache (cache_size=-16384), temp_store=MEMORY, a 5-second busy timeout, and FTS5 full-text search (current tenant schema v35).
  • Tenant scoping — api.route() resolves the authenticated user, enters an AsyncLocalStorage tenant context, and executes the handler. Helpers such as db() and dataDir() throw outside a valid tenant context.
  • Durable background worker — Started via instrumentation.ts (disable with BUNKO_WORKER=false; configure idle interval with BUNKO_WORKER_IDLE_MS, default 30000 ms). 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:

RegistryFileWhat it defines
Kindssrc/domain/kinds.tsAll 13 media kinds (KIND_IDS), family (reading/video), progress unit, catalog/release sources, reader direction, and inKindScope() visibility logic.
Providerssrc/domain/providers.tsAll 11 catalog providers + 5 server providers (PROVIDERS), <provider>:<key> regex validation, and ext:<addon>:<meta> extension IDs.
Integrationssrc/domain/integrations.ts + src/lib/integrations.tsAll 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/tracked

API 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) require Content-Type: application/json (1 MiB max body size). Never pass tokens in query strings.
  • Pass Accept-Language: en|fr|es|de|it on HTTP calls (or ?uiLanguage= on SSE EventSource URLs) to control foreground source ordering.

Core HTTP and SSE endpoints

Method & pathAuthPurpose
GET /api/healthPublicHealth check returning { status: "ok", version: "1.0.0" } (503 on SQLite failure).
GET/POST/DELETE /api/authPublic / SessionSession state, login, and logout (POST /api/auth/setup for first start; POST /api/auth/password for password change).
GET/POST/PATCH/DELETE /api/usersAdminMulti-user account and permission management.
GET/PATCH /api/settingsAuthRead and update tenant connections, library targets, and preferences.
GET /api/statusAuthLive connection health and version checks for configured integrations.
GET /api/search · GET /api/search/streamAuthProgressive catalog search (fast=1) and live SSE deep search across Suwayomi sources.
GET /api/discover · GET /api/home · GET /api/planning · GET /api/newsAuthFiltered catalog discovery, home shelves, weekly calendar, and news/RSS feed.
GET/POST/PATCH/DELETE /api/trackedAuthPersonal library CRUD, ratings, status, manual links, display title, and volume declarations.
GET/POST/PATCH/DELETE /api/lists · GET /api/rankingsAuthCustom lists, ranked lists, tier lists, ordered favorites, and automatic rankings.
GET /api/reader/chapter · GET /api/reader/page · PUT /api/reader/progressAuthBuilt-in CBZ chapter/volume reader and reading position sync.
GET /api/books · GET /api/books/manifest · PUT /api/books/progressAuthBuilt-in EPUB and PDF book reader endpoints.
GET /api/downloads · GET /api/downloads/stream · POST /api/downloads/enqueueAuthLocal CBZ download queue snapshots, SSE updates, batch enqueue, and controls.
GET/PATCH/POST /api/unlinkedAuthLibrary › Unlinked inbox for Kavita, Jellyfin/Emby, and Stash.
GET/POST /api/translations · POST /api/translations/controlAuthTraDoc book translation jobs and lifecycle actions.
POST /api/import/yamtrackAuthAsynchronous 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.