DevTools Logo

OpenAPI & Swagger Cheat Sheet

Quick reference for the OpenAPI 3.1 specification: paths, operations, schemas, components, security, and spec organization.

Reference
openapi
swagger
rest

OpenAPI (formerly Swagger) is a machine-readable specification for REST APIs, written in JSON or YAML. OpenAPI 3.1 uses JSON Schema 2020-12 for schemas and adds webhooks. The root document has four top-level required fields: openapi, info, paths, and (optionally) components.

Top-Level Structure

yaml
openapi: 3.1.0
info:
  title: Pet Store API
  version: 1.0.0
  description: Example API
servers:
  - url: https://api.example.com/v1
paths: {}
components: {}
Table
FieldRequiredPurpose
openapiYesSpecification version (3.0.3, 3.1.0)
info.titleYesAPI name
info.versionYesAPI version string
serversNoBase URLs; defaults to /
pathsYesOperations grouped by URL path
componentsNoReusable schemas, parameters, security schemes
tagsNoLogical grouping for operations
webhooksNoOutbound events (3.1+)

Paths & Operations

Each path maps to an HTTP method object. summary and description are shown in docs; operationId must be unique and becomes the function name in generated SDKs.

yaml
paths:
  /pets:
    get:
      summary: List pets
      operationId: listPets
      parameters:
        - $ref: '#/components/parameters/limit'
      responses:
        '200':
          description: A paged array of pets
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pets'
    post:
      summary: Create a pet
      operationId: createPet
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        '201':
          description: Created pet

Parameters

Parameters appear in four locations: query, header, path, cookie. Path parameters must also appear in the path string wrapped in braces (/pets/{id}).

Table
FieldPurpose
nameParameter name
inLocation: query, header, path, cookie
requiredtrue for path parameters
schemaData type; type: string, type: integer, etc.
styleSerialization (query defaults form, path simple)
example / examplesSample value for docs
yaml
parameters:
  - name: limit
    in: query
    required: false
    schema:
      type: integer
      minimum: 1
      maximum: 100
      default: 20

Schemas & Components

Schemas follow JSON Schema. Reuse them through components.schemas and $ref to avoid duplication.

yaml
components:
  schemas:
    Pet:
      type: object
      required: [id, name]
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
        status:
          type: string
          enum: [available, pending, sold]
    Pets:
      type: array
      items:
        $ref: '#/components/schemas/Pet'

Security Schemes

Define components.securitySchemes once, then apply security globally or per-operation (empty security: [] disables it for an operation).

yaml
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
    apiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key
security:
  - bearerAuth: []

Common Pitfalls

[!WARNING] In OpenAPI 3.x, request bodies live in requestBody — the body/formData parameters of Swagger 2.0 are gone. Migrating 2.0 → 3.0 requires rewriting them.

[!TIP] Prefer components + $ref over inline duplication; most tools generate SDKs and docs straight from the spec, so naming consistency matters.

References