🚀 Build a Todo App API with FastAPI + uv (The Cleanest Way!)
Source: Dev.to
Introduction
Want to build a lightning‑fast API without the Python packaging mess?
In this guide we’ll create a Todo API using FastAPI and uv, a next‑gen Python package manager that automates virtual environments, handles dependency resolution, and stays out of your way.
What is uv?
uv is a modern Python package manager that replaces pip and virtualenv with:
- 🚀 Ultra‑fast dependency installation
- 📦 Automatic virtual environment creation
- 🧼 Cleaner project setup with zero
requirements.txt
Built in Rust, it’s ideal for fast, reproducible Python workflows—perfect for APIs, CLI tools, and data apps.
Project Setup
mkdir fastapi-todo && cd fastapi-todo
uv init --app
uv add fastapi --extra standard
Define the data model (models.py)
# models.py
from pydantic import BaseModel
class Todo(BaseModel):
id: int
title: str
completed: bool = False
Build the FastAPI application (main.py)
# main.py
from fastapi import FastAPI, HTTPException
from models import Todo
app = FastAPI()
todos: list[Todo] = []
@app.get("/")
def home():
return {"message": "Welcome to the FastAPI Todo App"}
@app.get("/todos")
def list_todos():
return todos
@app.post("/todos", status_code=201)
def add_todo(todo: Todo):
if any(t.id == todo.id for t in todos):
raise HTTPException(status_code=400, detail="Todo with this ID already exists.")
todos.append(todo)
return todo
@app.put("/todos/{todo_id}")
def update(todo_id: int, updated: Todo):
for i, t in enumerate(todos):
if t.id == todo_id:
todos[i] = updated
return updated
raise HTTPException(status_code=404, detail="Todo not found")
@app.delete("/todos/{todo_id}")
def delete(todo_id: int):
global todos
todos = [t for t in todos if t.id != todo_id]
return {"message": "Todo deleted"}
Run the application
uv run fastapi dev
The server starts at http://127.0.0.1:8000.
Explore the interactive API docs at .