A Payload CMS plugin for OpenAPI (3.0, 3.1) specification generation
The Payload OpenAPI Plugin is designed to autogenerate an OpenAPI specification from your Payload CMS instance. This specification can be used for documentation purposes or to generate client SDKs. It offers several features and can be easily installed and set up.
To install the Payload OpenAPI Plugin, you can use either npm or yarn. Run the following command in your terminal:
yarn add payload-oapi
or
npm i payload-oapi
To add the OpenAPI specification endpoint to your Payload app, you need to import the openapi plugin and add it to your payload configuration. Here is an example:
import payloadOpenAPIPlugin from 'payload-oapi';
const payloadConfig = {
// Your existing Payload configuration
// ...
plugins: [
// Your existing plugins
// ...
payloadOpenAPIPlugin(),
],
};
// Continue with your Payload app setup
// ...
To add a documentation UI, you can use various plugins such as Swagger UI, Rapidoc, or Redoc. Here are examples for using Swagger UI and Rapidoc plugins:
import swaggerUI from 'swagger-ui';
import payloadOpenAPIPlugin from 'payload-oapi';
// Import the generated OpenAPI specification
import openAPISpec from './openapi-spec.json';
// Initialize the Swagger UI
swaggerUI({
spec: openAPISpec
});
import Rapidoc from 'rapidoc';
import payloadOpenAPIPlugin from 'payload-oapi';
// Import the generated OpenAPI specification
import openAPISpec from './openapi-spec.json';
// Generate documentation using Rapidoc
Rapidoc.create({
specFile: openAPISpec
});
The Payload OpenAPI Plugin is a useful tool for autogenerating an OpenAPI specification from your Payload CMS instance. It provides various features, such as complete descriptions of CRUD endpoints and integration with popular documentation UI libraries like Swagger UI and Rapidoc. Installation and setup are straightforward, allowing you to easily incorporate the plugin into your Payload app.