Strapi developers often encounter the challenge of ensuring custom routes are included in the generated documentation. This step-by-step guide aims to simplify this process, ensuring a seamless integration.
1. Installing the Documentation Plugin:
- Install the Documentation plugin in your Strapi project with npm install strapi-plugin-documentation if it's not already present, you can also follow this guide from Strapi Developers Docs.
2. Adding a Custom Route:
- Reference the Strapi Custom Route Project. It includes a custom route for finding posts by slug.
3. Initial Documentation Check:
- Notice the absence of custom routes in the generated documentation before making changes.

4. Code Integration for Custom Routes:
Add the following code to the register function in the src/index.js file of your project:
register(/*{ strapi }*/) {
if (strapi.plugin('documentation')) {
console.log('register override')
const override = {
paths: {
'/posts/slug/{slug}': {
get: {
operationId: 'findBySlug',
summary: 'Find a post by slug',
description: '',
parameters: [
{
name: 'slug',
in: 'path',
description: 'slug of post',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: 'Successful response',
content: {
'application/json': {},
},
},
},
tags: ['Post'],
},
},
},
}
strapi
.plugin('documentation')
.service('override')
.registerOverride(override)
}
}
Code Breakdown:
- This script checks for the documentation plugin and then registers an override for a specific path (/posts/slug/{slug}). The get method under this path is detailed with an operationId, a summary, and other necessary configurations.
5. Server Restart:
- Restart your Strapi server to apply the changes.
6. Documentation Verification:
- The custom route for finding posts by slug should now appear in your documentation.

Conclusion:
This guide provides a clear path for Strapi developers to include custom routes in their API documentation, ensuring a comprehensive and user-friendly API interface.