Six months from now, a new developer on the Bookshop team wants to add a feature. They ask: which endpoints need a token? Is author_id required or optional? When something fails, what does the error look like? If the answers live only in your head, they have to wait for you to reply in chat. And the one asking could even be you, after you have forgotten.
Those answers are written down in API documentation: the manual for other developers, and for you in six months. It says which endpoints exist, what to send, what comes back, and what can go wrong. It's like a restaurant menu. A good menu lists each dish, the spice levels to choose from, what's in a portion, a photo, and a note saying "if it's sold out, you get fried rice instead". You can order without calling the waiter for every question. In an API, the dish is the endpoint, the spice level is a parameter, the portion is the response, the photo is an example, and the "sold out" note is the list of errors.
OpenAPI: one file, read by people and machines
The OpenAPI Specification (once called Swagger) is the industry standard for writing REST API documentation as one YAML or JSON file. YAML is a text format for data, like JSON, but its levels are built with indentation (spaces at the start of a line). An OpenAPI file is called a spec. This lesson uses version 3.1, even though the newest version is 3.2 (published September 2025). Version 3.0 is still widely used too, and for a spec this simple it's written the same way as in 3.1. Because the format is fixed, one file serves many tools:
- Swagger UI or Redoc turn it into a documentation page. On a Swagger UI page, you can also try each endpoint directly.
- A validator checks real requests and responses against the file.
- A generator writes client code from the file.
In Laravel, Scribe or L5-Swagger can build the file from your code. In Node, swagger-jsdoc for instance.
The spec for GET /books
This is the documentation for the Bookshop's GET /books endpoint, which can be filtered with ?author_id=2:
openapi: 3.1.0
info:
title: Bookshop API
version: 1.3.0
servers:
- url: https://api.bookshop.test
paths:
/books:
get:
summary: List books, optionally by author
parameters:
- name: author_id
in: query
required: false
schema:
type: integer
responses:
"200":
description: A list of books
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Book"
"400":
description: author_id is not a whole number
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500":
description: Something broke on the server
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
components:
schemas:
Book:
type: object
required: [id, title, author_id, price, stock]
properties:
id:
type: integer
title:
type: string
author_id:
type: integer
price:
type: integer
stock:
type: integer
Error:
type: object
required: [error]
properties:
error:
type: string
- YAML builds its levels with indentation in spaces (never tabs). A line indented further belongs to the line above it. A
-opens one list item. paths→/books→get: one endpoint, written as its address, then its method.parameters: the list of inputs.- name: author_idwithin: querymeans the value comes in the query string (?author_id=2).required: falsemeans it doesn't have to be sent.schema: type: integermeans the value must be a whole number.responses: one key per status code. Write the code in quotes,"200":, so YAML reads it as text, not a number.content→application/json→schema: the shape of the body. For200, the shape istype: array, and each entry (items) is a Book.
In the exercise, you use the same shape for GET /api/products, with a category parameter of type string, responses "200" and "500", and a Product holding id, name, price.
Schema: the shape of the data
A schema describes the shape of a piece of data. It has:
type:object,array,string,integer(a whole number),number(decimals allowed),boolean.properties: which fields an object may have, each with its own schema.items: the shape of each entry in an array.required: the fields that must be there. This is the part people forget:propertiesonly describes each field when it is there, and requires none of them. So withoutrequired, a book with nostockcounts as valid.$ref: "#/components/schemas/Book": points at a schema written once undercomponents/schemas. Book is used by many endpoints, so its shape is written once and then referred to. The value is in quotes because#starts a comment in YAML.
Check the docs against real responses
Stale documentation (no longer true to its code) is worse than none, because developers confidently follow the wrong instructions. For example, a new server version sends "price": "89000" (text) while the docs say integer. An app that adds up prices joins text instead: "89000" + 1000 becomes "890001000".
Because the spec is machine-readable, this mistake can be checked automatically. A contract test sends a request, then matches its response against the spec. Here are the results for two requests:
GET /books?author_id=1 -> 200 | response: body[0].price: must be integer, came as string "89000"
GET /books?author_id=two -> 400 | request: author_id=two is not an integer | response: fits the spec
The first line fails: price came as text, but the spec asks for integer. In the second line, the request is wrong (author_id=two is not an integer), and its 400 answer fits the spec.
The principle is called docs-as-code (treating documentation like code). The spec file lives in the same repo (repository) as the code, changes in the same pull request (a proposed code change), and is checked in CI (automatic checks on every code change). This way the documentation is updated together with the code, so it doesn't go stale.
What good documentation contains
- Examples of requests and responses to copy right away, for instance with
example:
example:
- id: 2
title: This Earth of Mankind
author_id: 2
price: 125000
stock: 0
- Every error: each status and its body shape (
{"error": "..."}), as in the Error Handling lesson. - Authentication: how to get a token and how to send it (the Authentication lesson). In OpenAPI, the way to send the token is written once under
components:
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
then security: [{ bearerAuth: [] }] is written on the endpoints that need a login, such as POST /books.
- Limits: rate limits and pagination (the Rate Limiting and Pagination lessons).
- A changelog per version: a list of what was added, changed and removed, for example "1.3.0: added the author_id filter to GET /books". A change that breaks clients needs a new version, as in the API Versioning lesson.
⚠️ Docs with holes, or gone stale
- Forgetting
required. A missing field gets through without anyone noticing. List every mandatory field inrequired. - Documenting only
200. Clients don't know that400and500can come, or what their bodies look like. List the errors too. - Wrong YAML indentation, or tabs. An
in: queryshifted by one level belongs to another section. Use consistent spaces. - Changing the spec apart from the code. Over time the spec goes stale. Change it in the same PR (pull request), and check it with a contract test.
TL;DR: API documentation says what to send, what comes back, and what can go wrong. The industry standard is OpenAPI (once Swagger): one YAML or JSON file (YAML: a text format for data whose levels are built with indentation). That file is read by people (through Swagger UI or Redoc pages) and machines (validators, generators). An endpoint is written under paths, with parameters (such as - name: author_id, in: query, schema: type: integer) and responses per status code ("200", "400", "500"), each with a schema (the shape of the data). An array uses type: array and items, an object uses properties, and mandatory fields must be listed in required. A schema used again and again is written once under components/schemas, then referred to with $ref. Good documentation has examples, every error, authentication and a changelog (a record of changes per version). It is also updated in the same PR (a proposed code change) as the code, so it doesn't go stale (no longer true to the code). This is called docs-as-code.
⚡ OpenAPI: one file, read by people and machines
Meaning: OpenAPI (once called Swagger) is the industry standard for writing REST API documentation as one YAML or JSON file (YAML is a text format for data, like JSON). Swagger UI and Redoc turn it into a documentation page. A validator checks real requests and responses against the file. A generator writes client code from the file.
⚡ One endpoint in the spec
Write:
paths:
/books:
get:
summary: List books, optionally by author
parameters:
- name: author_id
in: query
required: false
schema:
type: integer
Meaning: A spec is an OpenAPI file, and the endpoint GET /books is written under paths. in: query means the value of author_id comes in the query string (?author_id=2), required: false means it doesn't have to be sent, and type: integer means it must be a whole number. YAML's levels are built with indentation in spaces, never tabs.
⚡ The required people forget
Guess first: the Book schema (the shape of the data) has properties, its list of fields, but no required, the list of fields that must be there. Does a book with no stock count as valid?
It does. properties only describes each field when it is there, and requires none of them.
Meaning: List every mandatory field in required.
⚡ The docs change along with the code
Meaning: Stale documentation (no longer true to its code) is worse than none, because developers confidently follow the wrong instructions. Keep the spec file in the same repo (the place where a project's code is kept) as the code. Change it in the same pull request (a proposed code change) as the code, and check it in CI (automatic checks). This is called docs-as-code.