komga-sdk keeps transport and generated API calls small. Put application policy around the client rather than relying on implicit middleware.
Reuse one configured client
Create a client at your application boundary and pass it to operations:lib/komga.ts
books.ts
Keep credentials out of source
Use environment variables or a secrets manager:.env
Authorization, X-API-Key, cookies, and Set-Cookie before logging requests or responses.
Choose a result boundary
Generated functions are nonthrowing by default. Handle the result locally when a failed request is an expected branch:unwrap when your application boundary is exception-based. It throws KomgaApiError for an error result and preserves the response status and body:
unwrap, set generated throwOnError, or add an application policy yourself. Keep one policy at each boundary instead of mixing result checks and broad try/catch blocks in every caller.
Paginate deliberately
Use a page size that fits the operation and memory budget. Keep the requiredbookSearch field in every getBooks call:
unpaged: true only for bounded datasets. For large batch jobs, process one page at a time, use a stable sort, and add a delay or concurrency limit in your application when the server needs it. Avoid changing filters or sort order during a multi-page export; doing so can duplicate or skip records.
Make batch work observable and safe
Mutations can return204 No Content, so success is indicated by error === undefined, not by a value in data:
Retry-After for rate limits and cap exponential backoff rather than issuing an unbounded burst.
A small application-owned wrapper can handle transient result failures without pretending the SDK has a retry policy:
Add policies outside the SDK
The core client uses native Fetch and has no retries, caching, timeout policy, or debug logger. If your application needs a policy, make it visible at the boundary:AbortSignal through generated options:
Cache only stable, bounded data
Caching belongs to the application, not the generated client:Treat generated types as compile-time types
Generated types describe the API contract to TypeScript. They do not validate data at runtime:any; use the generated DTO, page, search, response, and error types or narrow unknown at an explicit boundary.
Respect permissions and the runtime
Generated operations carry Komga’s operation documentation, including required roles such asADMIN or FILE_DOWNLOAD. A 403 is a permission result, not a reason to retry unchanged credentials. Check the account’s library sharing and operation role before scheduling a batch job.
The SDK requires a runtime with Fetch, Headers, Request, Response, TextEncoder, and Web Crypto support. Node.js 18+, modern browsers, and compatible edge runtimes provide these APIs. Pass fetch explicitly when the host runtime supplies a custom implementation.