Quick answer
OpenAPI describes a REST API in one file: the paths, the operations, the request and response shapes, and the error cases, in YAML or JSON. Tools read that file to generate documentation, client code, and mock servers, so the description becomes the contract instead of a wiki page that quietly drifts. This post writes one for a real shop orders API and validates it with Python.
A small clothing shop in Karachi takes orders by phone and WhatsApp, and the owner asks a developer to build an orders API for the website. A week later the frontend developer asks: what does POST /orders return when the order data is wrong? The answer lives in three places: a chat message from last Tuesday, a comment in the code, and the backend developer's memory. None of the three agrees. The API works, but nobody can say exactly what it promises. That gap is what OpenAPI closes.
What I would check before using it
My first check is whether the API is actually REST over HTTP with JSON. OpenAPI describes exactly that shape: resources, verbs, status codes, JSON bodies. If the system is GraphQL, gRPC, or a message queue, OpenAPI is the wrong tool.
My second check is who owns the spec. In design-first, the team writes the spec before the code and reviews it like code, which suits an API with several consumers. In code-first, the framework generates the spec from the code, which is how FastAPI works. A team that has not agreed on one of them ends up with two specs that disagree.
My third check is the OpenAPI version. Version 3.1 aligns with JSON Schema, so types behave the way most developers expect. Version 3.0 is older and still common, and some tools lag behind 3.1, so check the generators you actually use before picking. A version your tooling cannot read is worse than no spec at all.
My fourth check is validation in the pipeline. A spec nobody checks rots within a month, so the file belongs in version control and the build should validate it on every change. My fifth check is authentication: if the API needs a key or a token, describe it with a securitySchemes block, because an undocumented auth step is the most common reason a new consumer's first call fails.
My verdict, as judgement: adopt OpenAPI for any API with more than one consumer, or any API you expect to still be running in a year. It flips for a single-consumer API with a short life, or a design still changing daily.
A Karachi shop orders API, written and validated
The demo is the shop from the intro: three operations, one resource, realistic data. An order is ORD-1043, two Wool Shawls at Rs 1,800 each, total Rs 3,600, status placed. The spec is OpenAPI 3.1.0 in YAML, with the order shape defined once under components.
openapi: 3.1.0
info:
title: Karachi Shop Orders API
description: Orders API for a small clothing shop in Karachi.
version: 1.0.0
servers:
- url: https://api.karachi-shop.example.com/v1
paths:
/orders:
get:
summary: List orders
operationId: listOrders
responses:
'200':
description: A list of orders
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Order'
post:
summary: Create an order
operationId: createOrder
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/NewOrder'
responses:
'201':
description: Order created
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
'400':
description: The order data failed validation
/orders/{id}:
get:
summary: Get one order
operationId: getOrder
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
description: One order
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
'404':
description: No order with that id
components:
schemas:
Order:
type: object
required:
- id
- items
- total
properties:
id:
type: string
example: ORD-1043
items:
type: array
items:
$ref: '#/components/schemas/OrderItem'
total:
type: integer
example: 3600
status:
type: string
enum:
- placed
- packed
- delivered
example: placed
NewOrder:
type: object
required:
- items
properties:
items:
type: array
items:
$ref: '#/components/schemas/OrderItem'
OrderItem:
type: object
required:
- name
- price
- quantity
properties:
name:
type: string
example: Wool Shawl
price:
type: integer
example: 1800
quantity:
type: integer
example: 2
The validator is openapi-spec-validator, installed with pip install openapi-spec-validator pyyaml. A short script loads the YAML and validates it. The first version of this spec had a real mistake: the total field was typed type: int, which looks right if you write Python all day, but JSON Schema has no int type. The validator caught it at once:
$ ./venv/bin/python validate_short.py orders-api-broken.yaml
OpenAPIValidationError: 'int' is not valid under any of the given schemas
On instance: ['properties', 'total', 'type'] -> 'int'
exit code: 1
The fix is one word, integer instead of int; the valid types are string, number, integer, boolean, array, object, and null. While fixing that I also noticed the first draft had no responses block on POST /orders. The validator did not complain: validation checks the shape of what is written, not whether every operation documents its responses. I added the 201 and 400 responses anyway, because a client needs to know what success and bad input look like. Then the same script, run against the fixed file:
$ ./venv/bin/python validate_short.py orders-api.yaml
Spec is valid.
Title: Karachi Shop Orders API | Version: 1.0.0
Server: https://api.karachi-shop.example.com/v1
Paths:
GET /orders listOrders
POST /orders createOrder
GET /orders/{id} getOrder
exit code: 0
Three operations, one contract, and a check any pipeline can run. Once it validates, the same file can feed a documentation page, a mock server, and a test client. That is the point: the frontend developer can now read POST /orders and see the 400 case without asking anyone. Try the spec in Postman, which imports an OpenAPI file and builds a collection from it.
Common mistakes and how to fix them
First, the int mistake above: writing type: int because that is the type name in your language. The validator answers 'int' is not valid under any of the given schemas and points at the exact field. The fix is the JSON Schema names: integer for whole numbers, number for decimals, string for text.
Second, leaving an operation without a responses block. The validator stays quiet, so this one slips through, and the client team is left guessing what the endpoint returns. The fix is a rule for your specs: every operation gets at least one response, including the error cases.
Third, a $ref that points at a schema that does not exist, usually after renaming a schema and missing one reference. The validator fails with PointerToNowhere: '/components/schemas/OrderLine' does not exist, naming the broken pointer. The fix is to correct the name or add the missing schema, then re-run the validator.
Frequently Asked Questions
Is OpenAPI the same as Swagger? Not quite. Swagger was the original name of the specification; today Swagger is the name of the tooling around it, while OpenAPI is the specification itself. Swagger UI is one such tool.
Do I still need OpenAPI if my framework generates documentation already? Yes, in most cases, as judgement. A framework like FastAPI generates an OpenAPI file from your code, the code-first path, and the generated file is still the contract your consumers read. Hand-written specs suit APIs designed before the code exists.
Should I write the spec in YAML or JSON? Either is valid, since they describe the same document. YAML is easier to read and write by hand, which is why most teams use it. Pick YAML for a hand-written spec.
Can I validate a spec automatically? Yes. The validator used here runs from the command line, so it fits in a continuous integration step that fails the build when the spec is invalid. That check stops most of the drift that makes specs untrustworthy.
What to remember
- OpenAPI describes a REST API in one file: paths, operations, shapes, and error cases.
- Type names come from JSON Schema:
integer, notint, andnumberfor decimals. - Every operation needs a
responsesblock, including error cases, even though the validator will not demand it. - Reusable schemas under
componentsdefine the order shape once. - Validate the spec in the pipeline, so the contract stays true.
Where this leaves you
The shop now has a contract its frontend developer can read without asking anyone, and a check that fails the build when the contract breaks. Start with one API you already run: write down its paths and shapes in OpenAPI 3.1, run the validator, and fix whatever it reports. The first spec takes an afternoon, the second takes an hour, because the patterns repeat. From there, generated docs and a mock server are a small step.
Further reading
- OpenAPI Specification 3.1.0
- Swagger OpenAPI Specification Guide
- Postman Explained: Testing APIs Before the Frontend Exists
- FastAPI Explained: Python APIs at Full Speed
- Webhooks Tutorial: Receive and Verify Them in Python
- Django REST Framework Tutorial: A Real Shop Orders API
Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.
Originally published at https://practicaldevblog.blogspot.com/2026/12/openapi-tutorial-rest-api-spec.html

Top comments (0)