Skip to content

[Documentation] Generate OpenAPI/Swagger Documentation for All API Endpoints #14

Description

@KarenZita01

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

  1. Install Dependencies: Run npm install swagger-jsdoc swagger-ui-express. These will serve the documentation from within the Express app.
  2. 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).
  3. 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).
  4. 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.
  5. 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.
  6. 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

  1. 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.
  2. Click on an endpoint to expand it — verify path parameters, query parameters, request body schemas, and response schemas are displayed correctly.
  3. Use the "Try it out" feature — execute a request from Swagger UI and verify the response matches the documented schema.
  4. 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.
  5. Temporarily set NODE_ENV=production and verify that /api-docs requires authentication (returns 401/403 without a valid JWT).

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial Campaign | FWC26Campaign: Official Campaign | FWC26

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions