· 2 min read
Building an OpenAPI Code Generation Pipeline with Kubb
The dashboard I worked on consumed multiple APIs, all with OpenAPI specs that changed frequently. Keeping hand-written TypeScript types in sync was unsustainable, so I built a code generation pipeline to automate it.
Why Kubb
I evaluated several tools before landing on Kubb. It won out because it generates both TypeScript types and Zod schemas from the same spec in a single pass, and its plugin system made it easy to customize the output to match the project’s naming conventions.
Preprocessing the Specs
The specs needed some cleanup before generation. Some had inconsistencies that would produce incorrect validators. I added a preprocessing step that normalizes the input before every generation, ensuring the output is always clean regardless of upstream changes.
The Automated Pipeline
A CI workflow runs on a scheduled cadence. It fetches the latest specs, runs the full generation pipeline, type-checks the output, and diffs against the previous snapshot. If there are breaking changes, it notifies the team with a summary of what changed.
This gives the team early warning before breaking changes surface as type errors in development.
Impact
The pipeline eliminated an entire class of integration bugs. Engineers trust that the generated types match the API contract. Code reviews became faster because reviewers don’t need to verify type accuracy manually. The pipeline guarantees it.