Your First FastAPI Vertical Slice
A FastAPI application connects an HTTP operation to typed Python behavior and automatically publishes an OpenAPI contract. Your first vertical slice will accept a validated ticket, return a response, expose interactive documentation, and run through an automated test. Its in-memory storage is intentionally temporary and not production persistence.
What will you be able to do?
- create and run a FastAPI application;
- define health, create, and read operations;
- inspect OpenAPI rather than trusting assumptions;
- test through HTTP using FastAPI's test client.
How do you build the smallest useful service?
Create src/supportdesk_ai/main.py:
Run and verify:
Open /docs, create a ticket, copy its ID, and read it back. Restart the server and observe that the ticket disappears; this proves storage is process memory.
How do you test through the boundary?
Why does this matter in AI-native systems?
OpenAPI can describe deterministic API operations, but it must not automatically expose every route as an AI tool. Tool schemas require narrower permissions, descriptions, limits, approval policy, and server-bound identity. The same Pydantic rigor will later validate model outputs.
Common mistakes
- --reload in production spawns development behavior; use it locally only.
- Global dictionaries lose data and diverge across workers.
- Returning raw exception text can leak internals.
- Interactive docs are useful but may require authentication or disabling in sensitive production contexts.
- Import-time network calls make startup and tests fragile.
AI Pair-Programmer Prompt
Exercise
Add GET /v1/tickets with a maximum limit of 100 and a deterministic order. Test valid creation, invalid empty input, missing ID, and bounded listing.
Acceptance criteria: OpenAPI documents constraints, all tests pass, and restart behavior is recorded honestly.
Knowledge check
- What generates the OpenAPI request schema?
- Why does data vanish on restart?
- Is every API operation a safe AI tool?
Answers
- FastAPI uses the type annotations and Pydantic models.
- It exists only in a process-local dictionary.
- No; tool exposure needs separate permission and safety design.
Completion checklist
Primary references