REST API Masterclass
Architecting Modern Backends01.Home02.What is REST?03.HTTP Deep Dive04.URI Best Practices05.JSON & Data Formats06.Environment Setup07.Your First Resource08.Advanced Controllers09.Database Strategy10.JWT Authentication11.Role-Based Auth (RBAC)12.API Versioning13.Filtering & Searching14.Pagination & Sorting15.Global Error Handling16.Rate Limiting17.CORS & Security18.Swagger & OpenAPI19.Testing with Supertest20.Webhooks & Caching21.Production Checklist
Swagger & OpenAPI (OAS)
If your API isn't documented, it doesn't exist. OpenAPI is the world's most popular standard for defining RESTful interfaces.
1. Why document with OAS?
- Interactivity: Clients can test the API directly from the docs.
- Contract First: Design the API with the frontend team before coding.
- Auto-Generation: Generate client libraries in 20+ languages automatically.
2. Swagger UI Integration
In Node.js, swagger-jsdoc and swagger-ui-express are the standard choices.
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const options = {
definition: {
openapi: '3.0.0',
info: { title: 'My API', version: '1.0.0' },
},
apis: ['./routes/*.js'], // Path to the API docs
};
const specs = swaggerJsdoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));3. Documenting a Route
Use YAML comments directly above your code to keep documentation and implementation together.
/**
* @openapi
* /users:
* get:
* description: Returns a list of users
* responses:
* 200:
* description: Success
*/Tip: Modern tools like **Postman** can import OpenAPI YAML files and automatically create request collections, saving hours of manual setup for your team.