Command Syntax
spec
Generates an OpenAPI specification file from your TypeScript controllers.What it does
Scan Controllers
Reads your TypeScript controllers starting from the entry file or using controller path globs
Extract Metadata
Analyzes decorators (
@Route, @Get, @Post, etc.) and TypeScript types to extract API metadataOptions
Path to the tsoa configuration file. Supports
.json, .yaml, .yml, .js, and .cjs formats.Override the base API path defined in configuration. This is prepended to all route paths.
Override the API host defined in configuration. Used in OpenAPI 2.0 specs.
Force YAML output format instead of JSON.
Force JSON output format instead of YAML.
Examples
Output
Creates a specification file at:dist/swagger.json
The
spec command only generates the OpenAPI specification. To also generate route handlers, use spec-and-routes.routes
Generates route handler files with built-in validation for your chosen middleware framework.What it does
Options
Path to the tsoa configuration file.
Override the base API path for route generation.
Examples
Output
Creates a routes file at:src/routes.ts
The generated file includes:
- Route registration for your middleware framework
- Request/response validation
- Type coercion and sanitization
- Authentication integration
- Error handling
spec-and-routes
Generates both OpenAPI specification and route handlers in a single pass. This is more efficient than runningspec and routes separately.
Why use this command?
Performance
Scans controllers once instead of twice, significantly faster for large projects
Consistency
Ensures spec and routes are generated from identical metadata
Simplicity
Single command for complete code generation
CI/CD Friendly
Simpler build scripts with one command
Options
Combines all options from bothspec and routes commands:
Path to the tsoa configuration file.
Override the base API path for both spec and routes.
Override the API host in the generated specification.
Generate spec in YAML format.
Generate spec in JSON format.
Examples
Output
Generates both:- Specification file:
{spec.outputDirectory}/{spec.specFileBaseName}.{json|yaml} - Routes file:
{routes.routesDir}/{routes.routesFileName}
Global Options
These options work with any command:Display help information.
Display the tsoa version.
Exit Codes
The CLI uses standard exit codes:| Code | Meaning | Common Causes |
|---|---|---|
0 | Success | Generation completed without errors |
1 | Error | Configuration errors, invalid TypeScript, missing files |
Handling Errors in Scripts
Common Workflows
Development Mode
package.json
Build Pipeline
package.json
Testing
package.json
Troubleshooting
Command not found
Command not found
- Use
npx tsoainstead oftsoa - Run via npm script:
npm run tsoa:gen - Install globally:
npm install -g tsoa(not recommended)
Configuration file not found
Configuration file not found
- Create a
tsoa.jsonin your project root - Specify the path:
tsoa spec -c path/to/config.json - Use a different format:
tsoa.yamlortsoa.js
Generation takes too long
Generation takes too long
Solutions:
- Use
spec-and-routesinstead of running commands separately - Add more paths to
ignorein configuration - Use
controllerPathGlobsinstead of scanning fromentryFile - Ensure TypeScript
skipLibCheck: truein tsconfig.json
TypeScript errors during generation
TypeScript errors during generation
Solutions:
- Ensure your project compiles:
tsc --noEmit - Check
compilerOptionsin tsoa.json matches tsconfig.json - Fix any type errors in controller files
Next Steps
Configuration
Learn about all configuration options
tsoa.json Reference
Complete configuration schema