Convert WordPress REST API schema to OpenAPI/Swagger v2 specification document
# Using npm
npm install wpapi-to-swagger
# Using pnpm
pnpm add wpapi-to-swagger
# Using yarn
yarn add wpapi-to-swagger
import { transform } from 'wpapi-to-swagger'
// Get WordPress REST API schema
const wpApiSchema = {
routes: {
'/wp/v2/posts': {
namespace: 'wp/v2',
endpoints: [
{
methods: ['get'],
description: 'Get list of posts',
args: {
page: {
type: 'integer',
description: 'Page number',
required: false
},
per_page: {
type: 'integer',
description: 'Number of items per page',
required: false
}
}
}
]
},
'/wp/v2/posts/(?P<id>[\\d]+)': {
namespace: 'wp/v2',
endpoints: [
{
methods: ['get'],
description: 'Get a single post',
args: {
context: {
type: 'string',
description: 'Request context',
required: false,
enum: ['view', 'embed', 'edit']
}
}
}
]
}
}
}
// Transform to Swagger specification
const swaggerSpec = transform(wpApiSchema)
// Output result
console.log(JSON.stringify(swaggerSpec, null, 2))
- Convert WordPress REST API schema to OpenAPI/Swagger v2 specification
- Automatically handle WordPress-style path parameters (e.g.
/wp/v2/posts/(?P<id>[\d]+)
→/wp/v2/posts/{id}
) - Automatically determine parameter locations based on HTTP methods (path, query, formData)
- Support parameter type conversion and validation rules
- Generate complete API documentation including paths, methods, parameters, and responses
Convert WordPress REST API schema to OpenAPI/Swagger v2 specification document.
Parameters:
source
(WordPressAPISchema): WordPress REST API schema object
Returns:
- (OpenAPISpecificationV2): Document object conforming to OpenAPI/Swagger v2 specification
// WordPress API schema
interface WordPressAPISchema {
routes: Record<string, WordPressRoute>
}
// WordPress route
interface WordPressRoute {
namespace: string
endpoints: WordPressEndpoint[]
}
// WordPress endpoint
interface WordPressEndpoint {
methods: ('get' | 'post' | 'put' | 'delete')[]
description?: string
args?: Record<string, WordPressArgument>
}
// WordPress argument
interface WordPressArgument {
type?: string | string[]
description?: string
required?: boolean
enum?: string[]
default?: any
items?: {
type?: string
}
maximum?: number
minimum?: number
format?: string
schema?: any
}
The package also provides some useful utility functions:
convertEndpoint(endpoint)
: Convert WordPress-style endpoint paths to Swagger stylegetParametersFromEndpoint(endpoint)
: Extract path parameters from endpoint pathsgetParametersFromArgs(endpoint, args, method)
: Extract Swagger parameters from argument definitions
{
"swagger": "2.0",
"basePath": "",
"host": "",
"info": {
"title": "WordPress REST API",
"version": "1.0",
"description": "Using the WordPress REST API you can create a plugin to provide an entirely new admin experience for WordPress, build a brand new interactive front-end experience, or bring your WordPress content into completely separate applications."
},
"paths": {
"/wp/v2/posts": {
"get": {
"tags": ["wp/v2"],
"summary": "Get list of posts",
"description": "Get list of posts",
"operationId": "get_wp_v2_posts",
"parameters": [
{
"name": "page",
"in": "query",
"description": "Page number",
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "per_page",
"in": "query",
"description": "Number of items per page",
"required": false,
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": { "description": "OK" },
"400": { "description": "Bad Request" },
"404": { "description": "Not Found" }
}
}
},
"/wp/v2/posts/{id}": {
"get": {
"tags": ["wp/v2"],
"summary": "Get a single post",
"description": "Get a single post",
"operationId": "get_wp_v2_posts_id",
"parameters": [
{
"name": "id",
"in": "path",
"description": "",
"required": true,
"type": "integer",
"format": "int64"
},
{
"name": "context",
"in": "query",
"description": "Request context",
"required": false,
"type": "array",
"items": {
"type": "string",
"enum": ["view", "embed", "edit"]
},
"collectionFormat": "multi"
}
],
"responses": {
"200": { "description": "OK" },
"400": { "description": "Bad Request" },
"404": { "description": "Not Found" }
}
}
}
}
}
- Generate API documentation for WordPress sites
- Create client applications based on WordPress API
- Provide interactive API documentation using Swagger UI
- Integrate with API development tools like Postman, Swagger Editor, etc.