Developer and Text Tools

How to Write and Validate an OpenAPI 3 File

Learn the structure of an OpenAPI 3.1 document, the mistakes validators catch most often, and how docs, mock examples and snippets come from one file.

Saizul Amin
Saizul Amin2 Nov 2026 · 4 min read
How to Write and Validate an OpenAPI 3 File

How to Write and Validate an OpenAPI 3 File

An OpenAPI document is a contract. It tells front-end developers what to send, tells back-end developers what to return, and lets tools generate documentation, client libraries, tests and mock servers. Writing one is not hard once you know its shape, and catching mistakes early saves hours later. This guide walks through the structure of an OpenAPI 3.1 file and the most common errors, using the OpenAPI Swagger Editor and Viewer to check the work as you go.

The shape of the document

An OpenAPI file has a few top-level sections:

  • openapi: the version of the specification, such as 3.1.0.
  • info: title and version of your API, with an optional description.
  • servers: one or more base URLs.
  • paths: every endpoint, with its operations (get, post, put, patch, delete).
  • components: reusable schemas, responses, parameters and security schemes.

Most files are written in YAML because it is shorter to read and allows comments, but JSON is equally valid. Swagger 2.0 was the older format; if you still have one, convert it, because current tools focus on version 3.

Paths and operations

Each path key starts with a slash. Variable parts are written in braces, as in /pets/{petId}. Under the path you list operations, and each operation should have a summary, an operationId and a responses object. The operationId matters more than it looks: code generators turn it into a function name, so it must be unique and stable. Names like listPets, createPet and getPet read well in generated clients.

Parameters

Parameters live in the query string, a header, a cookie or the path. A path parameter has to be marked required: true and its name has to match the template exactly. The most common validator error in real files is a renamed path segment, say /pets/{id}, while the parameter is still called petId. A good editor flags both sides: the template that has no parameter and the parameter that is not in the template.

Parameters shared by every operation on a path can be declared once at the path level. Operations can override them by declaring a parameter with the same name and location.

Request bodies and schemas

Request and response bodies are described by schemas, which are a dialect of JSON Schema. Keep them in components/schemas and point at them with $ref, for example $ref: "#/components/schemas/Pet". Use required lists to say which properties must be present, enum for fixed values, and format for strings such as date, date-time, email or uuid. allOf combines schemas, which is a neat way to say "a Pet is a NewPet plus an id".

Broken references are the second most common error. A typo in a $ref path produces documentation with empty holes, so the validator should resolve every local reference and report the ones that point nowhere.

Responses

Every operation needs at least one response. Keys are status codes such as 200 or 404, ranges like 4XX, or the word default. Give each response a description, and for JSON responses a schema or an example. Reusable responses such as a validation error can sit in components/responses.

Examples are documentation

Readers understand an API faster from an example than from a schema. When you add example values to schema properties, tools can build complete example payloads automatically: the editor's mock tab does exactly that, falling back to type-based values when you have not supplied any. Those examples double as fixtures for front-end development before the back end exists, and as starting points for contract tests.

A workflow that keeps the file healthy

  1. Start from a small template and add one endpoint at a time.
  2. Keep the validator panel open and fix errors immediately, while you still remember the change.
  3. Read the documentation preview as a consumer would: are summaries clear, are required fields marked?
  4. Copy the generated request snippet and try it against a development server.
  5. Commit the file next to your code and review changes in pull requests like any other code.

Style versus structure

Structural validation answers "is this a valid OpenAPI document?". Style linting answers "is this a good one?": consistent naming, plural resource names, descriptions everywhere, error formats that match. Many teams add a linter with their own rules in continuous integration. Fix structural errors first, because style rules are meaningless on a document that does not parse correctly.

Limits

The checks in a browser editor cover the rules most likely to break tools: required fields, path templating, parameters, responses, unique operation IDs and local references. They are not a complete JSON Schema meta-validation, external reference files are not fetched, and generated examples are illustrations rather than real data. Treat a clean result as a good sign, then test the API itself.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages