TypeScript  

How to Generate TypeScript Models from ASP.NET Core DTOs Using OpenAPI

When building a React frontend on top of ASP.NET Core Web APIs, one of the biggest risks is model duplication.

You define DTOs in C#.

Then you manually recreate those same models in TypeScript.

That duplication creates drift.

Over time, the backend changes and the frontend silently breaks.

The professional solution is simple:

Generate your TypeScript models directly from your ASP.NET Core OpenAPI specification.

Let’s walk through how.

🧠 Step 1: Enable OpenAPI in ASP.NET Core

In your ASP.NET Core project, install Swagger support:

dotnet add package Swashbuckle.AspNetCore

Then configure it in Program.cs:

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

And enable middleware:

app.UseSwagger();
app.UseSwaggerUI();

Now your API exposes an OpenAPI specification at:

https://localhost:5001/swagger/v1/swagger.json

This JSON file describes your entire API contract.

This becomes the source of truth.

πŸ”§ Step 2: Choose a TypeScript Generation Tool

There are two widely used tools in .NET ecosystems:

NSwag

OpenAPI Generator

Both can generate:

β€’ TypeScript interfaces

β€’ API client classes

β€’ Strongly typed service methods

For ASP.NET Core teams, NSwag integrates very well.

πŸ›  Option 1: Using NSwag

Install NSwag CLI:

dotnet tool install --global NSwag.ConsoleCore

Create a configuration file:

{
  "runtime": "Net70",
  "documentGenerator": {
    "fromDocument": {
      "url": "https://localhost:5001/swagger/v1/swagger.json"
    }
  },
  "codeGenerators": {
    "openApiToTypeScriptClient": {
      "className": "ApiClient",
      "template": "Fetch",
      "typeScriptVersion": 4.7,
      "generateClientClasses": true,
      "generateDtoTypes": true,
      "output": "src/api/generated.ts"
    }
  }
}

Run:

nswag run

This generates:

β€’ All DTO interfaces

β€’ Strongly typed API client

β€’ Typed request and response models

Now your React frontend uses the same contract as your backend.

No manual duplication.

πŸ›  Option 2: Using OpenAPI Generator

Install OpenAPI Generator:

npm install @openapitools/openapi-generator-cli -g

Generate TypeScript models:

openapi-generator-cli generate \
  -i https://localhost:5001/swagger/v1/swagger.json \
  -g typescript-fetch \
  -o src/api

This creates:

β€’ Interfaces

β€’ Typed API service functions

β€’ Fetch based client

It works well with React applications.

πŸ— Example: Before and After

Without generation, you manually define:

interface OrderDto {
  id: number;
  total: number;
  createdDate: string;
}

If backend changes, this may become outdated.

With generation:

The model is created automatically from:

public class OrderDto
{
    public int Id { get; set; }
    public decimal Total { get; set; }
    public DateTime CreatedDate { get; set; }
}

If the backend changes, regenerate types.

Frontend updates automatically.

πŸ”„ Automating Generation

For professional projects:

Add a script in package.json:

"scripts": {
  "generate-api": "nswag run"
}

Run generation:

npm run generate-api

You can also:

β€’ Integrate generation into CI pipelines

β€’ Regenerate on every backend change

β€’ Fail builds if API contract changes

This enforces strict backend frontend alignment.

βš™ Best Practices for Enterprise Projects

Generate types into a dedicated folder like:

src/api/generated

Do not manually edit generated files.

Create a thin wrapper layer if needed.

Regenerate whenever:

β€’ DTOs change

β€’ Endpoints are added

β€’ Properties are renamed

Treat OpenAPI as your contract boundary.

πŸš€ Why This Matters in ASP.NET Core

ASP.NET Core is strongly typed.

If your React frontend is TypeScript and generated from OpenAPI:

You now have:

Strong backend

Strong frontend

Strong integration contract

This dramatically reduces:

β€’ Integration bugs

β€’ Runtime surprises

β€’ Deployment failures

β€’ Refactoring risk

For serious enterprise applications, this is a best practice.

🎯 Final Answer

How do you generate TypeScript models from ASP.NET Core DTOs?

  1. Enable Swagger or OpenAPI in ASP.NET Core

  2. Use NSwag or OpenAPI Generator

  3. Generate TypeScript interfaces and clients

  4. Automate regeneration

  5. Treat OpenAPI as the contract source

This approach eliminates model duplication, protects against API drift, and creates a professional grade full stack architecture.

In large ASP.NET Core plus React systems, this is not optional.

It is foundational.