Guide for Creating OpenAPI Specifications
This guide outlines the rules and best practices for creating OpenAPI specifications. By following these guidelines, you can ensure a consistent and well-documented API.
General Rules
-
Operation ID:
- Every path endpoint in the OpenAPI specification must have an
operationId.
- The
operationId should follow one of these formats:
<HandlerName>
<HandlerName::ServiceName>
-
Tags:
- Every path endpoint must have one or more
tags.
- Tags should be used to categorize endpoints logically (e.g., by resource, functionality, or service).
Steps to Create an OpenAPI Specification
1. Define the Basics
Start with the fundamental structure of the OpenAPI document:
openapi: 3.0.0
info:
title: Your API Title
description: A brief description of your API.
version: 1.0.0
servers:
- url: https://api.example.com/v1
2. Add Paths and Endpoints
For each API endpoint, include the following:
- Path: Specify the endpoint (e.g.,
/users, /orders/{id}).
- HTTP Methods: Define the HTTP method (
GET, POST, PUT, DELETE, etc.).
- Operation ID: Assign a unique and meaningful
operationId for the method.
- Tags: Associate relevant tags for better organization.
Example:
paths:
/users:
get:
summary: Retrieve a list of users
operationId: GetUsers
tags:
- Users
responses:
'200':
description: A list of users
/orders/{id}:
delete:
summary: Delete an order by ID
operationId: DeleteOrder::OrderService
tags:
- Orders
parameters:
- name: id
in: path
required: true
description: The ID of the order to delete
schema:
type: string
responses:
'204':
description: Order successfully deleted
3. Add Components (Optional)
If applicable, define reusable components such as schemas, parameters, and responses:
Example:
components:
schemas:
User:
type: object
properties:
id:
type: string
name:
type: string
email:
type: string
responses:
NotFound:
description: Resource not found
Full OpenAPI Specification Example
Here is a complete example of an OpenAPI specification based on the description:
openapi: 3.0.0
info:
title: Example API
description: An example API to demonstrate the OpenAPI specification.
version: 1.0.0
servers:
- url: https://api.example.com/v1
paths:
/users:
get:
summary: Retrieve a list of users
operationId: GetUsers
tags:
- Users
responses:
'200':
description: A list of users
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
/orders/{id}:
delete:
summary: Delete an order by ID
operationId: DeleteOrder::OrderService
tags:
- Orders
parameters:
- name: id
in: path
required: true
description: The ID of the order to delete
schema:
type: string
responses:
'204':
description: Order successfully deleted
'404':
$ref: '#/components/responses/NotFound'
components:
schemas:
User:
type: object
properties:
id:
type: string
name:
type: string
email:
type: string
responses:
NotFound:
description: Resource not found
Best Practices
By adhering to these rules and recommendations, your OpenAPI specification will be easier to understand, maintain, and utilize by developers and consumers alike.
Guide for Creating OpenAPI Specifications
This guide outlines the rules and best practices for creating OpenAPI specifications. By following these guidelines, you can ensure a consistent and well-documented API.
General Rules
Operation ID:
operationId.operationIdshould follow one of these formats:<HandlerName><HandlerName::ServiceName>Tags:
tags.Steps to Create an OpenAPI Specification
1. Define the Basics
Start with the fundamental structure of the OpenAPI document:
2. Add Paths and Endpoints
For each API endpoint, include the following:
/users,/orders/{id}).GET,POST,PUT,DELETE, etc.).operationIdfor the method.Example:
3. Add Components (Optional)
If applicable, define reusable components such as schemas, parameters, and responses:
Example:
Full OpenAPI Specification Example
Here is a complete example of an OpenAPI specification based on the description:
Best Practices
Use Meaningful
operationIdValues:GetDataorFetchInfo.Organize with Tags:
Users,Orders,Authentication, etc.Consistency:
By adhering to these rules and recommendations, your OpenAPI specification will be easier to understand, maintain, and utilize by developers and consumers alike.