Skip to content

OpenAPI

  • OpenAPI (formerly known as Swagger) is a specification for describing REST APIs.
  • It’s like a blueprint that tells developers exactly how your API works: what endpoints are available, what data they expect, and what they return.
  • Think of it as a user manual for your API that both humans and computers can understand.
  • OpenAPI specifications are written in YAML (or JSON) format and follow a specific structure. YAML is human-readable and uses indentation to represent hierarchy, making it ideal for API documentation.


  • Define your API’s basic info (title, version, description).
  • List your server URIs.
  • Document at least one GET endpoint.
  • Create schemas for your main data models.
  • Add request/response examples.
  • Test your specification in Swagger Editor.
  • All endpoints have clear descriptions and examples.
  • Every response code is documented with appropriate schemas.
  • Data models are complete with proper types and validations.
  • Authentication requirements are clearly specified.
  • File validates without errors in VS Code extension.
  • Specification renders properly in OpenAPI Playground.
  • Examples are realistic and helpful for implementation.
  • Error responses include meaningful error messages.
  • Generate documentation to share with others.

  • Use clear, descriptive names for endpoints and parameters.
  • Follow RESTful naming (nouns for resources, verbs for actions).
  • Use consistent casing (camelCase for properties, kebab-case for paths)
  • Write clear, concise descriptions for every element.
  • Include realistic examples in requests and responses.
  • Explain what each status code means in context.
  • Document error scenarios and their causes.

  1. Start simple: Begin with basic info and one or two endpoints.
  2. Add gradually: Expand endpoints one at a time.
  3. Test frequently: Validate after each major addition.
  4. Refine and polish: Add examples and improve descriptions.
  5. Final review: Check completeness and consistency.

  • Plan before writing: Sketch out your API structure first.
  • Use references: Study well-documented APIs for inspiration.
  • Iterate often: Don’t try to perfect everything in one pass.
  • Get feedback: Have peers review your specification.
  • Think like a user: Write documentation that helps someone implement your API

  • Indentation matters: Use 2 spaces (not tabs) for each level.
  • Key-value pairs: key: value
  • Lists: Use hyphens (-) for array items.
  • Strings: Can be quoted or unquoted (quote when containing special characters).
  • Comments: Start with #

Every OpenAPI specification follows this basic structure:

openapi: 3.2.0
info:
# Basic API information
servers:
# Server configurations
paths:
# API endpoints
components:
# Reusable components
yaml

Example of a Basic Structure of an OpenAPI File

Section titled “Example of a Basic Structure of an OpenAPI File”

An OpenAPI specification is written in YAML or JSON format. Here’s the basic structure:

openapi: 3.2.0
info:
title: My Planets API
description: A simple API for managing planetary data
version: 1.0.0
servers:
- url: https://api.space-research.com/v1
description: Production server
paths:
/planets:
get:
summary: Get all planets
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Planet'
components:
schemas:
Planet:
type: object
required:
- id
- name
- system
properties:
id:
type: integer
example: 1
name:
type: string
example: "Earth"
system:
type: string
example: "Solar System"
yaml

info:
title: Your API Name
description: What your API does
version: 1.0.0
contact:
name: Your Name
email: your.email@example.com
yaml

servers:
- url: https://api.example.com/v1
description: Production server
- url: https://staging-api.example.com/v1
description: Staging server
yaml

paths:
/planets/{id}:
get:
summary: Get planet by ID
parameters:
- name: id
in: path
required: true
schema:
type: integer
responses:
'200':
description: Planet found
'404':
description: Planet not found
yaml

components:
schemas:
Planet:
type: object
properties:
id:
type: integer
name:
type: string
system:
type: string
yaml

Start with the OpenAPI version and basic information about your API:

  • Version: Always specify OpenAPI 3.0.3 or higher.
  • Title: Clear, descriptive name for your API.
  • Description: Brief overview of what your API does.
  • Version: Your API’s version (use semantic versioning like 1.0.0).
  • Contact: Optional contact information.

Specify where your API will be hosted:

  • Development server: Local development URI.
  • Production server: Live deployment URI.
  • Description: Brief note about each server’s purpose.

This is the core of your specification. For each endpoint:

Path Structure:

  • Start with the URI path (e.g., /planets, /planets/{id})
  • Define HTTP methods (get, post, put, delete, etc.).
  • Add descriptions and summaries.

Parameters:

  • Path parameters: Variables in the URI (like {id})
  • Query parameters: Optional filters or pagination.
  • Request body: For POST/PUT operations.

Responses:

  • Define all possible HTTP status codes.
  • Specify response content types.
  • Include example responses.

Create reusable data structures in the components section:

  • Object properties: Define each field with type and description
  • Required fields: Specify which fields are mandatory.
  • Data types: Use standard types (string, integer, boolean, array, object).
  • Validation rules: Add constraints like minimum/maximum values.

Define authentication methods:

  • API Keys: For simple token-based auth.
  • Bearer tokens: For JWT or OAuth.
  • Basic auth: For username/password authentication.

/books:
get:
summary: Get all books
parameters:
- name: limit
in: query
schema:
type: integer
default: 10
yaml

/books:
post:
summary: Create a new book
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Book'
yaml

/books/{id}:
put:
summary: Update a book
parameters:
- name: id
in: path
required: true
schema:
type: integer
yaml

/books/{id}:
delete:
summary: Delete a book
parameters:
- name: id
in: path
required: true
schema:
type: integer
yaml

OpenAPI supports various data types with validation rules:

properties:
age:
type: integer
minimum: 0
maximum: 150
email:
type: string
format: email
website:
type: string
format: uri
birthDate:
type: string
format: date
rating:
type: number
minimum: 1.0
maximum: 5.0
tags:
type: array
items:
type: string
yaml

components:
securitySchemes:
ApiKeyAuth:
type: apiKey
in: header
name: X-API-Key
security:
- ApiKeyAuth: []
yaml

components:
securitySchemes:
BearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
yaml

  • Inconsistent indentation: Always use 2 spaces.
  • Missing colons: Every key needs a colon.
  • Incorrect list formatting: Use proper hyphen alignment.
  • Missing required fields: Always mark required fields in your schemas.
  • Inconsistent data types: Use the same type for the same data across endpoints.
  • Missing examples: Always provide realistic examples.
  • Incomplete error handling: Document all possible error scenarios.
  • Vague descriptions: Write clear, specific descriptions for endpoints and parameters.
  • Ignoring status codes: Document all possible HTTP response codes.
  • Non-RESTful patterns: Follow REST principles for consistency.
  • Overly complex schemas: Keep data models simple and focused.
  • Missing versioning: Always include API version information.
  • Inconsistent naming: Use consistent naming conventions (camelCase or snake_case).