Goal: Build a simple API to register and log in users, store them in MySQL, hash passwords securely using Argon2, and return a JWT token upon login. Fully testable via Postman or curl.
Prerequisites
-
Python 3.9+ (tested with Python 3.13)
-
MySQL running locally (root user, no password)
-
Terminal / command line
-
Code editor (VS Code, PyCharm, etc.)
Step 1 — Create Project & Virtual Environment
Step 2 — Install Dependencies
On macOS zsh, quote extras like
passlib[argon2]andpydantic[email].
Verify installations:
All should print OK without errors.
Step 3 — Project Structure
Step 4 — Create Project Files
database.py
models.py
schemas.py
auth.py
crud.py
init_db.py
main.py
Step 5 — Create MySQL Database
Open MySQL shell:
Then, in your project folder:
You should see:
Step 6 — Start FastAPI Server
Swagger UI: http://127.0.0.1:8000/docs
Step 7 — Test Endpoints
Register
-
POST
http://127.0.0.1:8000/register -
Body (JSON):
-
Expected Response:
Login
-
POST
http://127.0.0.1:8000/login -
Body (JSON):
-
Expected Response:
Use Authorization: Bearer <access_token> for future protected endpoints.
Step 8 — Test with curl (Optional)
Step 9 — Troubleshooting
-
500 Internal Server Error: Check terminal for traceback.
-
email-validator missing: Run
pip install "pydantic[email]". -
bcrypt 72-byte error: Avoided by SHA256 + Argon2.
-
MySQL password issue: Update
DATABASE_URLif your root has a password. -
macOS zsh install error: Quote extras like
"passlib[argon2]".
✅ Done! You now have a fully functional FastAPI user registration & login system with MySQL and JWT authentication.

