CLI Usage โ
Generate API clients using the ng-openapi command line interface. This page covers day-to-day workflows; the complete flag list lives in the CLI reference.
Basic Commands โ
Direct Generation โ
bash
ng-openapi -i swagger.json -o ./src/apiThe input can also be a URL:
bash
ng-openapi -i https://api.example.com/openapi.yaml -o ./src/apiConfiguration File โ
bash
ng-openapi -c openapi.config.tsGenerate Subcommand โ
bash
ng-openapi generate -i swagger.json -o ./src/api
ng-openapi gen -c openapi.config.ts # Short aliasCommon Options โ
Types Only โ
bash
ng-openapi -i swagger.json -o ./src/api --types-onlyString Dates โ
bash
ng-openapi -i swagger.json -o ./src/api --date-type stringConfiguration vs CLI โ
CLI flags cover the quick cases; everything else (headers, plugins, method naming, validation, โฆ) needs a configuration file:
typescript
// openapi.config.ts
import { GeneratorConfig } from "ng-openapi";
const config: GeneratorConfig = {
input: "./swagger.json",
output: "./src/api",
options: {
dateType: "Date",
enumStyle: "enum",
customHeaders: { "X-API-Key": "key" },
responseTypeMapping: { "application/pdf": "blob" },
},
};
export default config;bash
ng-openapi -c openapi.config.tsWorkflow Recipes โ
Generate Before Serving/Building โ
json
{
"scripts": {
"generate:client": "ng-openapi -c openapi.config.ts",
"dev": "npm run generate:client && ng serve",
"prebuild": "npm run generate:client",
"build": "ng build"
}
}Regenerate on Spec Changes โ
json
{
"scripts": {
"generate:watch": "nodemon --watch swagger.json --exec 'npm run generate:client'"
}
}Fetch the Spec First โ
json
{
"scripts": {
"fetch:spec": "curl https://api.example.com/swagger.json > swagger.json",
"generate:client": "npm run fetch:spec && ng-openapi -c openapi.config.ts"
}
}Alternatively, point input directly at the URL and use validateInput to guard against unexpected spec changes.
Multiple APIs โ
json
{
"scripts": {
"generate:users": "ng-openapi -c users-api.config.ts",
"generate:orders": "ng-openapi -c orders-api.config.ts",
"generate:all": "npm run generate:users && npm run generate:orders"
}
}See Multiple Clients for the full setup.
Help and Version โ
bash
ng-openapi --help
ng-openapi generate --help
ng-openapi --versionResources โ
- CLI Reference โ all flags and defaults
- Configuration Reference โ all config-file properties
- Generated Output โ what lands in your output directory