Description
The EquipChain API currently has no formal API documentation, making it difficult for internal developers and potential external integrators to understand available endpoints, request formats, and response structures. This issue implements automated OpenAPI 3.1 (Swagger) documentation generation using swagger-jsdoc and swagger-ui-express to create a living documentation portal that stays synchronized with the codebase.
The documentation should cover all existing and planned API endpoints with detailed information including: path parameters, query parameters, request body schemas (referencing Zod schemas), response schemas for success and error cases, authentication requirements (indicating which endpoints require JWT), rate limit information, and example requests/responses. The Swagger UI should be served at /api-docs and protected behind authentication in production environments.
The documentation must be generated from JSDoc-style annotations in route files using swagger-jsdoc. Each route file should include a YAML or JSON comment block describing the endpoint. This approach keeps documentation close to the implementation, making it more likely to stay up-to-date. The generated OpenAPI specification should also be exportable as a JSON file (/api-docs.json) for use with API client generators like openapi-generator.
Technical Context & Impact
Step-by-Step Implementation Guide
- Install Dependencies: Run
npm install swagger-jsdoc swagger-ui-express. These will serve the documentation from within the Express app.
- Create Swagger Configuration: Write
src/config/swagger.js that defines the OpenAPI object: version, title ("EquipChain API"), description, contact info, servers (development/production URLs), components (securitySchemes for JWT and API Key), and tags for grouping endpoints (Auth, Admin, Meters, System, Webhooks).
- Add JSDoc Annotations to Routes: Add OpenAPI YAML comments above each route handler in
src/routes/. Start with existing routes (GET /) and then add annotations for routes created in other issues (auth, admin, etc.). Each annotation should specify: summary, description, tags, parameters, requestBody (with content type), responses (with schemas).
- Create Docs Route: Write
src/routes/docs.js with GET /api-docs serving swagger-ui-express (use serve, setup), and GET /api-docs.json serving the raw OpenAPI spec object. In production, protect /api-docs behind authentication.
- Wire in index.js: Mount the docs route in
src/index.js. Ensure the docs are available at /api-docs and verify the Swagger UI renders correctly with all endpoints listed.
- Write Validation Test: Create a test that validates the generated OpenAPI spec is valid (using swagger-parser or similar) and that all documented endpoints exist in the Express router.
Verification & Testing Steps
- Start the server and navigate to
http://localhost:3000/api-docs in a browser — verify the Swagger UI loads with the EquipChain API title and lists all documented endpoints.
- Click on an endpoint to expand it — verify path parameters, query parameters, request body schemas, and response schemas are displayed correctly.
- Use the "Try it out" feature — execute a request from Swagger UI and verify the response matches the documented schema.
- Fetch
http://localhost:3000/api-docs.json — verify the JSON is valid OpenAPI 3.1 spec. Use an online validator or the swagger-parser CLI to validate.
- Temporarily set
NODE_ENV=production and verify that /api-docs requires authentication (returns 401/403 without a valid JWT).
Description
The EquipChain API currently has no formal API documentation, making it difficult for internal developers and potential external integrators to understand available endpoints, request formats, and response structures. This issue implements automated OpenAPI 3.1 (Swagger) documentation generation using
swagger-jsdocandswagger-ui-expressto create a living documentation portal that stays synchronized with the codebase.The documentation should cover all existing and planned API endpoints with detailed information including: path parameters, query parameters, request body schemas (referencing Zod schemas), response schemas for success and error cases, authentication requirements (indicating which endpoints require JWT), rate limit information, and example requests/responses. The Swagger UI should be served at
/api-docsand protected behind authentication in production environments.The documentation must be generated from JSDoc-style annotations in route files using
swagger-jsdoc. Each route file should include a YAML or JSON comment block describing the endpoint. This approach keeps documentation close to the implementation, making it more likely to stay up-to-date. The generated OpenAPI specification should also be exportable as a JSON file (/api-docs.json) for use with API client generators likeopenapi-generator.Technical Context & Impact
swagger-jsdoc(^7.x),swagger-ui-express(^5.x). These are production dependencies since the docs should be served by the running API.src/config/swagger.jsinitializes the swagger-jsdoc instance with the API info and path to route files. Newsrc/routes/docs.jsmounts swagger-ui-express and the raw JSON endpoint.Step-by-Step Implementation Guide
npm install swagger-jsdoc swagger-ui-express. These will serve the documentation from within the Express app.src/config/swagger.jsthat defines the OpenAPI object: version, title ("EquipChain API"), description, contact info, servers (development/production URLs), components (securitySchemes for JWT and API Key), and tags for grouping endpoints (Auth, Admin, Meters, System, Webhooks).src/routes/. Start with existing routes (GET /) and then add annotations for routes created in other issues (auth, admin, etc.). Each annotation should specify: summary, description, tags, parameters, requestBody (with content type), responses (with schemas).src/routes/docs.jswithGET /api-docsserving swagger-ui-express (useserve,setup), andGET /api-docs.jsonserving the raw OpenAPI spec object. In production, protect/api-docsbehind authentication.src/index.js. Ensure the docs are available at/api-docsand verify the Swagger UI renders correctly with all endpoints listed.Verification & Testing Steps
http://localhost:3000/api-docsin a browser — verify the Swagger UI loads with the EquipChain API title and lists all documented endpoints.http://localhost:3000/api-docs.json— verify the JSON is valid OpenAPI 3.1 spec. Use an online validator or theswagger-parserCLI to validate.NODE_ENV=productionand verify that/api-docsrequires authentication (returns 401/403 without a valid JWT).