What you are designing
An API is the interface your product offers to other software. OpenAPI is the file that describes it precisely enough that people, tools and coding agents can all build from the same page.
You already design interfaces. A screen has a layout, labels and states, and a person learns it by looking. An API has addresses, field names and status codes, and a developer or an AI agent learns it by reading. If the names are inconsistent or a failure state was never designed, they struggle in the same way a person struggles with a confusing screen.
API stands for application programming interface. The APIs in this tutor are HTTP APIs, the kind a web or mobile app calls to load and save data. Each call is a request (a method, an address and sometimes a body of data) that gets one response (a status code and usually a body).
# request: the app asks to create an invoice POST /v1/invoices Content-Type: application/json { "customer_id": "cus_208", "total": 48000, "currency": "CAD" } # response: the API says it worked and returns the new invoice 201 Created { "id": "inv_1042", "status": "draft", "total": 48000, ... }
An OpenAPI description is a text file, usually openapi.yaml, that lists every request the API accepts and every response it can give. Because the format is a standard, tools can turn the file into documentation, a mock server that answers with example data, client code, and tests. Writing this file before any code exists is called design-first. It is the API equivalent of agreeing a wireframe before the build.
The three things you decide
Resources
The things your product is about: invoices, customers, appointments. Each gets an address, such as /invoices for the collection and /invoices/inv_1042 for one item.
Decide what exists and what it is called.
Operations
What a caller may do to a resource. An operation is an HTTP method plus a path: GET /invoices reads the list, POST /invoices creates one.
Decide what can be done, and what can go wrong.
Schemas
The fields of each resource: their names, types, which are required and which values are allowed. A schema is a form specification for data.
Decide what the data looks like, field by field.
Check yourself: which part of the design is it?
“Our product is about appointments, patients and vets.”
“The receptionist can cancel an appointment.”
“An appointment has a start time, a vet and a status that is booked, done or cancelled.”
How to define it
Start from what callers need to get done, find the nouns, then work down to operations, fields and failures. Do not start from your screens or your database tables.
- List the jobs. Write five to ten sentences describing what a client app must be able to do. “Show the invoice list, filtered by status.” “Send an invoice to the customer.”
- Find the nouns. Underline the things in those sentences. The ones with their own identity and life cycle are your resources.
- Map each job to an operation. Pick a method and a path on a resource. Most jobs are one of five: list, read one, create, change, delete. The rest are named actions, such as send or cancel.
- Shape the data. For each resource, list the fields with a type, a description and an example. Mark what is required and what only the server sets.
- Design the failures. For each operation, decide which errors a caller can meet. Every one is a state some screen has to show.
- Review, mock, repeat. Read the contract as if you had to build a screen from it. A mock server lets a front end try it before the back end exists.
The methods
| Method | Means | Safe to repeat? |
|---|---|---|
| GET | Read. Changes nothing. | Yes |
| POST | Create something, or run a named action. | No, unless you design for it |
| PATCH | Change some fields of one item. | Usually |
| PUT | Replace one item entirely. | Yes |
| DELETE | Remove one item. | Yes |
Anatomy of an OpenAPI file
This is one operation and part of one schema. Select any underlined part to see what it is for.
paths:
:
:
: Send an invoice
: sendInvoice
: Emails the invoice to the customer's
billing contact and moves it from draft to sent.
:
- name: invoiceId
in: path
required: true
schema: { type: string }
:
'200':
description: The invoice, now sent.
content:
application/json:
schema:
: '#/components/schemas/Invoice'
:
$ref: '#/components/responses/Conflict'
:
schemas:
Invoice:
type: object
: [customer_id, total]
properties:
id: { type: string, : true }
total:
type: integer
: Amount in cents.
: 48000
status:
type: string
: [draft, sent, paid, void]
due_date:
type: string
: date
path
What comes back: every status code is a screen state
The first digit tells the story. 2xx means it worked. 4xx means the caller has to change something. 5xx means the server failed and the caller did nothing wrong. When you add a status code to an operation, you are also adding a state to every screen that calls it.
| Code | Means | What the screen does |
|---|---|---|
| 200 OK | Worked, here is the data. | Show it. |
| 201 Created | Worked, a new item exists. | Confirm and go to the new item. |
| 204 No Content | Worked, nothing to return. | Remove the row, confirm quietly. |
| 401 Unauthorized | Not signed in, or the session expired. | Send the person to sign in. |
| 403 Forbidden | Signed in, but not allowed. | Explain the permission, offer no retry. |
| 404 Not Found | No such item. | Show a “no longer exists” state. |
| 409 Conflict | Valid request, wrong moment. Sending an invoice that was already sent. | Refresh and explain what changed. |
| 422 Unprocessable | Some fields are not acceptable. | Mark the fields, keep what was typed. |
| 429 Too Many Requests | Slow down. | Wait and retry automatically. |
The app asks to send invoice 1042, which was already sent yesterday. What should the API answer?
The receptionist changes only the time of an appointment. Which method fits?
Constraints to design around
Each constraint below changes a design decision. The line at the bottom of each card says which one.
You cannot update the callers
Once an app in someone's pocket or a partner's integration depends on your API, it keeps sending the old requests for years.
So: treat the contract as permanent. Add freely, but never rename, remove or retype what exists.
Screens change faster than data
An endpoint shaped like a screen, such as “dashboard data”, breaks at the next redesign and is useless to any other client.
So: model the things the product is about. Resources survive redesigns.
The network fails halfway
A request can succeed on the server while the reply is lost. The app, or the person, tries again.
So: make reads and deletes safe to repeat, and let creates carry an idempotency key so a retry does not make a duplicate.
Lists grow
Forty invoices today is forty thousand in three years. A list with no limit works in the demo and times out in production.
So: every list has a default and a maximum page size, and a way to ask for the next page.
The caller is not trusted
Anything can call an API, with any data. The app's form validation protects the person, not the server.
So: state every rule in the schema, check permissions on every call, and never take the account from the request body.
Every failure needs words
A bare “400” leaves the screen with nothing to show. Front-end teams end up inventing messages.
So: one error shape for the whole API, with a readable title, a stable code, and which field is wrong.
Money and time are traps
Decimal numbers lose cents in transit. A date without a time zone means a different day in Vancouver and in Paris.
So: money as whole cents with a currency. Dates as 2026-12-01, moments as UTC timestamps.
Payloads cost data and battery
Too little in a response forces five follow-up calls. Too much is slow on a phone.
So: return what the common screens need, including a display name beside each ID.
Machines read it literally
Code generators and AI agents build from your names, descriptions and examples, with no chance to ask what you meant.
So: describe every field, give examples, and name things as you want them to appear in code.
Breaking or safe?
A change is breaking if an app written against the old contract can stop working under the new one. Judge each change to an API that is already in use.
Add an optional notes field to the invoice.
Rename total to amount_cents because the new name is clearer.
Make due_date required when creating an invoice.
Add a new status value, overdue, that the API can now return.
You really do need to change the shape of the invoice list. What is the right route?
Best practices
Ten habits, each with a before and after. The workbook checks most of them for you.
1. Name resources, not screens
Plural nouns for collections, and an ID for one item.
GET /dashboardData, GET /invoiceScreenGET /invoices, GET /invoices/{invoiceId}2. Let the method be the verb
Paths are nouns in lowercase with hyphens. The one exception is a real business action that is more than saving fields: put it after the item, as a POST.
POST /createInvoice, POST /deleteInvoice, GET /send_invoice?id=1042POST /invoices, DELETE /invoices/{invoiceId}, POST /invoices/{invoiceId}/send3. One naming style, everywhere
Pick one case for field names and keep it. Use the same word for the same thing in every resource. A caller who has learned one part of the API should be able to guess the rest.
custName, Due_Date, createdOn, client_id on one resource and customer_id on anothercustomer_name, due_date, created_at, customer_id everywhere4. Make fields hard to get wrong
Give every field a type, a description and an example. List allowed values instead of describing them. Mark fields the server sets as read-only, and keep the required list short.
status: string, price: 480.00, date: "12/01/26"status: one of draft, sent, paid, void. total: 48000 in cents with a currency. due_date: "2026-12-01"5. Prefer a status to a pile of booleans
is_sent and is_paid allow combinations that make no sense, and a third state needs a third flag. One status field with named values has room to grow.
6. Design errors like interface copy
Use one shape for every error. This tutor uses the standard one, “problem details” (RFC 9457): a readable title, the status, a detail saying what to do, and an errors list naming each wrong field with a stable code.
{ "error": "Bad Request" }{ "title": "Some fields need attention", "status": 422, "errors": [{ "field": "total", "code": "required", "message": "Enter the invoice total." }] }7. Page every list, and wrap it
Give lists a limit with a default and a maximum, and a cursor for the next page. Return the items inside an object, so there is room for the cursor now and other details later without breaking anyone.
[ {...}, {...}, ... 40,000 more ]{ "data": [ {...}, {...} ], "next_cursor": "c_8fj2k" }8. Say what else happens
A summary names the job in a few words. A description says what a caller cannot guess: side effects, preconditions and limits. “Emails the customer” is a fact someone needs before they wire up a button.
9. Version from day one, then only add
Put /v1 in the base address before anyone depends on it. After that, add optional things freely and treat everything else as a new version.
10. Review it like a design
Walk every job through the contract and count the calls. Mock it and build one screen against it. Run a linter for consistency, the way you would run a design lint on a file. Most fixes are names and missing failure states.
Check yourself
Which field design will cause the fewest bugs?
The mobile team wants one endpoint that returns everything for the home screen. What is the better offer?
Further reading
- OpenAPI: getting started, the official introduction to the format
- OpenAPI Specification, the authoritative reference
- Zalando RESTful API guidelines, a thorough public style guide with reasons
- Microsoft REST API guidelines, another widely used house style
- RFC 9457, Problem Details, the standard error shape
- Stripe API reference, a well-regarded example of naming, errors and pagination
- Prism, a mock server that answers from an OpenAPI file
- Redocly CLI, a linter and documentation builder
- oasdiff, which compares two versions of a contract and lists breaking changes
Practice project: rescue the clinic API
Paws & Claws, a small vet clinic, had an API written one screen at a time. The new mobile app cannot be built on it. Your job is to redesign the contract.
The broken design has verbs in its paths, an endpoint named after a screen, a price stored as a decimal, a status anyone can set to anything, a list with no limit, and no designed errors at all. Load it into the workbook and the review panel will list every problem.
Your brief
- The reception app has three screens: today's appointments, a booking form, and an appointment's details with a Cancel button.
- Write those as jobs first. Then find the resources. Appointment is one. Is the pet another?
- Redesign the operations so each screen needs one or two calls, and each job has its failure states.
- Get the review panel to zero issues.
- Open the spec and read it as if you were the coding agent. Would you know what to build?
Appointment and Patient, and about five operations: GET /appointments with a date filter, POST /appointments, GET /appointments/{appointmentId}, POST /appointments/{appointmentId}/cancel and GET /patients.Then do it for your own product
Clear the workbook, write the jobs first, and only then add resources and operations. If the product already has screens, keep the design file closed until the jobs are written.
Design workbook
Fill this in from top to bottom. The review panel updates as you type, and step 7 turns it into a spec.
Spec for your coding agent
Built from the workbook. Paste the brief into Claude Code or another coding agent as the task. It contains the OpenAPI file, which you can also copy on its own for a mock server or a linter.