4. FastAPI Core
FastAPI is a high-performance web framework for building Python APIs from standard type hints.
Why FastAPI?
If you are coming from other languages, or even if you have used Python before, you might have heard of frameworks like Flask or Django. Why are we using FastAPI instead?
Over the last few years, FastAPI has risen astronomically in popularity among professional teams for several key reasons:
-
Speed:
FastAPI is built on top of Starlette (a lightweight ASGI framework/toolkit) and Pydantic. It's one of the fastest Python frameworks available, matching the performance of Node.js and Go in many benchmarks.
-
Type-Driven Development:
Because it uses Python's native type hints, it provides excellent editor support, autocompletion, and validation out of the box. You write standard Python, and FastAPI handles the heavy lifting.
-
Automatic OpenAPI Integration:
FastAPI automatically generates interactive API documentation (Swagger UI and ReDoc) based on your code and type hints. You never have to manually write an API specification again.
-
Developer Experience:
It reduces boilerplate significantly. Instead of manually parsing JSON request bodies or validating query parameters, FastAPI does it for you.
FastAPI vs. Flask and Django
- Django is a "batteries-included" framework. It comes with its own ORM (database layer), templating engine, and admin panel. It's fantastic for massive, monolithic web applications, but can feel heavy or opinionated if you just want to build a clean, microservice-style JSON API.
- Flask is a micro-framework that is very unopinionated. However, because it's older, it was built before Python had type hints and native asynchronous capabilities (
async/await). While Flask has added async support recently, FastAPI was built from the ground up for modern Python.
FastAPI hits the sweet spot: it's lightweight like Flask, but provides massive productivity boosts through type hinting and automatic documentation.
Setting Up the Project
Our FastAPI code is in a public repo. Go ahead and clone it.
git clone https://github.com/nnja/python-for-pros.git
cd python-for-pros
uv sync
code . # open in vs code, trust the authors
For each exercise, you'll grab a local copy of the branch with all the files you need.
Add the solutions repo as a second remote so you can diff against it when you get stuck:
git remote add solutions https://github.com/nnja/python-for-pros-solutions.git
git fetch solutions