Skip to content

Quick Start โ€‹

Generate Angular services and TypeScript types from your OpenAPI specification.

Step 1: Prepare Your OpenAPI Specification โ€‹

You need an OpenAPI/Swagger specification file:

  • JSON file (swagger.json, openapi.json)
  • Yaml file (swagger.yml, openapi.yaml)

Step 2: Generate API Client โ€‹

Using Command Line โ€‹

bash
ng-openapi -i ./swagger.json -o ./src/api

Using Configuration File โ€‹

Create openapi.config.ts:

typescript
import { GeneratorConfig } from "ng-openapi";

const config: GeneratorConfig = {
    input: "./swagger.json",
    output: "./src/api",
    options: {
        dateType: "Date",
        enumStyle: "enum",
    },
};

export default config;

Then run:

bash
ng-openapi -c openapi.config.ts

Step 3: Configure Your Angular App โ€‹

Add the provider to your app.config.ts:

typescript
import { ApplicationConfig } from "@angular/core";
import { provideHttpClient } from "@angular/common/http";
import { provideDefaultClient } from "./api/providers";

export const appConfig: ApplicationConfig = {
    providers: [
        provideHttpClient(),
        provideDefaultClient({
            basePath: "https://api.example.com",
        }),
    ],
};

Step 4: Use Generated Services โ€‹

typescript
import { inject } from "@angular/core";
import { toSignal } from "@angular/core/rxjs-interop";
import { PetsService } from "./api/services";
import { Pet } from "./api/models";

export class PetsComponent {
    private readonly petsService = inject(PetsService);
    readonly pets = toSignal(this.petsService.listPets());
}

Generated Structure โ€‹

After generation, you'll have:

src/api/
โ”œโ”€โ”€ models/               # TypeScript interfaces, enums
โ”œโ”€โ”€ services/             # One Angular service per controller
โ”œโ”€โ”€ tokens/               # Injection tokens
โ”œโ”€โ”€ utils/                # Date transformer, download helpers, โ€ฆ
โ”œโ”€โ”€ providers.ts          # provideDefaultClient() setup function
โ””โ”€โ”€ index.ts              # Main exports

See Generated Output for what every file does.

Next Steps โ€‹

Released under the MIT License.
This site is powered by Netlify
About ยท Impressum