From OpenAPI to TypeScript
Start with an OpenAPI JSON document and generate a client for fetch or Axios. The C# example shows how to produce the document with ASP.NET Core.
Install the tool
Use the .NET 10 or .NET 11 SDK. For a repository-local installation, create a tool manifest if your project needs one, then install the package.
dotnet new tool-manifest
dotnet tool install --local MagnusOpera.OpenApiGen
Generate the client
From the tool manifest directory, pass the path to your OpenAPI JSON file and choose a transport. The ASP.NET Core example shows how to create a sample document.
dotnet tool run openapigen --transport fetch openapi.json ./generated
Regression tests cover OpenAPI 3.1 and 3.2 documents generated by ASP.NET Core 10 and 11. Compatibility with documents from other frameworks depends on the operations, schemas, and media types they use.
Use a dedicated output directory. OpenApiGen clears it before regenerating the client.
ASP.NET Core example
Create an ASP.NET Core Web API in a GreetingApi folder. Run these commands from the same directory as the tool manifest.
dotnet new webapi -o GreetingApi
Replace GreetingApi/Program.cs with a route that declares a JSON success response and a problem response. The Greetings tag becomes the generated module name.
using Microsoft.AspNetCore.Http.HttpResults;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapOpenApi();
app.MapGet("/greetings/{name}", GetGreeting)
.WithTags("Greetings")
.ProducesProblem(400);
app.Run();
static Results<Ok<Greeting>, ProblemHttpResult> GetGreeting(string name)
{
if (name.Length > 40)
return TypedResults.Problem("Name is too long.", statusCode: 400);
return TypedResults.Ok(new Greeting($"Hello, {name}!"));
}
record Greeting(string Message);
dotnet run --project GreetingApi --no-launch-profile --urls http://localhost:5050
In another terminal, download the example document from the tool manifest directory, then run the generation command.
curl -fsS http://localhost:5050/openapi/v1.json -o openapi.json
Build-time option. Add Microsoft.Extensions.ApiDescription.Server to the ASP.NET Core project to emit OpenAPI JSON during dotnet build. Set OpenApiDocumentsDirectory in the project file to choose where it goes, then pass the generated JSON directly to OpenApiGen. ASP.NET Core build-time setup
Call from TypeScript
These TypeScript examples call the greeting API above. Use Node.js 24. From the directory where you generated the client, create a package for this demo and enable ES modules.
npm init -y
npm pkg set type=module
Fetch
Save the sample as client.ts alongside generated/ and run npx tsx client.ts. Check the status to narrow the body type.
import { getGreetingsName } from "./generated/index";
const apiFetch: typeof fetch = (input, init) =>
fetch(typeof input === "string"
? new URL(input, "http://localhost:5050")
: input, init);
const [status, body] = await getGreetingsName(apiFetch, "Ada");
if (status === 200) {
console.log(body.message);
}
Axios
Install Axios and generate into a separate directory. Save the example as client-axios.ts and run npx tsx client-axios.ts.
npm install axios
dotnet tool run openapigen --transport axios openapi.json ./generated-axios
import axios from "axios";
import { getGreetingsName } from "./generated-axios/index";
const http = axios.create({ baseURL: "http://localhost:5050" });
const [status, body] = await getGreetingsName(http, "Ada");
if (status === 200) {
console.log(body.message);
}