How functional API calls work with the Api service
masterBy default (since version 1.0), ng-openapi-gen generates individual functions for each API operation and provides a single @Injectable service (named Api by default, configurable via apiService) to invoke them. This approach is more tree-shakeable for large APIs because only the specific functions you import and use will be bundled.
To use this pattern, inject the Api service and use its .invoke() method, passing the generated operation function as the first argument.
import { Component, inject, OnInit, signal } from '@angular/core';
import { Api } from './api/api';
import { getResults } from './api/fn/operations/get-results';
import { Result } from './api/models';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
templateUrl: './app.html',
styleUrl: './app.css',
})
export class App implements OnInit {
protected readonly results = signal<Result[] | null>(null);
private api = inject(Api);
async ngOnInit() {
this.results.set(await this.api.invoke(getResults, { limit: 5 }));
}
}