081
LVL 03 — MID DEVELOPERSESSION 081DAY 81

API DOCUMENTATION

🎫 PIXELCRAFT-068
Feature | 🟡 Medium | Priority: 🟠 High

A mobile team wants to build an iOS app against the PixelCraft API. They ask: "Where are the docs?" There are none. Build interactive API documentation at /api/docs with Swagger — every endpoint documented with parameters, responses, and examples.
CONCEPTS.UNLOCKED
📖
OpenAPI / Swagger
The industry standard for describing REST APIs. A YAML/JSON file that defines every endpoint, parameter, request body, and response. Machine-readable — tools generate docs, SDKs, and tests from it.
🤖
Auto-Generating Docs
Write JSDoc comments → generate OpenAPI spec. swagger-jsdoc reads your route comments and produces a spec. swagger-ui-express renders it as an interactive page.
🏷️
API Versioning
/api/v1/ prefix for all routes. When you make breaking changes, create /api/v2/ — old clients keep working on v1. Version in URL is the simplest and most visible strategy.
📝
Documentation-Driven Development
Write the API spec first, then implement. The spec becomes the contract between frontend and backend. Teams can work in parallel — frontend mocks from the spec, backend implements it.
🧪
Try It Out
Interactive docs let you test endpoints. Swagger UI has a "Try it out" button — fill in parameters, click Execute, see the real response. API documentation that doubles as a testing tool.
📋
Response Schemas
Define the shape of every response. What fields does /api/images return? What type is each field? Schema definitions serve as both documentation and validation contracts.
HANDS-ON.TASKS
01
Set Up Swagger
npm install swagger-jsdoc swagger-ui-express const swaggerJsdoc = require('swagger-jsdoc'); const swaggerUi = require('swagger-ui-express'); const swaggerSpec = swaggerJsdoc({ definition: { openapi: '3.0.0', info: { title: 'PixelCraft API', version: '1.0.0', description: 'Image editing platform API', }, servers: [ { url: '/api/v1', description: 'v1' }, ], components: { securitySchemes: { bearerAuth: { type: 'http', scheme: 'bearer', bearerFormat: 'JWT', }, }, }, }, apis: ['./routes/*.js'], }); app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, { customSiteTitle: 'PixelCraft API Docs', }) );
02
Document Endpoints with JSDoc
/** * @openapi * /images: * get: * summary: Get user's images * tags: [Images] * security: * - bearerAuth: [] * parameters: * - in: query * name: page * schema: * type: integer * default: 1 * description: Page number * - in: query * name: limit * schema: * type: integer * default: 20 * description: Items per page * responses: * 200: * description: List of images * content: * application/json: * schema: * type: object * properties: * images: * type: array * items: * $ref: '#/components * /schemas/Image' * total: * type: integer * 401: * description: Unauthorized */ router.get('/images', authenticate, getImages);
03
Define Schemas
/** * @openapi * components: * schemas: * Image: * type: object * properties: * id: * type: string * example: "507f1f77bcf86cd7" * title: * type: string * example: "Sunset photo" * thumbnailUrl: * type: string * example: "/thumbnails/abc.webp" * width: * type: integer * example: 1920 * height: * type: integer * example: 1080 * createdAt: * type: string * format: date-time * Error: * type: object * properties: * error: * type: string * requestId: * type: string */
04
Add API Versioning
// Version all routes under /api/v1/ const v1Router = express.Router(); v1Router.use('/auth', authRoutes); v1Router.use('/images', imageRoutes); v1Router.use('/analytics', analyticsRoutes); app.use('/api/v1', v1Router); // When v2 is needed: // app.use('/api/v2', v2Router); // v1 keeps working for old clients
URL versioning (/api/v1/) is the simplest approach. Other strategies: header versioning (Accept: application/vnd.api.v1+json) or query param (?version=1). URL is most visible and easiest to reason about.
05
Document All Endpoints

Add JSDoc/OpenAPI comments to every route: POST /auth/register, POST /auth/login, GET /images, POST /images/upload, GET /images/:id, DELETE /images/:id, GET /analytics/daily-active-users. Include request body examples and all possible response codes.

06
Close the Ticket
git switch -c feature/PIXELCRAFT-068-api-docs git add server/ git commit -m "Add Swagger API docs + versioning (PIXELCRAFT-068)" git push origin feature/PIXELCRAFT-068-api-docs # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

API design is interface design.

An API is a contract between systems. Good API design follows the same principles as good UI design — consistency, predictability, and clear feedback.

// API design principles:

Consistency
  All endpoints follow the same pattern
  GET /resources, POST /resources
  GET /resources/:id

Predictability
  If GET /images works, GET /users
  should work the same way

Clear errors
  400 = your fault (bad input)
  401 = not logged in
  403 = logged in, not authorized
  404 = doesn't exist
  500 = our fault (bug)

// Famous APIs to study:
Stripe  → gold standard of API design
GitHub → REST and GraphQL both
Twilio → excellent error messages

// "Your API is a user interface.
// Developers are users too."
"Docs Lab"
[A]Export the OpenAPI spec as a JSON file (/api/docs.json). Use it to auto-generate a TypeScript client SDK with openapi-typescript-codegen. Frontend devs get typed API calls for free.
[B]Add request/response examples for every endpoint. Include both success and error examples. Add a "Getting Started" section to the docs with authentication flow and first API call.
[C]Research: Stripe's API docs are considered the gold standard. What makes them great? Analyze: code examples in multiple languages, clear error messages, test mode, and the design principles behind them.
REF.MATERIAL
ARTICLE
Swagger / SmartBear
The official OpenAPI spec: paths, operations, parameters, request bodies, responses, schemas, security. The industry standard for API description.
OPENAPIOFFICIALESSENTIAL
VIDEO
Fireship
Quick overview of REST principles: resources, HTTP methods, status codes, and why API design matters. The context behind the docs.
RESTQUICK
ARTICLE
Stripe
The gold standard of API documentation. Study the layout, examples, error handling, and developer experience. This is what great docs look like.
STRIPEREFERENCEESSENTIAL
ARTICLE
Surnet
Generate OpenAPI specs from JSDoc comments in your Express routes. Write docs alongside your code — they stay in sync automatically.
SWAGGERNODE.JS
VIDEO
Academind
Setting up Swagger UI with Express: defining specs, documenting endpoints, adding authentication, and customizing the UI.
SWAGGERTUTORIAL
// LEAVE EXCITED BECAUSE
Visit /api/docs → see every endpoint, parameter, and response beautifully documented. Click "Try it out" → test any endpoint live. The mobile team can now build against your API without asking a single question.