Overview
NestJS CRUD automatically integrates with Swagger/OpenAPI to generate comprehensive API documentation for all CRUD endpoints. No additional configuration is needed beyond standard NestJS Swagger setup.Basic Setup
Install Swagger dependencies:main.ts:
main.ts
http://localhost:3000/docs.
CRUD endpoints are automatically documented with proper schemas, parameters, and response types.
Tagging Endpoints
Use@ApiTags() to organize endpoints:
users.controller.ts
Automatic Documentation
The framework automatically generates documentation for:Query Parameters
All query parameters are documented with descriptions and links:fields- Select specific fieldsfilter- Filter conditionsor- OR conditionssort- Sortingjoin- Relations to includelimit- Pagination limitoffset- Pagination offsetpage- Page numbercache- Cache controlincludeDeleted- Include soft-deleted records (when enabled)
Path Parameters
Path parameters are automatically documented based on yourparams configuration:
projects.controller.ts
companyId(path parameter, type: number)id(path parameter, type: number)
Response Schemas
Response schemas are automatically generated from:- Your entity model
- Custom serialization DTOs
Operation Descriptions
Default operation summaries are generated based on your model name:- GET /users - “Retrieve multiple Users”
- GET /users/:id - “Retrieve a single User”
- POST /users - “Create a single User”
- POST /users/bulk - “Create multiple Users”
- PATCH /users/:id - “Update a single User”
- PUT /users/:id - “Replace a single User”
- DELETE /users/:id - “Delete a single User”
- PATCH /users/:id/recover - “Recover one User”
DTO Documentation
Document your DTOs with Swagger decorators:Request DTOs
create-note.dto.ts
Response DTOs
get-company-response.dto.ts
Entity Documentation
Document your entities for accurate schemas:user.entity.ts
Response Types
Single Resource
ForgetOne, create, update, replace:
Multiple Resources
ForgetMany (without pagination):
Paginated Response
ForgetMany with pagination:
Customizing Responses
Swagger automatically handles different response types based on your configuration:Soft Delete Documentation
When soft delete is enabled, theincludeDeleted query parameter is automatically added:
- includeDeleted (query parameter, type: integer, values: 0 or 1)
Authentication Documentation
Document authentication requirements:main.ts
users.controller.ts
Complete Example
Here’s a fully documented setup:main.ts
companies.controller.ts
Best Practices
1
Use ApiProperty Decorators
Add
@ApiProperty() to all DTOs and entities for complete documentation.2
Add Examples
Include example values in
@ApiProperty() for better developer experience.3
Organize with Tags
Use
@ApiTags() to group related endpoints.4
Document Authentication
Use
@ApiBearerAuth() or similar decorators for protected endpoints.Troubleshooting
Missing Response Schemas
If response schemas aren’t showing:- Ensure
@ApiProperty()decorators are on all DTO properties - Verify serialization DTOs are correctly configured
- Check that entities have proper decorators
Incorrect Parameter Types
If parameter types are wrong:- Verify
paramsconfiguration in@Crud() - Ensure entity properties have correct TypeScript types
- Check that enums are properly defined
Related Resources
- Serialization - Control response DTOs
- Global Configuration - Configure default behaviors
- NestJS Swagger Documentation - Official NestJS Swagger guide