Building Type-Safe APIs with OpenAPI and Code Generation
How code generation from OpenAPI specs can eliminate runtime errors and improve developer productivity.
January 15, 2025
8 min read
TypeScriptOpenAPIDX
The Problem with Manual API Typing
When working with REST APIs in TypeScript, we often manually create types based on API documentation. This approach has several issues:
Enter Code Generation
By generating TypeScript types directly from OpenAPI specifications, we can:
Our Approach at Semfi
At Semfi, we built a custom NX plugin that:
// Generated code example
export const useGetUsers = (options?: UseQueryOptions) => {
return useQuery({
queryKey: ['users'],
queryFn: () => api.getUsers(),
...options,
});
};Results
After implementing this approach:
Getting Started
Check out my open-source plugin openapi-ts-nx-plugin to get started with automatic code generation in your NX monorepo.