Skip to content

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/api

The input can also be a URL:

bash
ng-openapi -i https://api.example.com/openapi.yaml -o ./src/api

Configuration File โ€‹

bash
ng-openapi -c openapi.config.ts

Generate Subcommand โ€‹

bash
ng-openapi generate -i swagger.json -o ./src/api
ng-openapi gen -c openapi.config.ts  # Short alias

Common Options โ€‹

Types Only โ€‹

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

String Dates โ€‹

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

Configuration 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.ts

Workflow 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 --version

Resources โ€‹

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