Prerequisites
- Node.js 18.0.0 or higher
- npm, yarn, or pnpm
- Basic TypeScript knowledge
Step 1: Initialize Your Project
Initialize TypeScript
Create a
tsconfig.json file:tsconfig.json
The
experimentalDecorators and emitDecoratorMetadata options are required for tsoa to work properly.Step 2: Create Your First Controller
Create a simple user controller with CRUD operations:src/controllers/userController.ts
Step 3: Generate Routes and Spec
Add these scripts to yourpackage.json:
package.json
dist/swagger.json- Your OpenAPI specificationsrc/routes.ts- Express route handlers with validation
Step 4: Create Your Server
Create the Express server that uses the generated routes:src/server.ts
Step 5: Run Your API
Start the server:http://localhost:3000!
Step 6: Test Your Endpoints
Try these curl commands to test your API:What Just Happened?
Let’s break down what tsoa did for you:- Analyzed your TypeScript code - tsoa read your controller decorators and type definitions
- Generated OpenAPI spec - Created a complete
swagger.jsonwith all your endpoints, parameters, and response types - Generated route handlers - Created
routes.tswith:- Express route registration
- Request validation based on your TypeScript types
- Automatic serialization/deserialization
- Error handling
View Your OpenAPI Spec
Your generated OpenAPI specification is available atdist/swagger.json. You can:
- View it in Swagger Editor
- Serve it with Swagger UI
- Use it to generate client SDKs with OpenAPI Generator
Next Steps
Core Concepts
Learn about controllers, decorators, and models
Authentication
Add authentication and authorization to your API
Validation
Understand tsoa’s validation capabilities
Framework Guides
Detailed setup guides for Express, Koa, and Hapi
Troubleshooting
Routes not generating
Routes not generating
Make sure your
tsoa.json is in the project root and the entryFile path is correct. Also ensure your TypeScript compilation is successful.Decorators not working
Decorators not working
Verify that
experimentalDecorators and emitDecoratorMetadata are enabled in your tsconfig.json.Validation errors
Validation errors
Check that your request body matches the expected TypeScript types. tsoa validates all incoming requests based on your type definitions.
404 errors
404 errors
Make sure you’ve called
RegisterRoutes(app) in your server file and that your routes are being generated in the correct directory specified in tsoa.json.