Overview

6 Describing HTTP operations with OpenAPI

This chapter explains how to describe HTTP operations with the OpenAPI Specification and why doing so accelerates and de-risks API design. It positions OpenAPI as the standard format for REST APIs that supports the whole lifecycle: enabling collaborative design discussions, linting and quality checks, documentation, testing, mocking, code and configuration generation, and gateway alignment. The chapter introduces working with OpenAPI in parallel to API design (not during needs analysis), clarifies specification-first versus code-first authoring approaches, and offers practical guidance on versions (favoring 3.1/3.0) and formats (YAML for human readability). It advocates starting with a minimal document and incrementally enriching it as HTTP choices are made.

Readers learn to map resources and operations to OpenAPI constructs: resource paths become keys under paths with human-readable summaries; path parameters are defined at the path level with name, in, required (true for path), and schema. Operations are added as lowercase HTTP methods under each path, each with a concise summary that mirrors the intent captured during design. Inputs are captured as non-body parameters (path, query, header) in a parameters list and as a requestBody for payloads, using description and content with an explicit media type to make them visible to tools. Throughout, the chapter emphasizes using summaries and descriptions to keep intent clear and aligning the specification with design artifacts while postponing fine-grained data modeling to the schema fields.

Outputs are specified per operation in responses, where each HTTP status code maps to a response object with a clear description; multiple outcome cases that share a status are merged when they return the same kind of data. Response bodies are described with content, media types (e.g., application/json), and schema (with descriptions kept next to the schema), while no-body responses like 204 omit content. Response headers are defined under headers, such as returning a Location header after resource creation. The chapter closes by reinforcing a disciplined, incremental workflow: begin as soon as resource paths are drafted, add methods and inputs/outputs as decisions are made, use the schema placeholders for later detailed data modeling, and eventually refactor shared elements to reduce duplication and improve maintainability.

Using a spreadsheet to describe our REST API was only to simplify our learning. We can describe the programming interface with OpenAPI once we start to design it.
CH06 F01 lauret3
An API Documentation tool can render an OpenAPI document and show information similar to our API spreadsheet but in a more user-friendly way.
CH06 F02 lauret3
YAML is a cousin of JSON with additional features, such as commenting.
CH06 F03 lauret3
All of the REST API information we put in our custom API spreadsheet can be described in a standard way with an OpenAPI document.
CH06 F04 lauret3
We design and describe the programming interface in parallel.
CH06 F05 lauret3
When deciding between specification-first and code-first approaches, it’s essential to check the pros and cons of each approach within the context in which you work.
CH06 F06 lauret3
We describe HTTP operations with OpenAPI as we design them.
CH06 F07 lauret3
We can use OpenAPI to describe the HTTP representation of resources instead of the resource table of the API spreadsheet.
CH06 F08 lauret3
We can use OpenAPI to describe the HTTP representation of operations instead of the operation table of the API spreadsheet.
CH06 F09 lauret3
We add operations to the OpenAPI document when choosing their HTTP method.
CH06 F10 lauret3
In OpenAPI, input data is organized into body and non-body data (headers, path, and query parameters).
CH06 F11 lauret3
We add operation inputs to the OpenAPI document when choosing their locations in the HTTP request.
CH06 F12 lauret3
We add responses to the OpenAPI document when choosing their HTTP statuses.
CH06 F13 lauret3
We add output data to the OpenAPI document when choosing their locations in the HTTP response.
CH06 F14 lauret3

FAQ

What is the OpenAPI Specification and why use it during API design?OpenAPI is a standard, machine-readable format to describe REST(ish) APIs: resource paths, HTTP operations, inputs, outputs, and data models (via JSON Schema). During design it speeds up authoring, reduces errors, enables rendering for non-experts, supports mocks, and serves as a single artifact reused across the lifecycle (design, development, testing, deployment, documentation, SDK generation).
How is “design-first” different from “specification-first,” and how do they compare to “code-first”?Design-first is a process: understand needs and design the API before coding. Specification-first is a technique: author the OpenAPI document independently of the implementation (often used within design-first). Code-first generates OpenAPI from code. Spec-first gives an independent source of truth; code-first keeps docs aligned with implementation but may miss independent oversight. You can mix or switch approaches based on context.
Which OpenAPI version should I choose?Prefer OpenAPI 3.1 if your tools support it; it aligns with a newer JSON Schema version and adds features like webhooks. If not, use 3.0, which has broad support. Avoid starting new work with 2.0 (Swagger 2.0). Migration from 2.0 to 3.x is straightforward, but verify tool compatibility.
Should I write OpenAPI in YAML or JSON?Both are valid. YAML is often easier for humans (fewer brackets and quotes) and supports comments, but be careful with indentation. JSON is fine too. Converting between them is easy, though YAML comments are lost when converting to JSON.
How do I start a minimal OpenAPI document?Begin with three top-level entries: openapi (e.g., 3.1.0), info (title and version), and paths (an object). Many editors (e.g., Swagger Editor “Next”) validate syntax and render the spec as you write.
How do I describe resource paths and path parameters?Add each resource path as a key under paths and give it a short summary. For path parameters, define them in a parameters array at the path level with: name (matching the placeholder), in: path, required: true, and a schema (to be detailed when modeling data).
How do I add HTTP operations to a path?Under a path, add the HTTP method in lowercase (get, post, put, patch, delete) as a key. Provide a concise summary to clarify the operation’s intent for readers and future maintainers.
Where do I describe operation inputs?Use parameters for non-body inputs: - in: path, query, or header - name, optional required flag, and schema For body inputs (POST/PUT/PATCH), use requestBody with a description and content containing the media type (e.g., application/json) and schema.
How do I document responses, bodies, and headers?Under each operation, use responses keyed by quoted HTTP status codes (e.g., "200"). Add a human-readable description. If there is a response body, include content with media type(s) and a schema (you can put a schema description here too). Response headers go in a headers map; each header has a description, required flag, and schema. For 204 No Content, omit content.
What if multiple outputs share the same HTTP status code?YAML/JSON cannot duplicate keys, so merge those outputs under one response entry for that status. Combine the descriptions and ensure they share the same data type (or a common parent). If the data types differ meaningfully, reconsider the operation or the modeling.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Design of Web APIs, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Design of Web APIs, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Design of Web APIs, Second Edition ebook for free