Every frontend team eventually pays for API drift: an endpoint string changes in one screen, a request type is copied into another, and each developer builds a slightly different query hook around the same backend operation.
I wanted to change the default. In this codebase, the backend’s OpenAPI document is the contract; Kubb turns that contract into generated TypeScript types, Fetch clients, and TanStack Query option factories and keys. Feature code then composes those generated artifacts through a shared server and query layer, so developers follow one discoverable pattern instead of rebuilding API plumbing for every feature.
The change: from manual wiring to a shared workflow#
Before code generation, each feature could hand-maintain endpoint paths, request/response shapes, and fetching behavior. Those copies were easy to create and hard to keep aligned. The real cost was not typing the URL once; it was every team member having to remember the right conventions and re-check them whenever the API changed.
I set up Kubb as a repeatable pipeline:
Backend OpenAPI document → Kubb → generated types, clients, and query option factories → feature server functions → shared query cache and UI hooks
The project’s kubb.config.ts reads the API document URL from environment configuration and runs @kubb/plugin-oas, @kubb/plugin-ts, @kubb/plugin-fetch, and @kubb/plugin-react-query. Their generated output is grouped under src/api/generated/ as types/, clients/, and hooks/. The team regenerates it with npm run generate:api; the package script runs Kubb and the repository’s post-generation compatibility step.
The generated hooks/ folder contains query and mutation option factories plus operation keys; it does not mean the UI calls generated useQuery hooks directly. Feature modules compose those helpers with server functions to preserve the application’s SSR and session policy. This keeps generated code centralized and predictable. Engineers consume it; they do not patch generated files by hand.
Keep generated contracts; own the application policy#
Generation solves the contract duplication problem. It does not decide where credentials live, how SSR should reach the backend, which cache entries should be invalidated, or how the app handles an expired session. Those are application architecture decisions.
In this project, backend calls stay on the TanStack Start server boundary. Feature modules use createServerFn and the shared serverFetch; endpoint URLs, request types, and response types come from Kubb’s generated output. Query options then connect that server function to TanStack Query. UI code uses shared app hooks where it needs consistent query and mutation behavior.
For example, src/modules/watchlist/watchlist-server.ts validates the create payload with Kubb’s generated body type and gets the URL from the generated mutation key:
export const createWatchlistServerFn = createServerFn({ method: "POST" })
.validator((data: WatchlistControllerCreateBody) => data)
.handler(async ({ data }): Promise<WatchlistControllerCreateStatus201> => {
return await serverFetch<WatchlistControllerCreateStatus201, WatchlistControllerCreateBody>(
watchlistControllerCreateMutationKey()[0].url,
{ method: "POST", body: data },
);
});
No manually copied endpoint path or request/response DTO is needed here. The generated contract supplies the shapes and operation key; the feature server function supplies the server-side policy and call boundary.
The same feature file defines query options around its server function. In a view such as src/modules/watchlist/index.tsx, useAppQuery from src/hooks/use-app-query.ts preserves the shared cache while applying application-wide handling for redirects, session expiry, server errors, and user feedback. useAppMutation in src/hooks/use-app-mutation.ts centralizes success/error handling and targeted query invalidation for mutation flows.
Make the pattern easy to find and repeat#
The useful architectural move was not “install a generator.” It was giving the team a path that is visible in the repository:
- ▸Contract and generator setup: kubb.config.ts
- ▸One command to refresh generated artifacts: package.json → generate:api
- ▸Generated operation types, clients, and helpers: src/api/generated/
- ▸Feature-owned server calls and query options: for example, src/modules/watchlist/watchlist-server.ts
- ▸Shared transport and session behavior: src/lib/server-fetch.ts and server session utilities
- ▸Consistent UI query/mutation policy: src/hooks/use-app-query.ts and src/hooks/use-app-mutation.ts
A new developer can follow one feature from generated operation to server function to query options to UI. Code review can focus on the actual product rule—validation, authorization, pagination, or cache invalidation—instead of re-litigating URL spelling and response typing on every screen.
What changed for the organization#
- ▸Frontend engineers get discoverable contracts and repeatable query patterns, with fewer handwritten API details to maintain.
- ▸Backend and frontend teams share the OpenAPI boundary; documented contract changes surface as TypeScript work during generation and type-checking.
- ▸Reviewers and QA can spend more attention on behavior and edge cases because the transport conventions are centralized.
- ▸The codebase gains a consistent place to add cross-cutting policy without forcing every feature to implement it independently.
That is a productivity and consistency improvement by design; I have not attached a made-up time-saved or defect-reduction number. I would measure adoption, type-check failures after contract changes, duplicate transport code, and time to implement a typical endpoint before quantifying the result.
The trade-off I keep visible#
Generated types are only as trustworthy as the OpenAPI document. Code generation does not guarantee runtime data validity, correct authorization, or a good API design. Backend authorization remains authoritative, and runtime validation is still useful at boundaries where malformed or untrusted data matters. The generator also needs to run predictably in local development and CI so generated output does not drift from the contract.
The principle is straightforward: generate what is mechanical; keep product policy explicit. Kubb removes repetitive contract plumbing, while feature modules and shared hooks make the secure, SSR-aware path clear for every developer. That gives an organization a repeatable API integration platform—not a collection of individually clever endpoint wrappers.
