What Is the OpenAPI Specification? Write and Validate an HTTP API Contract
The OpenAPI Specification (OAS) is a standard format for describing an HTTP API’s URLs, operations, inputs, responses, and security so both people and tools can read them. One OpenAPI document can drive documentation, client generation, and contract checks, but writing a specification does not automatically implement a server or make it behave as described. This article uses OAS 3.2.1, the latest published version as of November 18, 2026, to explain how to describe and review one read operation in a single YAML document. If your tools do not support 3.2.1, check which OAS version they support and set the document’s version field accordingly. ([OAS 3.2.1 Specification](https://spec.openapis.org/oas/v3.2.1.html), [official version list](https://spec.openapis.org/oas/))
The OpenAPI Specification (OAS) is a standard format for describing an HTTP API’s URLs, operations, inputs, responses, and security so both people and tools can read them. One OpenAPI document can drive documentation, client generation, and contract checks, but writing a specification does not automatically implement a server or make it behave as described. This article uses OAS 3.2.1, the latest published version as of November 18, 2026, to explain how to describe and review one read operation in a single YAML document. If your tools do not support 3.2.1, check which OAS version they support and set the document’s version field accordingly. (OAS 3.2.1 Specification, official version list)
What does an OpenAPI document describe?
OpenAPI is an interface-description format for HTTP APIs. A document can describe server URLs, paths and HTTP methods, parameters, request bodies, possible responses, data shapes, and authentication schemes. The description itself is written as JSON or YAML; for example, a YAML document can define an API request body whose media type is application/json. The fact that the description file uses JSON or YAML does not require the API’s own request and response bodies to use either format. (OAS introduction and document format, JSON and YAML compatibility)
Three version values are easy to confuse, but they serve different purposes. The top-level openapi value identifies the OAS specification version used by the document. info.version is the version of the OpenAPI document itself; it is not the OAS version or necessarily the release version of the API being described. Define the actual API versioning policy separately. In the example below, /v1 in the server URL is an assumed API path, not a version policy that OpenAPI enforces automatically. (OpenAPI Object, Info Object)
How do you describe a minimal read API?
The example below describes an HTTP API that looks up one item by ID. example.com and the item data are illustrative assumptions for showing the format; they are not a real server address or a reported execution result.
openapi: 3.2.1
info:
title: Catalog API
summary: Item lookup API
version: '2026-11-18'
servers:
- url: https://api.example.com/v1
description: Illustrative production server URL
security:
- bearerAuth: []
paths:
/items/{itemId}:
get:
operationId: getItem
summary: Get one item
description: Returns the item with the specified ID.
parameters:
- name: itemId
in: path
required: true
description: ID of the item to retrieve
schema:
type: string
responses:
'200':
description: The item was found.
content:
application/json:
schema:
$ref: '#/components/schemas/Item'
'404':
description: No item exists for that ID.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
schemas:
Item:
type: object
required: [id, name]
properties:
id:
type: string
description: Item identifier
name:
type: string
description: Name of the item to display
price:
type: number
description: Item price in an illustrative currency
Error:
type: object
required: [code, message]
properties:
code:
type: string
message:
type: string
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWTIn this example, paths contains the /items/{itemId} path template, and get describes the HTTP operation available at that path. A variable in braces must also be declared as a parameter. A path parameter is required, so it is marked required: true. operationId should be unique because tools can use it to refer to an operation or name generated code. (Paths and path templates, Parameter Object, Operation Object)
Each response starts with a status code and description. If it has a body, define its media type and schema under content. In YAML, status codes are recommended as strings for compatibility with JSON. The specification requires each operation to have at least one response and expects the description to cover a successful response and known errors. Add responses for other outcomes the real API returns, such as 401, 403, or 500. Listing only 200 and 404 does not mean other responses cannot occur. (Responses Object)
components groups reusable schemas, parameters, responses, and security schemes. Declaring an object there does not change API behavior; it must be referenced from elsewhere in the description, as #/components/schemas/Item is here. A schema’s required list marks properties that must be present, and each property’s type describes its data shape. In OAS 3.2.1, the Schema Object is based on JSON Schema Draft 2020-12, but check how your tools support its keywords. In particular, implementations can differ in whether and how they validate format, so do not treat it alone as proof that the server enforces a constraint. (Components Object, Schema Object and data types)
How do you keep the description aligned with the real API?
With a design-first workflow, the team reviews the contract before implementing the client and server. With a code-first workflow, the description is generated from an existing implementation. Either way, when the description and server disagree, tools and users can rely on the wrong contract. An OpenAPI file is not the API implementation, and generated client code or server stubs do not automatically supply business logic or a production environment. OpenAPI descriptions are used by many kinds of tools, including documentation and client generators, server routing, and testing tools. (OAS tooling and usage scenarios)
For a first draft, work through these steps:
Choose an OAS version and check that your team’s editor, validator, and code generator support it. Put that version in the top-level `openapi` field.
Add the API name and document version under `info`, and describe environment-specific server addresses under `servers`. If local, staging, and production URLs differ, distinguish their actual values and variables clearly.
For each path, document the HTTP method and a short operation summary. Declare path variables as parameters, and check that query, header, and cookie parameters match the actual contract and serialization rules.
Describe request and response bodies with media types and reusable schemas. Check that required and optional fields, nullability, constraints, and examples match the real contract.
Add successful and known error responses. Define authentication under `securitySchemes` and attach it globally or to the operations that require it.
Parse and validate the YAML/JSON and OAS structure, then inspect generated documentation or code. Compare the document with the server’s actual requests and responses using contract or integration checks, and include specification-change checks in the release process.
A validator passing means the document structure was accepted. It does not prove that a server implements the responses correctly or that every client tool will interpret the file the same way. Contract verification against the running API is a separate step. When using a newer OAS version or a keyword unsupported by a tool, check explicitly how that tool interprets the document instead of ignoring its warnings.
What security and compatibility issues are easy to miss?
securitySchemes documents the API’s authentication method; it does not enable authentication on the server. The Bearer/JWT declaration in the example does not implement token validation, expiry checks, or permission checks. The server must enforce authentication and authorization. Do not include internal addresses or credentials that should not be public. (Security Scheme Object, security considerations)
Tools may automatically retrieve external $ref resources, which could be hosted on an untrusted domain. Use trusted sources and manage the network access of tools that load or preview external references. OpenAPI descriptions may also contain Markdown with HTML, so rendering tools need suitable sanitization. Treat the API description as input processed by tools. (External resources and Markdown security)
Tool support also varies. Do not assume every editor, validator, and SDK generator immediately supports every feature in the latest specification. Validate the document with the actual toolchain your team uses. If you split a document across files for reuse, also verify base-URI resolution for $ref, cross-references, and handling of reference cycles. (Official OAS versions and schema guidance, reference resolution)
Checklist before adopting OpenAPI
Do you understand that the OAS version and `info.version` have different roles, and have you defined the API’s own versioning policy separately?
Do the paths, methods, parameters, bodies, and responses match the real API?
Does every path variable have a required parameter declaration, and does every operation document successful and known error responses?
Do reusable schemas match the server contract, without treating `format` or a tool-specific feature as a validation guarantee?
Does the security description match the server’s actual authentication and authorization, with no secrets in the document?
Have you checked tool support and security boundaries for `$ref`, Markdown, and external resources?
Does the release workflow compare the contract with the real API in addition to parsing and validating the document?
References
[OpenAPI Specification 3.2.1](https://spec.openapis.org/oas/v3.2.1.html)
[Official OpenAPI Initiative versions and schemas](https://spec.openapis.org/oas/)
[Introduction](https://spec.openapis.org/oas/v3.2.1.html#introduction)
[OpenAPI Object and version fields](https://spec.openapis.org/oas/v3.2.1.html#openapi-object)
[Info Object](https://spec.openapis.org/oas/v3.2.1.html#info-object)
[Path templates](https://spec.openapis.org/oas/v3.2.1.html#paths-object)
[Parameter Object](https://spec.openapis.org/oas/v3.2.1.html#parameter-object)
[Operation Object](https://spec.openapis.org/oas/v3.2.1.html#operation-object)
[Responses Object](https://spec.openapis.org/oas/v3.2.1.html#responses-object)
[Components Object](https://spec.openapis.org/oas/v3.2.1.html#components-object)
[Schema Object](https://spec.openapis.org/oas/v3.2.1.html#schema-object)
[Security Scheme Object](https://spec.openapis.org/oas/v3.2.1.html#security-scheme-object)
[Security considerations](https://spec.openapis.org/oas/v3.2.1.html#security-considerations)