Skip to content

FastAPI — Basic (50+ Coding Exercises)

  1. Create a minimal FastAPI app with a GET /health returning {\"status\": \"ok\"}.
  2. Run the app with Uvicorn and verify docs at /docs.
  3. Add GET /items/{item_id} returning the id as JSON.
  4. Add query params limit and offset to GET /items.
  5. Create a Pydantic model ItemCreate(name, price) and POST /items to accept it.
  6. Return 201 on successful create and include a generated id.
  7. Validate price >= 0 and return validation error automatically.
  8. Add response model ItemPublic that excludes internal fields.
  9. Raise HTTPException(404) when item not found in GET /items/{id}.
  10. Implement in-memory CRUD storage for items (dict-based).
  11. Implement PUT /items/{id} to update an item.
  12. Implement DELETE /items/{id} to delete an item.
  13. Add tags to group endpoints in OpenAPI docs.
  14. Create an APIRouter for /items routes and include it in the app.
  15. Create an APIRouter for /users routes and include it.
  16. Implement middleware that adds an X-Request-Id header if missing.
  17. Return the request id in every response.
  18. Add CORS middleware allowing a specific frontend origin.
  19. Add a dependency get_now() and inject it into an endpoint returning server time.
  20. Add a dependency that validates an API key header for a protected endpoint.
  21. Create a settings class loading DATABASE_URL from env (stub if not present).
  22. Add startup event to initialize a global resource (e.g., DB client placeholder).
  23. Add shutdown event to close the global resource.
  24. Add GET /headers that returns selected request headers.
  25. Add GET /cookies that reads a cookie and returns it.
  26. Add POST /upload that accepts a file upload and saves it to disk.
  27. Enforce max upload size (basic check) and reject with 413.
  28. Add POST /form that accepts form fields and returns parsed values.
  29. Add an endpoint that returns plain text using Response.
  30. Add custom JSONResponse with custom headers for one endpoint.
  31. Add error handler for HTTPException to return a consistent error format.
  32. Add error handler for request validation errors to match your error format.
  33. Add /api/v1 router prefix and move endpoints under it.
  34. Add /api/v2/health as a separate versioned endpoint.
  35. Implement a simple “todo” resource with CRUD endpoints in-memory.
  36. Add pagination to list endpoints (limit/offset).
  37. Add sorting for list endpoints (e.g., sort=name).
  38. Add filtering by min/max price for items.
  39. Add a dependency that logs request duration (print or logger).
  40. Add BackgroundTasks to enqueue a dummy task after creating an item.
  41. Add unit tests for /health using TestClient.
  42. Add tests for create/get/update/delete item endpoints.
  43. Add tests for validation errors (negative price).
  44. Add tests for 404 errors on missing item.
  45. Add a simple Makefile or script section (documented) to run the app.
  46. Add an endpoint that returns a list of items with response_model validation.
  47. Add response_model_exclude_none=True and verify output.
  48. Add endpoint that uses path+query+body all together.
  49. Add endpoint that uses a dependency to parse common query params.
  50. Add endpoint that returns current app version from settings.
  51. Add endpoint that accepts a list of items and bulk-creates them.
  52. Add endpoint that deletes all items (protected by API key dependency).