Prompt
How do I generate strongly typed API clients?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To generate strongly typed API clients, the usual approach is:
-
Describe the API with a machine-readable contract
- Most commonly:
- OpenAPI/Swagger for REST APIs
- gRPC/protobuf for RPC APIs
- GraphQL schema for GraphQL APIs
- Most commonly:
-
Use a code generator
- The generator reads the contract and emits client code with:
- typed request/response models
- typed method signatures
- enums / discriminated unions where supported
- validation helpers in some generators
- The generator reads the contract and emits client code with:
Common options by API style
REST APIs
OpenAPI is the standard.
Popular generators:
- OpenAPI Generator
- Swagger Codegen
- NSwag (.NET, TypeScript)
- Kiota (Microsoft, multi-language)
- orval (TypeScript/React Query)
- openapi-typescript (TS types only)
- openapi-fetch (typed fetch client for TS)
Example workflow:
- Write or export
openapi.yaml - Run generator
- Import generated client into your app
Example:
openapi-generator-cli generate -i openapi.yaml -g typescript-fetch -o ./client
This can produce code like:
const api = new PetApi();
const pets = await api.listPets();
gRPC APIs
Use protobuf definitions.
Generate strongly typed clients with:
protoc- language-specific plugins
Example:
protoc --ts_out=./client --plugin=protoc-gen-ts=...
This gives you typed request/response messages and service stubs.
GraphQL APIs
Use the GraphQL schema and operations.
Popular tools:
- GraphQL Code Generator
- Apollo codegen
These generate:
- typed query/mutation result types
- typed variables
- typed hooks in frontend frameworks
Example:
graphql-codegen
Best practices for strong typing
- Keep the API contract as the source of truth
- Generate models from the contract, rather than hand-writing them
- Version your API schema
- Use enums and oneOf/anyOf carefully for better type narrowing
- Add runtime validation if you need safety beyond compile-time types
- Regenerate clients in CI when the contract changes
If you’re using TypeScript
The most common choices are:
- openapi-typescript → generates types from OpenAPI
- openapi-fetch → lightweight typed HTTP client
- orval → generates clients + hooks
- GraphQL Code Generator → for GraphQL
Quick recommendation
- REST + TypeScript:
openapi-typescriptororval - REST + many languages:
OpenAPI Generator - gRPC:
protoc - GraphQL:
GraphQL Code Generator
If you want, I can show you a step-by-step example for your stack, like:
- TypeScript + REST
- Python
- Java/Kotlin
- C#
- Go