YAML is a human-readable data format commonly used to describe REST APIs, especially in OpenAPI specifications. You write the API's endpoints, parameters, and responses in YAML, and tools read that file to generate documentation, client code, and tests.
YAML Describes the API, Not the Traffic
A REST API is the running service; YAML is the written description of it. The YAML file is a contract: it lists each endpoint, the HTTP method it accepts, the parameters it takes, and the shape of the response. Tools read that contract and generate documentation pages, client libraries, and mock servers. The API itself still sends and receives JSON or XML at runtime; YAML is only the blueprint.
This separation matters because the description can be reviewed and versioned like code. A change to an endpoint shows up as a diff in the YAML file, so reviewers can see exactly what changed before it ships.
OpenAPI Is the Specification YAML Usually Fills
Most REST API YAML files follow the OpenAPI Specification, formerly called Swagger. OpenAPI defines which fields are allowed and what they mean, and YAML is one of the two formats it accepts (JSON is the other). A minimal OpenAPI file in YAML looks like this:
openapi: 3.0.0
info:
title: Example API
version: 1.0.0
paths:
/users:
get:
summary: List users
responses:
'200':
description: A list of users
The paths block is the heart of the file. Each key under it is a URL path, and under each path you list the HTTP methods (get, post, put, delete). Inside each method you describe parameters, request bodies, and responses. The info block names the API and its version, which tools display at the top of generated docs.
Reading a YAML API File in Five Parts
Every OpenAPI YAML file has the same skeleton. Once you recognise the five parts, any API description becomes readable.
- Version and info. The
openapifield states which version of the specification the file uses. Theinfoblock holds the title, version, and a short description. - Servers. The
serverslist gives the base URLs the API lives at, such as a production URL and a staging URL. - Paths. Each entry under
pathsis an endpoint. The method beneath it (get, post, and so on) holds the parameters, request body, and responses for that endpoint. - Components. Reusable pieces such as schemas, parameters, and security schemes live under
components. Paths refer to them by name instead of repeating the same definition. - Security. The
securityblock names which authentication scheme applies, such as an API key or a bearer token, and components define what that scheme looks like.
Indentation carries the meaning in YAML. Two spaces per level is the common convention, and a misplaced space changes the structure. If a tool reports a parse error, check indentation before anything else.
Why Teams Write API Definitions in YAML
YAML is easier to read than JSON for most people because it drops the braces and quotation marks. A reviewer can scan a YAML endpoint and see the method, path, and response shape without mentally parsing brackets. That readability is why most public OpenAPI files are published in YAML even though JSON works equally well.
The format also plays well with version control. Because each line is a discrete fact, a change to one parameter appears as a single-line diff. Tools can then compare two versions of the file and flag breaking changes, such as a removed field or a renamed endpoint, before they reach users.
Generating Docs and Client Code from the File
Once the YAML file exists, tools do the repetitive work. Documentation generators turn it into a browsable reference page where readers can try requests. Client generators produce typed code in languages such as Python, Java, or TypeScript, so callers do not hand-write request logic. Mock servers read the file and return example responses, which lets front-end work proceed before the real API is finished.
The catch is that the file must stay accurate. A generated client is only as correct as the YAML it came from. Teams often add a validation step to their build so that a malformed or outdated file fails before it is published.
Checking That Your YAML Matches the API
A description that drifts from the running service causes real confusion. These checks catch the common gaps:
- Compare each path in the YAML against the routes the service actually exposes.
- Confirm that every response code listed in the file is one the API can return.
- Check that required fields in the schema match what the API rejects when missing.
- Run a validator against the specification version named in the file.
- Regenerate docs and clients after every change, and review the diff.
If a field appears in the YAML but not in real responses, either the file is stale or the API has a bug. Either way, the mismatch is worth fixing before users rely on the description.
When YAML Is the Wrong Choice
YAML suits hand-written descriptions and files that people review often. For machine-generated output or very large specifications, JSON is sometimes preferred because it is unambiguous and every programming language parses it without a YAML library. Very deeply nested structures also become hard to read in YAML once indentation passes four or five levels.
For most REST API descriptions, though, YAML remains the default. It is readable, diff-friendly, and supported by the same tools that read JSON, so switching formats rarely gains anything.