Use Case 2: Templates โ Copyable Reference Applications
openapi-first ships four runnable, copyable applications under openapi_first/templates/. They are not part of the library API โ they are bundled scaffold examples you copy into your own project and build on.
๐ฌ 1. What Templates Are
A template is a complete, self-contained OpenAPI-first service:
- A bundled directory inside
openapi_first/templates/<name>/ - Copyable verbatim โ no code generation, no mutation โ via the CLI
- Each one demonstrates a specific set of OpenAPI-first behaviors and FastAPI features
All templates share the same skeleton:
๐ 2. The Four Templates
2.1 health_app โ minimal liveness probe
| File | Purpose |
|---|---|
openapi.yaml |
GET /health โ operationId: get_health |
routes.py |
get_health() returns {"status": "ok"} |
main.py |
OpenAPIFirstApp(openapi_path="openapi.yaml", routes_module=routes) |
Why it exists: the absolute minimal OpenAPI-first round trip โ one operation, one handler, zero moving parts. The best starting point to internalize the mental model.
Smoke test:
2.2 crud_app โ dict-based CRUD
| File | Purpose |
|---|---|
openapi.yaml |
Full CRUD over /items (list/get/create/update/delete) |
routes.py |
Handlers bound via operationIds list_items, get_item, create_item, update_item, delete_item |
data.py |
In-memory dict store with auto-incrementing id |
Behaviors you learn:
- Explicit status codes โ
create_item/delete_itemtakeresponse: Responseand set201/204;get_item/update_itemraiseHTTPException(404)onKeyError - Handlers as plain callables โ no FastAPI decorators, routing comes solely from the spec
- Mock data store โ
data.pyis a copyable in-memory store, explicitly not production-ready
2.3 model_app โ Pydantic model CRUD
| File | Purpose |
|---|---|
openapi.yaml |
Same CRUD surface, schemas reference models |
models.py |
Pydantic Item, ItemCreate, ItemBase (request/response models) |
routes.py |
Handlers type-annotated with the models; create_item sets 201 |
data.py |
In-memory store returning real model instances |
Behaviors you learn:
- Pydantic request/response models โ payloads validated and serialized via FastAPI
- Same handler contracts โ identical
operationIdset ascrud_app, so the two are interchangeable - Models in the client too โ the same spec drives
OpenAPIClientbody handling
2.4 vet_app โ the full-featured demo
| File | Purpose |
|---|---|
openapi.yaml |
Five resources (parents, vets, treatments, pets, appointments) + SSE + upload + discriminated unions |
models.py |
Pydantic models incl. discriminated unions (noteType literal fields) |
routes.py |
~20 handlers across all resources, incl. pagination, filtering, photo upload, SSE streaming |
sse.py |
Server-Sent Events helper (StreamingResponse, per-pet subscriber queues, background asyncio workers) |
data.py |
Larger in-memory store (parents โ vets โ treatments โ pets โ appointments) |
main.py |
App + CORS + lifespan example |
Behaviors you learn โ the advanced tier:
- Discriminated unions โ
ProcedureNotesusesoneOf+discriminator.noteTypemapping; Pydantic models useLiteral[...]discriminator fields - SSE streaming โ a
GET /pets/{id}/actionsoperation streamingtext/event-streamviaStreamingResponsewith background task workers - File upload โ
UploadFilehandler setting a multi-part body - CORS + middleware โ
add_middleware(CORSMiddleware, ...)alongside the spec-driven setup - Response injection โ handlers set
201/204explicitly via injectedResponse
๐ 3. CLI Reference
Protip:
DEFAULT_TEMPLATEishealth_app, soopenapi-first scaffoldwith no template name scaffolds the health app.
๐งฉ 4. Anatomy of a Scaffolded Service
After openapi-first scaffold health_app my-health-service, your directory contains a drop-in FastAPI service:
Run it:
/docs, /openapi.json, and every declared route now exist โ all derived from openapi.yaml.
โ ๏ธ 5. Production Disclaimer
Templates use in-memory, non-persistent, non-concurrency-safe data stores. They are learning scaffolds โ not production references. Swap in a real data layer (SQL/REDIS/object store) the moment you go beyond a demo.
See the __init__.py of each template for detailed client examples, CLI examples, and design notes.