Advertisement
Advanced ASP.NET Core, API Documentation with Swagger/OpenAPI

How would you generate a client SDK automatically from an ASP.NET Core API's OpenAPI specification?

Short answer

Tools like NSwag or the openapi-generator CLI consume the generated OpenAPI JSON document and produce strongly-typed client code (in C#, TypeScript, or many other languages) with methods matching each endpoint, request/response DTOs matching the schemas, and even authentication…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Tools like NSwag or the openapi-generator CLI consume the generated OpenAPI JSON document and produce strongly-typed client code (in C#, TypeScript, or many other languages) with methods matching each endpoint, request/response DTOs matching the schemas, and even authentication handling -- automating what would otherwise be tedious, error-prone, hand-written HTTP client code, and keeping the client in sync with the API contract as it evolves by regenerating whenever the spec changes.
Example

# Using NSwag CLI to generate a TypeScript client nswag openapi2tsclient /input:https://api.example.com/swagger/v1/swagger.json /output:api-client.ts # Or a C# client nswag openapi2csclient /input:swagger.json /output:ApiClient.cs /namespace:MyApp.Client

Real-world example
A frontend team generates a fully-typed TypeScript API client directly from the backend team's published OpenAPI spec as part of their build process, automatically catching compile-time errors if the frontend uses an API contract that no longer matches the backend.
Likely follow-ups
  • How do you automate client regeneration as part of a CI/CD pipeline when the API contract changes?
  • What's the risk of manually hand-writing HTTP client code instead?
Related topics