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/apiUsing 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.tsStep 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 exportsSee Generated Output for what every file does.
Next Steps โ
- Configuration Reference โ all generator options at a glance
- Angular Integration โ providers, interceptors, environments
- Multiple Clients โ several APIs in one app
- Schema Validation โ validate responses at runtime