WP OpenAPI is a WordPress plugin that generates OpenAPI 3.1.0 compatible documentation for your WordPress REST APIs.
It has two main features.
- Outputs OpenAPI 3.1.0 spec at
/wp-json-openapi
- Provides OpenAPI viewer using Stoplight's awesome Elements viewer
- Download the latest zip archive from releases page and activate it.
- You should see
WP OpenAPI
menu on the sidebar. - Click
All
to view all the endpoints or choose a namespace to view the endpoints registered in the namespace.
WP OpenAPI has two optional settings -- Enable Try It and Enable Callback Discovery.
Both settings are turned off by default.
You can enable them in Settings -> WP OpenAPI
Enable Try It: It enables Elements's TryIt feature. It lets you try the REST API endpoints.
Enable Callback Discovery: It shows you callback information about the endpoint you're viewing. It comes in handy if you want to look at the source code of the endpoint.
WP OpenAPI has the following filters to modify the output.
Name | Argument | Equivalent Filters Method |
---|---|---|
wp-openapi-filter-operations | Operation[] | AddOperationsFilter |
wp-openapi-filter-paths | Path[] | AddPathsFilter |
wp-openapi-filter-servers | Server[] | AddServersFilter |
wp-openapi-filter-info | Info | AddInfoFilter |
wp-openapi-filter-tags | Tag[] | AddTagsFilter |
wp-openapi-filter-components | Array | AddComponentsFilter |
wp-openapi-filters-elements-props | Array | |
wp-openapi-filters-schema-endpoint-permission | true |
You can use individual filters by calling add_filter.
You can also use Filters.
Filters::getInstance()->addPathsFilter(function(array $paths, array $args) {
if ($args['requestedNamespace'] === 'all) {
foreach ($paths as $path) {
foreach ($path->getOperations() as $operation) {
$operation->setDescription('test');
}
}
}
});
Since there is no central place to add securitySchemes
in WordPress, you can use the wp-openapi-filter-components
filter to add them.
add_filter('wp-openapi-filter-components', function(array $components) {
$components['securitySchemes'] = [
'api_key' => [
'type' => 'apiKey',
'name' => 'api_key',
'in' => 'header',
]
];
return $components;
});
Now, you can use the security
field in your REST API definition.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/test',
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'test' ),
'args' => array(
'status' => array(
'type' => 'string',
'enum' => array( 'yes', 'no' ),
),
'credentials' => [
'type' => 'object',
'properties' => [
'username' => ['type' => 'string'],
'application_password' => ['type' => 'string'],
]
]
),
'security' => array(
'api_key'=> array(),
)
),
)
);
WP OpenAPI comes with a WP CLI command to export a single HTML file.
You can use the file offline or host it on a webserver.
- Install WP CLI
- Open a terminal session and cd to your WordPress installtion directory.
- Run
wp openapi export --namespace=all --save_to=./export.html
To get started, run the following commands:
npm i
composer install
npm start
See wp-scripts for more usage information.
Prerequisites:
- Hub
- Write access to this repository
- Run both
npm i
andcomposer install
- Update the plugin version in wp-openapi.php and readme.txt
- Update
Tested up to
in readme.txt if necessary. - Open a new PR and merge it.
- Checkout
main
branch locally. - Run
./bin/release-to-github.sh
. This will create a new release in Github - Run
./bin/release-plugin-to-dot-org.sh
to release the new version to wp.org
You can also build a zip file for testing purposes by runnning ./bin/build-zip.sh
- Code clean up
- Write tests
- Make a publicly accessible endpoint
- Remove this dirty hack 😅