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',
})
);
/**
* @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);
/**
* @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
*/
// 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
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.
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 ✅
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/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.