A production-ready starter demonstrating:
- FastAPI with Pydantic v2
- JWT auth (password flow) with
python-jose - Password hashing with
passlib[bcrypt] - SQLAlchemy 2.0 + SQLite
- Dependency injection and routers
- Tests with
pytest+TestClient - CORS & settings via
.env
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txtCopy .env.example to .env and tweak values if you like.
uvicorn app.main:app --reload- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
- Register:
curl -X POST http://127.0.0.1:8000/auth/register -H "Content-Type: application/json" -d '{"username":"alice","password":"secret"}'- Get token:
curl -X POST http://127.0.0.1:8000/auth/token -H "Content-Type: application/x-www-form-urlencoded" -d "username=alice&password=secret"Copy the access_token.
- Create a todo:
curl -X POST http://127.0.0.1:8000/todos -H "Authorization: Bearer <TOKEN>" -H "Content-Type: application/json" -d '{"title":"Buy milk","completed":false}'- List your todos:
curl -H "Authorization: Bearer <TOKEN>" http://127.0.0.1:8000/todospytest -qfastapi-todo-jwt/
├─ app/
│ ├─ __init__.py
│ ├─ main.py
│ ├─ database.py
│ ├─ models.py
│ ├─ schemas.py
│ ├─ auth.py
│ ├─ deps.py
│ └─ routers/
│ ├─ auth.py
│ └─ todos.py
├─ tests/
│ └─ test_main.py
├─ .env.example
├─ requirements.txt
└─ README.md
- Uses SQLite by default; switch to Postgres by changing
DATABASE_URLin.env. - Passwords are hashed (bcrypt). Never store plaintext passwords.
- JWT is short-lived by default (30 minutes). Tune in
.env.