Quick Start
This guide walks you through generating C# code from an OpenAPI specification in just a few steps.
1. Install the tool
Section titled “1. Install the tool”dotnet tool install --global Nikcio.OpenApiCodeGen2. Get an OpenAPI specification
Section titled “2. Get an OpenAPI specification”You can use any OpenAPI 3.x specification. For this example, we’ll use the Petstore sample spec:
# Download the Petstore speccurl -o petstore.json https://petstore3.swagger.io/api/v3/openapi.jsonOr you can skip downloading and generate directly from the URL (see step 3).
3. Generate C# code
Section titled “3. Generate C# code”From a local file:
openapi-codegen petstore.json -o PetStore.cs -n MyApp.ModelsOr directly from a URL:
openapi-codegen https://petstore3.swagger.io/api/v3/openapi.json -o PetStore.cs -n MyApp.Models4. Use the generated code
Section titled “4. Use the generated code”The generated file contains ready-to-use C# records. Add it to your project and start using the types:
using System.Net.Http.Json;using MyApp.Models;
var client = new HttpClient { BaseAddress = new Uri("https://petstore3.swagger.io/api/v3/") };
// The generated Pet record works directly with System.Text.JsonPet? pet = await client.GetFromJsonAsync<Pet>("pet/1");
Console.WriteLine($"Pet: {pet?.Name} (Status: {pet?.Status})");5. Explore the options
Section titled “5. Explore the options”Customize the output with CLI flags:
# Use mutable collections instead of IReadOnlyList<T>openapi-codegen spec.yaml -o Models.cs --mutable-arrays
# Disable doc comments for a cleaner outputopenapi-codegen spec.yaml -o Models.cs --no-doc-commentsWhat’s Next?
Section titled “What’s Next?”- CLI Usage — Learn all the CLI commands and patterns
- Configuration — Understand all available options