Production-ready RESTful API built with FastAPI, SQLAlchemy, and MySQL. It provides JWT-protected CRUD operations for product inventory, containerized deployment with Docker, and first-class developer ergonomics (typed schemas, env-based config, automatic docs).
- FastAPI app with automatic Swagger UI at
/docs - JWT authentication with secure password hashing (Passlib + bcrypt)
- SQLAlchemy ORM models backed by MySQL
- Modular routing (users, products) and centralized database session handling
- Ready-to-run Dockerfile and docker-compose stack (API + MySQL)
- Environment-based configuration via
.env
- Python 3.11
- FastAPI, Uvicorn, SQLAlchemy 2.x, Pydantic v2
- MySQL 8
- Docker & docker-compose
python -m venv venv
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activatepip install -r requirements.txtCopy .env (already included) or override via shell variables. Minimum required values:
DATABASE_URL=mysql+pymysql://<user>:<password>@<host>:3306/<db>
SECRET_KEY=<random string>
ACCESS_TOKEN_EXPIRE_MINUTES=30
uvicorn app.main:app --reloadAccess docs at http://localhost:8000/docs. Tables are auto-created on startup via SQLAlchemy Base.metadata.create_all, but ensure the configured database exists first.
docker-compose up --buildThis brings up inventory_api (FastAPI) and inventory_db (MySQL 8) with persistent storage in the db_data volume.
POST /users/register– create an accountPOST /users/login– obtain JWT (OAuth2 password flow)GET /users/me– fetch current user profilePOST /products/– create product (auth required)GET /products/– list authenticated user's productsGET /products/{id}– fetch single productPUT /products/{id}– update productDELETE /products/{id}– delete product
All product routes require an Authorization: Bearer <token> header.
- Register a user via
/users/register. - Authenticate via
/users/loginusingusername&password(form-data) to receive a token. - Authorize subsequent product requests with the returned token.
app/
+-- auth.py # JWT helpers + dependencies
+-- database.py # SQLAlchemy engine/session
+-- main.py # FastAPI entrypoint
+-- models.py # ORM models
+-- schemas.py # Pydantic models
+-- routes/
+-- users.py # register/login/me endpoints
+-- products.py # JWT-protected CRUD operations
- Add middleware for structured logging & request tracing.
- Introduce domain-driven modules (clean architecture) if business logic grows.
- Write pytest suites (unit + integration) using
TestClientand a transient database. - Wire CI (GitHub Actions) for lint/test before deployment.
MIT (adjust as needed).