Practice & Assessment
Test your understanding of FastAPI Foundations
Multiple Choice Questions
6What does FastAPI use to generate automatic request validation and OpenAPI documentation?
A route is defined as `@app.get('/users/{user_id}')` with `def get(user_id: int, active: bool = True)`. What kind of parameter is `active`?
You have an internal `UserDB` model with a `hashed_password` field. What is the correct way to prevent this field from appearing in API responses?
What is the correct way to return HTTP 201 Created from a POST route?
When using `APIRouter(prefix='/users', tags=['users'])`, what happens when you call `app.include_router(router, prefix='/api/v1')`?
Which server is recommended for running FastAPI in development with auto-reload?
Coding Challenges
1Build a Product Catalog API
Create a FastAPI application with a `Product` Pydantic model (fields: `id: int`, `name: str` min 2 chars, `price: float` greater than 0, `category: str`). Implement four routes: GET `/products/` with optional `category` query filter, GET `/products/{id}` returning 404 if not found, POST `/products/` returning 201, DELETE `/products/{id}` returning 204. Use an in-memory list as the data store. The POST route must use a separate `ProductCreate` model without `id`. Expected output: all routes return JSON; invalid input returns 422 with field details. Estimated time: 20โ25 minutes.
Mini Project
Notes REST API
Build a complete Notes API using all module concepts. Define a `Note` Pydantic model with `id`, `title` (min 3 chars), `content`, `created_at` (datetime, auto-set), and `is_published` (default False). Create a `NoteCreate` model without `id` and `created_at`. Implement routes: GET `/notes/` (list all, optional `published` boolean filter), GET `/notes/{id}` (404 if missing), POST `/notes/` (201, auto-assign id and timestamp), PATCH `/notes/{id}/publish` (set `is_published=True`), DELETE `/notes/{id}` (204). Organize routes in a separate `routers/notes.py` file and mount them in `main.py`. All routes must use correct status codes and response models that exclude internal fields.
