Overview
FastAPI is a high-performance web framework for building APIs with Python. Its key features include:
- Type hint-based: Leverages Python type hints for automatic request/response validation
- Auto-generated documentation: Swagger UI (OpenAPI) is automatically generated, allowing you to test APIs from the browser
- High performance: Built on Starlette + Pydantic, delivering performance comparable to Node.js and Go
In this article, we’ll walk through building a TODO app CRUD API using FastAPI.
Prerequisites
- Python 3.11 or higher
- uv installed (see Setting Up a Python Development Environment on Mac with UV)
Environment Setup
First, create a project and install the required packages.
| |
fastapi is the web framework itself, and uvicorn is the ASGI server.
Hello World
Let’s start by verifying FastAPI works with a minimal setup. Create a main.py file.
| |
Start the server:
| |
The --reload option automatically reloads the server when code changes are detected.
Open http://localhost:8000 in your browser and you’ll see:
| |
Swagger UI
One of FastAPI’s greatest features is its auto-generated API documentation. Visit http://localhost:8000/docs to see the Swagger UI.
You can test APIs directly from here, eliminating the need for curl or Postman during development.
Implementing the CRUD API
Let’s implement the CRUD API for a TODO app.
Defining Models
First, define the TODO data structure using Pydantic models. Replace main.py with the following:
| |
TodoCreate: Request body schema for creating and updating TODOsTodoResponse: Response type definitiontodos: In-memory data store (dict)next_id: Auto-incrementing ID counter
Create (POST)
| |
By specifying response_model=TodoResponse, the response type is reflected in the Swagger UI.
List (GET)
| |
Get by ID (GET)
| |
Returns a 404 error via HTTPException when the ID doesn’t exist.
Update (PUT)
| |
Delete (DELETE)
| |
Complete Code
Here’s the full main.py with all endpoints combined:
| |
Testing
Start the server and test through the Swagger UI.
| |
Visit http://localhost:8000/docs and test in the following order:
1. Create a TODO
Open POST /todos, click “Try it out”, and enter:
| |
Click “Execute” to get a response like:
| |
2. List TODOs
Execute GET /todos to see the list of created TODOs.
3. Update
Use PUT /todos/1 to change completed to true:
| |
4. Delete
Execute DELETE /todos/1 to delete the TODO. Verify with GET /todos that an empty array is returned.
Summary
In this article, we built a TODO CRUD API using FastAPI. Here are the key advantages:
- Automatic request/response validation with Pydantic models
- Interactive API documentation via Swagger UI
- Simple API specification definition using just Python type hints
We used an in-memory data store in this tutorial, but in real applications, you would use an ORM like SQLAlchemy to connect to a database.
Related Articles
- Setting Up a Python Development Environment on Mac with UV - Python environment setup
- Event-Driven Backend for Syncing GCS with Gemini File Search API - Practical FastAPI example