█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/REST APIs/Documentation: OpenAPI and Scalar
50 minIntermediate

Documentation: OpenAPI and Scalar

After this lesson, you will be able to: Write OpenAPI/Swagger specs and generate interactive docs with Swagger UI or Scalar.

OpenAPI is the universal contract format for REST APIs. Generate docs, client SDKs, test mocks. Production APIs without docs feel broken.

Prerequisites:Request validation and error responses

What OpenAPI is

A YAML/JSON spec format for describing REST APIs. From the spec you can generate: interactive docs (Swagger UI, Scalar, Redoc), client SDKs (openapi-generator), server stubs, mock servers (Prism), test suites. Standard since ~2015. Every major tool supports it.

A minimal OpenAPI 3.1 spec

YAML for human reading; convert to JSON for tooling.

tsx
openapi: 3.1.0
info:
title: LastWrite Users API
version: 1.0.0
paths:
/users:
get:
summary: List users
responses:
'200':
description: List of users
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
post:
summary: Create a user
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/CreateUserInput'
responses:
'201':
description: Created
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'422':
$ref: '#/components/responses/ValidationError'
components:
schemas:
User:
type: object
properties:
id: { type: integer }
email: { type: string, format: email }
name: { type: string }
required: [id, email, name]
CreateUserInput:
type: object
properties:
email: { type: string, format: email }
name: { type: string, minLength: 1, maxLength: 100 }
required: [email, name]
responses:
ValidationError:
description: Validation error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

Generate docs from code (Node + Zod)

Single source of truth: Zod schemas → OpenAPI.

python
// npm i @hono/zod-openapi (Hono) OR zod-to-openapi (framework-agnostic)
import { z } from 'zod';
import { extendZodWithOpenApi, OpenAPIGenerator, OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
extendZodWithOpenApi(z);
const UserSchema = z.object({
id: z.number().int(),
email: z.string().email(),
name: z.string(),
}).openapi('User');
const registry = new OpenAPIRegistry();
registry.registerPath({
method: 'get',
path: '/users/{id}',
request: { params: z.object({ id: z.string() }) },
responses: { 200: { content: { 'application/json': { schema: UserSchema } } } },
});
const spec = new OpenAPIGenerator(registry.definitions, '3.1.0')
.generateDocument({ info: { title: 'My API', version: '1.0' } });

💡 Interactive docs in 5 lines

Once you have an openapi.json file, serve it + add Swagger UI / Scalar / Redoc to your app. Result: try-it-out UI for every endpoint, schema previews, code snippets in 10+ languages. Free, no design effort. Scalar (modern, fast) is the popular 2026 choice; Swagger UI is the classic.

Spec-first vs code-first

Spec-first: write OpenAPI, generate server stubs + types. Good for API-first teams. Code-first: write your routes + schemas, generate OpenAPI from them. Good for solo + small teams. Both work. Pick code-first if you're already coding; pick spec-first if you have multi-team contracts.

Common mistakes

Hand-written docs that drift from the code (always wrong eventually). Generating OpenAPI but never serving it (you have a spec but no docs UI). Versioning the spec wrong (every release should bump the `info.version`). Forgetting auth in the spec (clients can't tell which routes need auth).

💡 Mock servers: test consumers before the API exists

A mock server returns example responses for your endpoints before any backend is built, so frontend and mobile teams can start integrating against a contract immediately. Postman Mock Servers let you spin one up from a collection in the Postman UI with no code; Prism does the same from an OpenAPI file on the command line. This unblocks parallel work: the API team builds the real thing while consumers code against the mock, and both meet at the agreed contract. It is a standard professional workflow for any non-trivial API.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Request Validation and Error Responses
Back to REST APIs
Versioning and Backwards Compatibility→