Skip to content

Quick Start

This guide walks you through generating C# code from an OpenAPI specification in just a few steps.

Terminal window
dotnet tool install --global Nikcio.OpenApiCodeGen

You can use any OpenAPI 3.x specification. For this example, we’ll use the Petstore sample spec:

Terminal window
# Download the Petstore spec
curl -o petstore.json https://petstore3.swagger.io/api/v3/openapi.json

Or you can skip downloading and generate directly from the URL (see step 3).

From a local file:

Terminal window
openapi-codegen petstore.json -o PetStore.cs -n MyApp.Models

Or directly from a URL:

Terminal window
openapi-codegen https://petstore3.swagger.io/api/v3/openapi.json -o PetStore.cs -n MyApp.Models

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.Json
Pet? pet = await client.GetFromJsonAsync<Pet>("pet/1");
Console.WriteLine($"Pet: {pet?.Name} (Status: {pet?.Status})");

Customize the output with CLI flags:

Terminal window
# Use mutable collections instead of IReadOnlyList<T>
openapi-codegen spec.yaml -o Models.cs --mutable-arrays
# Disable doc comments for a cleaner output
openapi-codegen spec.yaml -o Models.cs --no-doc-comments