API Documentation (Swagger)
Relevant source files
The following files were used as context for generating this wiki page:
The Gauzy platform provides comprehensive, auto-generated API documentation using the @nestjs/swagger package. This system creates interactive API documentation, allowing developers to explore, understand, and test the various endpoints offered by the Gauzy API. The documentation is made available through two modern user interfaces: the standard Swagger UI and the alternative Scalar UI.
This documentation is crucial for developers building integrations or working with Gauzy's headless API features. It provides detailed information on endpoints, data models, and authentication schemes. The system is designed to be modular, automatically including API endpoints from the core application as well as any enabled plugins.
Sources: packages/core/src/lib/bootstrap/swagger.ts, README.md:29
Core Setup and Configuration
The API documentation is initialized during the application's bootstrap process. A dedicated function, setupSwagger, is responsible for configuring and generating the documentation. This function is located in the @gauzy/core package and is called from the main API application entry point.
Sources: packages/core/src/lib/bootstrap/swagger.ts:15-18, apps/api/project.json:20
Initialization Flow
The setupSwagger function uses NestJS's DocumentBuilder to create the base configuration for the API specification. It sets metadata such as title, description, version, and contact information. It also configures the supported authentication methods. Once the configuration is built, SwaggerModule.createDocument inspects the application's controllers and DTOs to generate the full OpenAPI specification. Finally, this document is used to set up the interactive UI endpoints.
The diagram below illustrates the high-level flow of the Swagger setup process.
This diagram shows the sequence of operations within the setupSwagger function to generate and serve the API documentation.
Configuration Options
The setupSwagger function accepts a configuration object to customize the generated documentation. The default values are configured for the Gauzy platform but can be overridden.
| Parameter | Type | Default Value | Description |
|---|---|---|---|
title | string | 'Gauzy API' | The title of the API documentation. |
description | string | 'Gauzy API Documentation' | A short description of the API. |
version | string | '1.0' | The current version of the API. |
swaggerPath | string | 'swg' | The URL path for the Swagger UI. |
scalarPath | string | 'docs' | The URL path for the Scalar UI. |
enableScalar | boolean | true | Flag to enable or disable the Scalar UI. |
servers | Array | See below | An array of server URLs (Dev, Demo, Prod). |
Sources: packages/core/src/lib/bootstrap/swagger.ts:16-89
The default servers configuration includes multiple environments, allowing developers to target different API instances directly from the documentation UI.
[
{
"url": "http://localhost:3000",
"description": "Development Server"
},
{
"url": "https://apidemo.gauzy.co/api",
"description": "Demo Server"
},
{
"url": "https://api.gauzy.co",
"description": "Production Server"
}
]
Sources: packages/core/src/lib/bootstrap/swagger.ts:58-72
API Authentication Schemes
The Swagger documentation is configured to support multiple authentication schemes, enabling developers to test protected endpoints.
Sources: packages/core/src/lib/bootstrap/swagger.ts:104-131
JWT Bearer Authentication
This is the primary authentication method for users. It uses a JSON Web Token (JWT) passed in the Authorization header.
- Type:
http - Scheme:
bearer - Bearer Format:
JWT - Header Name:
Authorization
Sources: packages/core/src/lib/bootstrap/swagger.ts:104-111
API Key Authentication
This method is used for programmatic access by third-party services or scripts.
- Type:
apiKey - Header Name:
X-API-Key - Description: API Key for authentication.
Sources: packages/core/src/lib/bootstrap/swagger.ts:112-119
OAuth 2.0 Authentication
The API supports the OAuth 2.0 Authorization Code flow, which is a standard for delegated authorization. This is commonly used for third-party applications to access user data on their behalf. The configuration specifies the authorization and token URLs.
- Type:
oauth2 - Flow:
authorizationCode - Authorization URL:
/oauth/authorize - Token URL:
/oauth/token - Scopes:
read,write,admin
Sources: packages/core/src/lib/bootstrap/swagger.ts:120-131
The diagram below shows the sequence for obtaining an access token via the OAuth 2.0 Authorization Code flow.
This diagram illustrates the OAuth 2.0 Authorization Code flow as configured in the Swagger documentation.
Sources: packages/core/src/lib/bootstrap/swagger.ts:120-131, packages/auth/src/lib/mcp/server/oauth-authorization-server.ts
API Documentation UIs
Gauzy provides two different web interfaces for the API documentation, catering to different user preferences.
Swagger UI
This is the traditional, widely-used interface for OpenAPI specifications. It is served at the /swg path by default.
The UI is configured with several custom options to improve usability:
persistAuthorization:true(Keeps authorization data across page reloads)docExpansion:'none'(Collapses all operations by default)filter:true(Enables filtering of endpoints)showRequestDuration:true(Displays the duration of API requests)
Sources: packages/core/src/lib/bootstrap/swagger.ts:142-154
Scalar UI
Scalar is a modern, alternative API documentation UI. It is enabled by default and served at the /docs path. It provides a clean, three-column layout and enhanced features.
The setup for Scalar includes a fallback mechanism. It first tries to dynamically import the @scalar/nestjs-api-reference package. If that fails (e.g., the package is not installed), it serves a simple HTML page that loads the Scalar UI from a CDN.
The diagram below shows the logic for setting up the Scalar UI.
This flowchart shows the dynamic setup of the Scalar UI, including the CDN fallback mechanism.
Integration with Plugins
The Gauzy API is modular, composed of the core application and various plugins. The Swagger documentation system is designed to automatically discover and include endpoints from all registered plugins.
Each plugin that exposes API endpoints, such as @gauzy/plugin-job-proposal, includes @nestjs/swagger as a dependency. When the main API application starts, the SwaggerModule scans all controllers, including those from plugins, and adds them to the generated documentation. This ensures that the API documentation is always complete and up-to-date with all enabled features.
Sources: apps/api/package.json:40-60, packages/plugins/job-proposal/package.json:30