Back to templates
FastAPI + SQLAlchemy
A minimal Todo application using FastAPI + SQLAlchemy + Alembic (for migrations)
FastAPI + SQLAlchemy + Alembic (Todo app)
Simple todo API using FastAPI, SQLAlchemy, and Alembic migrations.
Setup
python -m venv .venv
source .venv/bin/activate
pip install -e .
Database (MySQL)
You can configure the connection either with DATABASE_URL or separate env vars:
export DB_NAME=todos
export DB_USERNAME=root
export DB_PASSWORD=""
export DB_HOST=127.0.0.1
export DB_PORT=3306
# or override everything with DATABASE_URL
# export DATABASE_URL="mysql+pymysql://user:pass@localhost:3306/todos"
Create the database (if not already):
mysql -u user -p -e "CREATE DATABASE IF NOT EXISTS todos CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
Run migrations:
alembic upgrade head
Run the API
uvicorn src.main:app --host 0.0.0.0 --port 8000 --reload
Endpoints
GET /– health pingPOST /todos– create a todo{ "title": "Buy milk", "description": "...", "done": false }GET /todos– list todosGET /todos/{id}– fetch a single todoPATCH /todos/{id}– update fields (title, description, done)
Example update:
curl -X PATCH http://localhost:8000/todos/1 \
-H "Content-Type: application/json" \
-d '{"done": true}'
