This guide explains the automatic API documentation generation system for TeachLink backend. The system automatically generates:
- OpenAPI/Swagger specification from code
- Interactive documentation (ReDoc + Swagger UI)
- Example code in multiple languages (cURL, TypeScript, Python, JavaScript)
- SDK generators for TypeScript and Python
- Documentation versioning tracking changes across API versions
- Location:
http://localhost:3000/api/docs - Raw JSON:
http://localhost:3000/api/docs-json - Setup: src/main.ts
- Location:
docs/site/ - Entry Point:
docs/site/index.html - Features:
- ReDoc for interactive browsing
- Endpoint table
- Examples in Markdown
- Versioned specs
# Generate all documentation
npm run docs:generate
# Check if docs are up-to-date with spec
npm run docs:checkGenerated Files:
openapi-spec.json- Core OpenAPI spec (multiple locations)docs/api/openapi-spec.json- API docs copydocs/api/examples.md- cURL examplesdocs/site/index.html- Interactive HTML documentationdocs/site/styles.css- Stylingdocs/site/openapi-spec.json- Spec for ReDoc
# Generate OpenAPI spec
npm run sdk:generate:spec
# Generate TypeScript SDK
npm run sdk:generate:ts
# Output: sdk/typescript/
# Generate Python SDK
npm run sdk:generate:python
# Output: sdk/python/
# Generate all SDKs
npm run sdk:generateRequirements:
- OpenAPI Generator CLI (configured in
openapitools.json) - Maven (for generator)
Status: Implemented with enhancements available
Current implementation uses:
- NestJS Swagger module for runtime docs
- Manual OpenAPI spec definition in
scripts/generate-api-docs.js
Enhancement Path: Migrate to decorator-based approach for automatic generation from controllers.
Status: Partially implemented
Currently generates:
- cURL examples in
docs/api/examples.md
Enhancement Path: Add multi-language examples (TypeScript, Python, JavaScript)
Status: Not yet implemented
What's needed:
- Version-specific OpenAPI specs
- CHANGELOG tracking endpoint changes
- Backward compatibility indicators
Status: SDK generation configured, examples need expansion
Current:
- TypeScript SDK generation via OpenAPI Generator
- Python SDK generation via OpenAPI Generator
Enhancement:
- Add JavaScript/Node.js examples
- Add Go examples
- Add Java examples
- Add C# examples
Code Changes
↓
npm run docs:generate (CI/CD)
↓
OpenAPI Spec Generated
↓
Example Code Generated (Multi-language)
↓
Static Site Generated (ReDoc)
↓
SDK Generated (TypeScript, Python, Go, etc.)
↓
Commit to Git / Version Tagged
↓
Documentation Versioned in Archive
docs/
├── api/ # Generated API docs
│ ├── openapi-spec.json
│ └── examples.md
├── site/ # Generated HTML site
│ ├── index.html
│ ├── styles.css
│ └── openapi-spec.json
├── versions/ # Version archive (planned)
│ ├── v1.0/
│ ├── v1.1/
│ └── v2.0/
└── guides/ # Manual guides
├── getting-started.md
├── authentication.md
└── best-practices.md
sdk/
├── typescript/ # TypeScript SDK (generated)
└── python/ # Python SDK (generated)
# Check that Swagger is running
curl http://localhost:3000/api/docs-json | jq '.info.version'
# Generate docs
npm run docs:generate
# Verify generated files
ls -la docs/site/
ls -la sdk/typescript/
ls -la sdk/python/The spec is configured in scripts/generate-api-docs.js:
- Title: TeachLink API
- Version: From package.json
- Servers:
- Local:
http://localhost:3000 - Production:
https://api.teachlink.com
- Local:
- Tags: Organized by feature
- Security: Bearer token authentication
Multi-language examples are generated in:
docs/api/examples.md- cURL examples- SDK examples in generated SDK files
# Static site is ready at docs/site/
# Can be published to:
# - GitHub Pages
# - Netlify
# - AWS S3 + CloudFront
# - Any static host# Swagger UI with "Try it out"
open http://localhost:3000/api/docs
# OpenAPI JSON spec
curl http://localhost:3000/api/docs-json | jqTypeScript:
import { SearchApi } from './sdk/typescript';
const api = new SearchApi();
const results = await api.searchContent('javascript basics');Python:
from openapi_client.apis.tags import search_api
api = search_api.SearchApi()
results = api.search_content(q='javascript basics')# Open in browser
open docs/site/index.html
# Or serve locally
npx http-server docs/site/File: scripts/generate-openapi-spec-from-decorators.ts
Automatically scan NestJS controllers and generate OpenAPI spec:
@Controller('courses')
@ApiTags('Courses')
export class CoursesController {
@Get()
@ApiOperation({ summary: 'List courses' })
@ApiResponse({ status: 200, description: 'Courses found' })
listCourses() { ... }
}File: scripts/generate-examples-multi-language.js
Generate examples in:
- cURL
- TypeScript/JavaScript
- Python
- Go
- Java
- C#
File: scripts/manage-doc-versions.js
Track versions:
docs/versions/
├── v1.0.0/
├── v1.1.0/
└── v2.0.0/
File: .github/workflows/docs.yml
Automatically:
- Generate docs on commit
- Verify no breaking changes
- Publish to documentation site
- Archive previous versions
- Generate change summary
File: test/api/contract.spec.ts
Ensure code matches OpenAPI spec.
// Documentation configuration
export const docsConfig = {
enabled: true,
version: process.env.API_VERSION || '1.0.0',
servers: [
{ url: 'http://localhost:3000', description: 'Development' },
{ url: 'https://api.staging.teachlink.com', description: 'Staging' },
{ url: 'https://api.teachlink.com', description: 'Production' },
],
contact: {
name: 'TeachLink API Support',
email: 'api-support@teachlink.com',
url: 'https://teachlink.com/support',
},
license: {
name: 'Apache 2.0',
url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
},
};{
"scripts": {
"docs:generate": "node scripts/generate-api-docs.js",
"docs:check": "npm run docs:generate && git diff --exit-code -- openapi-spec.json docs/",
"docs:version": "node scripts/manage-doc-versions.js",
"sdk:generate": "npm run sdk:generate:spec && npm run sdk:generate:ts && npm run sdk:generate:python",
"sdk:generate:spec": "npm run docs:generate",
"sdk:generate:ts": "openapi-generator-cli generate -i openapi-spec.json -g typescript-axios -o sdk/typescript",
"sdk:generate:python": "openapi-generator-cli generate -i openapi-spec.json -g python -o sdk/python"
}
}# Force regenerate
npm run docs:generate
# Verify file timestamps
ls -la openapi-spec.json docs/api/openapi-spec.json# Check OpenAPI Generator is installed
openapi-generator-cli version
# Validate OpenAPI spec
npx swagger-cli validate openapi-spec.json
# Generate with verbose output
openapi-generator-cli generate -i openapi-spec.json -g typescript-axios -o sdk/typescript -v# Check Swagger setup in main.ts
curl http://localhost:3000/api/docs-json | jq '.paths | keys'
# Verify controllers have @Controller and @ApiTags decorators
grep -r "@Controller\|@ApiTags" src/