API Documentation Training
Master the automated API documentation system using Swagger annotations and Scalar UI.
π― Objectivesβ
By the end of this module, you will:
- β Understand the API documentation workflow
- β Write proper Swagger annotations
- β Follow standardized tag conventions
- β Generate and validate documentation
- β Troubleshoot common issues
- β Maintain high-quality API documentation
Estimated time: 2-3 days
Why This System?β
Problems We Solvedβ
- Inconsistent documentation: Previously had 8 different Stripe tags scattered across endpoints
- Manual synchronization: Documentation often outdated compared to actual code
- Poor developer experience: Basic Swagger UI with limited functionality
- No standards: Each developer documented differently
- Maintenance burden: Separate documentation files to maintain
Benefits We Gainedβ
- Automatic synchronization: Documentation generated directly from code annotations
- Modern interface: Scalar UI with interactive testing and better UX
- Consistent standards: Unified tag system and documentation patterns
- Zero maintenance: No separate documentation files to maintain
- Better DX: Developers document while coding, not as an afterthought
System Architectureβ
Core Componentsβ
-
Swagger Annotations in Code
- JSDoc comments with
@swaggertags - OpenAPI 3.0 specification format
- Embedded directly in route files
- Version controlled with the code
- JSDoc comments with
-
generate-docs Script
- Scans all
app/api/**/route.tsfiles - Extracts and validates Swagger annotations
- Generates unified
public/openapi.json - Creates automatic backups
- Merges with existing manual documentation
- Scans all
-
Scalar UI Interface
- Modern, responsive documentation interface
- Interactive API testing capabilities
- Advanced search and filtering
- Better UX than traditional Swagger UI
- Accessible at
/api/reference
-
Automated Workflow Integration
- CI/CD validation of documentation
- Pre-commit hooks for consistency
- Watch mode for development
- Automatic deployment with app
Complete Workflowβ
Getting Startedβ
1. Access Documentationβ
Local Development:
# Start the development server
yarn dev
# Open documentation
open http://localhost:3000/api/reference
Production:
# Live documentation
https://demo.ever.works/api/reference
2. Essential Commandsβ
# Generate documentation manually
yarn generate-docs
# Development mode with file watching
yarn docs:watch
# Validate all annotations
yarn docs:validate
# Check if documentation is up to date
git status public/openapi.json
3. Development Workflowβ
- Create/modify route in
app/api/*/route.ts - Add Swagger annotations using our standards
- Run
yarn generate-docsto update documentation - Verify on
/api/referencethat documentation looks correct - Commit changes including updated
public/openapi.json
Writing Swagger Annotationsβ
Understanding the Structureβ
Every Swagger annotation follows the OpenAPI 3.0 specification and must be placed in a JSDoc comment block starting with @swagger.
Basic Templateβ
/**
* @swagger
* /api/your-endpoint: # The actual API path
* post: # HTTP method (get, post, put, delete, patch)
* tags: ["Category"] # For grouping in UI
* summary: "Brief action" # Short description (appears in list)
* description: "Detailed" # Full explanation with context
---
## Standardized Tag System
### Why Consistent Tags Matter
Tags organize endpoints in the Scalar UI sidebar. Consistent tagging means:
- **Better navigation**: Users find related endpoints easily
- **Logical grouping**: Similar functionality grouped together
- **Professional appearance**: Clean, organized documentation
- **Scalability**: Easy to add new endpoints to existing categories
### Our Tag Conventions
**Format**: `"Provider/Category - Subcategory"` or `"Category"` for core features
#### Admin Operations
```yaml
"Admin - Users" # User management (CRUD, roles, permissions)
"Admin - Categories" # Category management (create, edit, delete, reorder)
"Admin - Items" # Content management (approve, reject, feature)
"Admin - Comments" # Comment moderation (delete, approve)
"Admin - Roles" # Role and permission management
Core Application Featuresβ
"Authentication" # Login, logout, password reset, session management
"Favorites" # User favorites (add, remove, list)
"Items & Content" # Public content browsing, search, filtering
"Featured Items" # Featured content management
Payment Systemsβ
"Stripe - Core" # Checkout, Payment Intent, Setup Intent
"Stripe - Payment Methods" # Payment method CRUD operations
"Stripe - Subscriptions" # Subscription lifecycle management
"Stripe - Webhooks" # Webhook event processing
"LemonSqueezy - Core" # All LemonSqueezy operations
"Payment Accounts" # Cross-provider account management
User and Systemβ
"User" # User profile, preferences, payment history
"System" # Version info, health checks, repository sync
"Security - ReCAPTCHA" # Security verification endpoints
Choosing the Right Tagβ
Decision Tree:
- Is it admin-only? β Use
"Admin - [Category]" - Is it payment-related? β Use
"[Provider] - [Function]" - Is it core app functionality? β Use single word like
"Authentication" - Is it user-specific data? β Use
"User" - Is it system/infrastructure? β Use
"System"
Examples:
// β
Good tag choices
tags: ["Admin - Users"] // Admin creating/editing users
tags: ["Stripe - Subscriptions"] // Subscription management
tags: ["Authentication"] // Login endpoint
tags: ["User"] // User profile data
// β Bad tag choices
tags: ["Users"] // Too vague - admin or user operation?
tags: ["Stripe"] // Too broad - what Stripe function?
tags: ["API"] // Meaningless - everything is API
tags: ["Payments - Stripe"] // Inconsistent with our format
Best Practicesβ
Writing Effective Descriptionsβ
Summary Guidelines:
- Use action verbs: "Create", "Update", "Delete", "Retrieve"
- Be specific: "Get user profile" not "Get user"
- Keep under 50 characters for UI readability
Description Guidelines:
- Explain the business purpose, not just the technical action
- Include authentication/authorization requirements
- Mention any side effects or important behavior
- Use 1-3 sentences maximum
Examples:
# β Bad
summary: "Get user"
description: "Gets a user"
# β
Good
summary: "Get user profile"
description: "Retrieves complete user profile including preferences, subscription status, and activity metrics. Requires authentication and returns filtered data based on user permissions."
# β Bad
summary: "POST /api/users"
description: "Creates user"
# β
Good
summary: "Create user account"
description: "Creates a new user account with email verification. Automatically assigns default role and sends welcome email. Requires admin privileges."