Documenting FOLIO Module APIs with OpenAPI and Redocly
Overview
This guide documents the process for creating OpenAPI documentation for Grails-based FOLIO modules (e.g. Agreements, Licenses, Serials Management, Open Access) that can be automatically added to the FOLIO API documentation (listed at https://dev.folio.org/reference/api/endpoints/ ).
The workflow described here uses Redocly to create structured, multi-file OpenAPI specifications that can be validated, previewed, and built into a final YAML file.
Redocly is both a commercial service and a toolkit for documenting APIs using OpenAPI. This documentation is about using the freely available toolkit, not the service. Using Redocly allows the API to be defined using files in a directory structure (making it easy to re-use definitions of common components such as parameters that apply to multiple endpoints), supports validation of the definition, and will build a single distributable yaml file which contains the full definition compiled from the files in the repository. There is a Redocly OpenAPI extension available for Visual Studio Code allowing live validation of the schema as you build it, as well as command line tools (node based) to validate and build definitions (see the Redocly starter repository README)
The APIs are defined using the OpenAPI specification. The specification allows for extremely detailed documentation for APIs, but we can pick and choose the key elements for us. In the Serials documentation, I chose to document in detail:
Paths (endpoints)
Parameters
This includes common parameters from the K-Int Web Toolkit at a top level, but I did not go as far as documenting every property that can be used in a
filterormatchetc.
Success responses
I did also document common error responses but in very little detail
Content schemas (the JSON passed to/from the API)
This is where the bulk of the work lies!
The redocly approach allows you to define elements inline or via references to other files. Generally I’ve used references for paths, parameters and components . Where there is opportunity to re-use any definitions these can be done via references. Any decisions here are about the efficiency of writing the documentation - the final distributable definition file built by Redocly expands all the references into a single file.
High-Level Process
1. Set Up Your Working Environment
Clone the Redocly OpenAPI starter repository
Install dependencies (
npm install)Update the
/openapi/openapi.yamlfile to have the basic information for a FOLIO API
2. Discover the Module's API Endpoints
Use the ModuleDescriptor to find the endpoints exposed to OKAPI/FOLIO API gateway
For each endpoint, add a
pathandrefto theopenapi.yaml
3. Document the HTTP operations, parameters and responses for each path
For each
pathyou have documented, create a yaml file that corresponds to thereffor the path in theopenapi.yamlTo document the API, for each
pathwe should list the operations (GET, PUT, POST, DELETE, etc.) supported by the endpoint, the parameters supported by the operation, and what responses can be received (at minimum the “success” response)When defining parameters and the schema used by responses, at this point add only a reference to a file (which doesn’t have to exist yet) in the
../components/parameters../components/schemadirectories
See this example of a path definition with some suggested common and best practices
4. Document path parameters
Once all the
pathsare documented, using the redoclynpm testor using the Visual Studio integrated validation, you can identify what parameters need to be documentedEach parameter should be documented in a file under
../components/parametersusing the filename from thepathdefinitionsSee this example of a parameter definition with some suggested common and best practices
5. Document Schemas (i.e. the structures of the request payloads and responses)
Once all the
pathsandparametersare documented, using the redoclynpm testor using the Visual Studio integrated validation, you can identify the top level schemas that need to be documented (i.e. those that are directly referenced in thepathsorparametersYou may identify additional schemas that are required as you document schemas (in situations where there is a schema that is part of another schema object, but not directly used by a path or parameter)
Each schema should be documented in a file under
../components/schemasusing the filename from thepathandparameterdefinitionSee this documentation on how to document schemas (this is the most involved part of the process)
See this example of a schema definition with some suggested common and best practices
5. Validate and build
Validate the OpenAPI specification (
npm test)Use Redocly tools to preview the documentation (
npm start)Build the final bundled YAML file (
npm run build)
6. Integrate with the Official Module Repository
Copy the built OpenAPI YAML to the appropriate location in the official FOLIO module repository
Follow the module's conventions for API documentation location (see Serials as an example)
This will then be picked up by automated system operations process to add to the FOLIO API documentation e.g. https://s3.amazonaws.com/foliodocs/api/mod-serials-management/s/serials-management.html
Key Resources
FOLIO Module Repositories
Example Documentation Projects
Serials Mangement
Serials Management API Documentation (Redocly based repo)
Serials Management OpenAPI definition (mod-serials-management repo)
Agreements API Documentation (PoC) (outdated but potentially useful starting point)
Tools and Documentation
Redocly repository file structure
Your Redocly project will have this structure:
openapi/
├── openapi.yaml # Main file: metadata, config, path list
├── paths/ # One YAML file per endpoint path
│ ├── endpoint1.yaml
│ └── endpoint2.yaml
├── components/
│ ├── schemas/ # Schema definitions for request/response objects
│ │ ├── Schema1.yaml
│ │ └── Schema2.yaml
│ └── parameters/ # Reusable parameter definitions
│ ├── param1.yaml
│ └── param2.yaml
└── code_examples/ # (Optional, not currently used)