Building APIs with FastAPI

Build a real JSON API with FastAPI: type-hinted endpoints, Pydantic validation, a database, JWT logins, background tasks, CORS, tests and deployment.

  • 0 students
  • 93 lessons
  • 7 h 40 min

Created by Ayim Nelson

What you will learn

  • Explain what FastAPI is and when to choose it over Django, DRF or Flask
  • Build endpoints with typed path parameters, query parameters and Pydantic request bodies
  • Read 422 validation errors and return the right status codes, errors and response models
  • Share logic with dependencies and store data with SQLModel, using a session per request
  • Protect an API with the OAuth2 password flow, hashed passwords and JWT access tokens
  • Use async def wisely, run background tasks and set up CORS for a React front end
  • Test an API with TestClient and pytest, configure it with pydantic-settings and deploy it

About this course

FastAPI is a modern Python framework for building APIs. You write ordinary functions with type hints, and FastAPI uses those hints to read and check incoming data, turn your results into JSON and write interactive documentation for you. In this course you build the Trotro Fares API, a service that lists trotro routes and fares around Accra, one short lesson at a time. Every response, error and terminal output you see came from running the real code on FastAPI 0.165 and Pydantic 2.

For: anyone who has finished the Python Crash Course. Python Beyond the Basics (type hints, dataclasses) and How the Web Works (HTTP, status codes, JSON) help a lot. You'll build the API on your own computer; the exercises run right here in the browser.

By the end: you'll have a tested JSON API backed by a database, where anyone can read routes and fares, signed-in users add and update them with a JWT, admins delete them, fare changes are logged in the background, and a React front end can call it all. You'll also know how to configure it with settings and deploy it.

Course content

11 sections · 93 lessons

Meet FastAPI9 lessons
Path and query parameters8 lessons
  • Path parametersReading · 4 min
  • Typed path parametersReading · 4 min
  • Fixed paths before path parametersReading · 3 min
  • Choices with EnumReading · 4 min
  • Query parametersReading · 4 min
  • Required and optional query parametersReading · 4 min
  • Checking values with Query and PathReading · 4 min
  • Build a path-parameter routerExercise · 15 min
Request bodies with Pydantic11 lessons
  • What Pydantic doesReading · 4 min
  • Pydantic validation errorsReading · 4 min
  • Field limits and defaultsReading · 4 min
  • Nested modelsReading · 4 min
  • A request body in FastAPIReading · 4 min
  • Sending a bodyReading · 3 min
  • Body, path and query togetherReading · 4 min
  • Tidying values with field_validatorReading · 4 min
  • Checking fields together with model_validatorReading · 4 min
  • Turning models back into dataReading · 3 min
  • Check fare reports with PydanticExercise · 12 min
Responses, status codes and errors7 lessons
  • When the body is wrong: 422Reading · 4 min
  • Reading the error listReading · 4 min
  • Response modelsReading · 4 min
  • Status codesReading · 4 min
  • Not found: HTTPExceptionReading · 4 min
  • Deleting with 204 No ContentReading · 3 min
  • Build a 422 error listExercise · 14 min
Automatic documentation7 lessons
  • Interactive docs at /docsReading · 4 min
  • The OpenAPI documentReading · 4 min
  • A path operation in the schemaReading · 4 min
  • Models in the schemaReading · 4 min
  • Titles, tags and descriptionsReading · 4 min
  • Examples in the docsReading · 3 min
  • Make a schema from a dataclassExercise · 12 min
Dependencies6 lessons
  • What a dependency isReading · 4 min
  • Reusing a dependency with AnnotatedReading · 3 min
  • Dependencies that check requestsReading · 4 min
  • Dependencies with yieldReading · 4 min
  • Dependencies that use dependenciesReading · 4 min
  • Write a tiny dependency resolverExercise · 15 min
A database with SQLModel12 lessons
  • Why SQLModelReading · 4 min
  • Table modelsReading · 4 min
  • Models for input and outputReading · 4 min
  • The engine and creating tablesReading · 4 min
  • A session for each requestReading · 4 min
  • Splitting the app with APIRouterReading · 4 min
  • Creating rowsReading · 4 min
  • Listing rows a page at a timeReading · 4 min
  • Reading one row or 404Reading · 3 min
  • Updating with PATCHReading · 4 min
  • Deleting rowsReading · 3 min
  • Build CRUD with pages on SQLiteExercise · 15 min
Logins with OAuth2 and JWT12 lessons
  • How logging in to an API worksReading · 4 min
  • Hashing passwords with pwdlibReading · 4 min
  • Users and signing upReading · 4 min
  • Checking a password at loginReading · 4 min
  • The /token endpointReading · 4 min
  • What's inside a JWTReading · 4 min
  • Making and checking tokens with PyJWTReading · 4 min
  • Finding the current userReading · 4 min
  • Protecting routesReading · 4 min
  • Admins only: 403Reading · 4 min
  • Keeping tokens safeReading · 4 min
  • Sign and check a JWT by handExercise · 15 min
Async, background tasks and CORS8 lessons
  • async def and awaitReading · 4 min
  • def or async def in FastAPIReading · 4 min
  • Calling other services with httpxReading · 3 min
  • Background tasksReading · 4 min
  • What CORS isReading · 4 min
  • Allowing your React appReading · 4 min
  • Calling the API from ReactReading · 4 min
  • Simulate an event loopExercise · 15 min
Testing, settings and deploying10 lessons
  • Testing with TestClientReading · 4 min
  • A test database with dependency_overridesReading · 4 min
  • Writing API testsReading · 4 min
  • Running the testsReading · 3 min
  • Settings with pydantic-settingsReading · 4 min
  • Settings in actionReading · 3 min
  • .env files and secretsReading · 4 min
  • Running in production with fastapi runReading · 4 min
  • Deploying the APIReading · 4 min
  • Load settings like pydantic-settingsExercise · 14 min
Wrapping up3 lessons
  • What you builtReading · 3 min
  • Where to go nextReading · 3 min
  • Final quizQuiz · 10 min

Reviews

No reviews yet.

Comments

Log in to join the conversation.

Loading comments...