Skip to main content

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:
Configure Swagger in your main.ts:
main.ts
Access your documentation at 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 fields
  • filter - Filter conditions
  • or - OR conditions
  • sort - Sorting
  • join - Relations to include
  • limit - Pagination limit
  • offset - Pagination offset
  • page - Page number
  • cache - Cache control
  • includeDeleted - Include soft-deleted records (when enabled)

Path Parameters

Path parameters are automatically documented based on your params configuration:
projects.controller.ts
Swagger will document:
  • companyId (path parameter, type: number)
  • id (path parameter, type: number)

Response Schemas

Response schemas are automatically generated from:
  1. Your entity model
  2. 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

For getOne, create, update, replace:

Multiple Resources

For getMany (without pagination):

Paginated Response

For getMany with pagination:

Customizing Responses

Swagger automatically handles different response types based on your configuration:

Soft Delete Documentation

When soft delete is enabled, the includeDeleted query parameter is automatically added:
Swagger will show:
  • includeDeleted (query parameter, type: integer, values: 0 or 1)

Authentication Documentation

Document authentication requirements:
main.ts
Then add to protected controllers:
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.
Swagger documentation is generated at build time. Restart your application after making changes to see updated documentation.

Troubleshooting

Missing Response Schemas

If response schemas aren’t showing:
  1. Ensure @ApiProperty() decorators are on all DTO properties
  2. Verify serialization DTOs are correctly configured
  3. Check that entities have proper decorators

Incorrect Parameter Types

If parameter types are wrong:
  1. Verify params configuration in @Crud()
  2. Ensure entity properties have correct TypeScript types
  3. Check that enums are properly defined