API Documentation Guide¶
This guide explains how to add OpenAPI documentation to your routes using the built-in documentation utilities.
Overview¶
The PRS Backend uses Fastify Swagger and Fastify Swagger UI to generate OpenAPI documentation from route schemas. We've added utilities to make it easy to generate these schemas from Zod validation schemas.
Documentation Utilities¶
The following utilities are available in the utils module:
documentRoute: Generates documentation for a routedocumentPublicRoute: Generates documentation for a public route (no authentication required)documentProtectedRoute: Generates documentation for a protected route (requires authentication)standardResponses: Generates standard response schemas for common operationszodToOpenApi: Converts a Zod schema to an OpenAPI schemagenerateRouteSchema: Generates an OpenAPI schema for a route
Basic Usage¶
Here's a basic example of how to add documentation to a route:
Using Standard Responses¶
The standardResponses utility generates standard response schemas for common operations:
Available standard responses:
responses.get: For GET requests (200, 404, 500)responses.list: For GET list requests (200, 500)responses.create: For POST requests (201, 400, 422, 500)responses.update: For PUT requests (200, 400, 404, 422, 500)responses.delete: For DELETE requests (204, 404, 500)responses.custom: For custom responses (errorResponse, successResponse, listResponse)
Complete Example¶
Here's a complete example of a route file with documentation:
| JavaScript | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
Accessing the Documentation¶
The API documentation is available at the /api-docs endpoint in non-production environments. It provides a user-friendly interface to explore and test the API.
Best Practices¶
- Be Descriptive: Provide clear summaries and descriptions for your routes
- Use Tags: Group related routes under the same tag
- Document All Parameters: Document all request parameters, including body, query, and path parameters
- Document Responses: Document all possible responses, including error responses
- Use Standard Responses: Use the
standardResponsesutility for consistent response documentation - Keep Schemas DRY: Define schemas once and reuse them across routes
- Test Documentation: Verify that the documentation is accurate by testing the API through the Swagger UI
Troubleshooting¶
If your documentation is not showing up correctly:
- Make sure you're using the correct documentation utility for your route
- Check that your Zod schemas are correctly defined
- Verify that the route is registered correctly
- Check the browser console for any errors
Advanced Usage¶
Custom Response Schemas¶
You can create custom response schemas for specific routes:
Security Requirements¶
You can specify custom security requirements for a route:
| JavaScript | |
|---|---|
Conclusion¶
Adding OpenAPI documentation to your routes is a simple process that provides significant benefits:
- Self-Documentation: The API documents itself as you develop
- Testing: The Swagger UI provides a convenient way to test your API
- Client Generation: The OpenAPI specification can be used to generate client libraries
- Consistency: The documentation utilities ensure consistent documentation across the API
By following this guide, you can ensure that your API is well-documented and easy to use.