Easily generate OpenAPI paths for the Strapi documentation plugin by automatically reading all your custom route definitions (01-custom.js) from each API.
Supports both TypeScript and JavaScript Strapi projects.
- Reads all
src/api/<content-type>/routes/01-custom.jsfiles in your Strapi project. - Returns a valid OpenAPI
pathsobject for use in Strapi's documentation plugin. - Works with both TypeScript and JavaScript projects.
- Minimal configuration: just add your custom route files and reference the utility in your documentation config.
Install with your favorite package manager:
# npm
npm install strapi-swagger-custom-paths# yarn
yarn add strapi-swagger-custom-paths# pnpm
pnpm add strapi-swagger-custom-paths# bun
bun add strapi-swagger-custom-pathsFor more details about how Strapi's documentation plugin works, see the official docs: https://docs.strapi.io/cms/plugins/documentation
- The function
getCustomSwaggerPaths()will scan your Strapi project'ssrc/apifolder for allroutes/01-custom.jsfiles. - It will merge all Swagger/OpenAPI route definitions and return a single
pathsobject ready to use in your documentation plugin config.
This package automatically collects Swagger/OpenAPI documentation from each custom route file in your Strapi project.
- It looks for files named
01-custom.jsor01-custom.tsinside each API folder:src/api/<content-type>/routes/01-custom.js. - Each route object should include a
config.swaggerproperty, which follows the OpenAPI specification for that HTTP method and path. - All
swaggerobjects are merged into thepathssection of your final OpenAPI documentation.
You can include any valid OpenAPI fields (summary, description, parameters, requestBody, responses, etc.) inside config.swagger.
module.exports = {
routes: [
{
method: 'GET',
path: '/my-content-type/current',
handler: 'my-content-type.getCurrent',
config: {
tags: ['MyContentType'],
swagger: {
summary: 'Get current MyContentType',
description: 'Retrieve the current MyContentType',
operationId: 'getCurrent',
tags: ['MyContentType'],
},
},
},
],
};export default {
routes: [
{
method: 'GET',
path: '/my-content-type/current',
handler: 'my-content-type.getCurrent',
config: {
tags: ['MyContentType'],
swagger: {
summary: 'Get current MyContentType',
description: 'Retrieve the current MyContentType',
operationId: 'getCurrent',
tags: ['MyContentType'],
},
},
},
],
};import { getCustomSwaggerPaths } from 'strapi-swagger-custom-paths'; // Add this line
export default () => ({
documentation: {
enabled: true,
config: {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "Documentation",
description: "",
termsOfService: "YOUR_TERMS_OF_SERVICE_URL",
contact: {
name: "Team",
email: "contact-email@something.io",
url: "mywebsite.io"
},
license: {
name: "Apache 2.0",
url: "https://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"x-strapi-config": {
plugins: ["upload", "users-permissions"],
path: "/documentation"
},
servers: [
{
url: "http://localhost:1337/api",
description: "Development server"
}
],
externalDocs: {
description: "Find out more",
url: "https://docs.strapi.io/developer-docs/latest/getting-started/introduction.html"
},
security: [
{
bearerAuth: []
}
],
paths: getCustomSwaggerPaths(), // Add this line
}
},
});const { getCustomSwaggerPaths } = require('strapi-swagger-custom-paths'); // Add this line
module.exports = () => ({
documentation: {
enabled: true,
config: {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "Documentation",
description: "",
termsOfService: "YOUR_TERMS_OF_SERVICE_URL",
contact: {
name: "Team",
email: "contact-email@something.io",
url: "mywebsite.io"
},
license: {
name: "Apache 2.0",
url: "https://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"x-strapi-config": {
plugins: ["upload", "users-permissions"],
path: "/documentation"
},
servers: [
{
url: "http://localhost:1337/api",
description: "Development server"
}
],
externalDocs: {
description: "Find out more",
url: "https://docs.strapi.io/developer-docs/latest/getting-started/introduction.html"
},
security: [
{
bearerAuth: []
}
],
paths: getCustomSwaggerPaths(), // Add this line
}
},
});- Node.js: >= 16.x
- Strapi: v4.x or v5.x
- Works with both JavaScript and TypeScript Strapi projects.
- Node.js: 16.x, 18.x, 20.x
- Strapi: 4.15.0, 5.12.5
MIT
Pull requests and issues are welcome! Please open an issue or PR on GitHub.
You can include any valid OpenAPI fields (summary, description, parameters, requestBody, responses, etc.) inside config.swagger.
DanSP89 (https://github.com/dansp89)
