A learning-focused sample application demonstrating REST API using FastAPI with SQLAlchemy ORM and MySQL database.
This project showcases a complete CRUD application with user management and item tracking features. It's designed as a hands-on example to learn key concepts including:
- FastAPI framework for building modern APIs
- SQLAlchemy 2.0 ORM for database operations
- MySQL 8.4 as the relational database
- Pydantic for data validation and serialization
- Alembic for database migrations
- Docker Compose for containerized development environment
- User registration with secure password hashing (bcrypt)
- CRUD operations for users and items
- One-to-many relationship (User → Items)
- Database connection pooling and health checks
- Automatic API documentation (Swagger UI)
- Environment-based configuration
- Database schema auto-creation on startup
- FastAPI - Modern, fast web framework for building APIs
- SQLAlchemy 2.0 - SQL toolkit and ORM
- Pydantic - Data validation using Python type annotations
- MySQL 8.4 - Relational database
- PyMySQL - Pure Python MySQL driver
- Bcrypt - Password hashing
- Uvicorn - ASGI server
- Docker & Docker Compose - Containerization
.
├── main.py # FastAPI application and route definitions
├── models.py # SQLAlchemy database models
├── schemas.py # Pydantic schemas for request/response validation
├── crud.py # Database operations (Create, Read, Update, Delete)
├── database.py # Database connection and session management
├── config.py # Application settings and configuration
├── docker-compose.yml # Docker services (MySQL, phpMyAdmin)
├── .env # Environment variables
├── Pipfile # Python dependencies
└── alembic/ # Database migration scripts
- Python 3.11+
- Docker and Docker Compose
- Pipenv (or pip)
-
Clone the repository
git clone <repository-url> cd fast-api-sqlalchemy-sample-project
-
Install dependencies
pipenv install
-
Configure environment variables
cp .env.example .env # Edit .env with your configuration -
Start MySQL database
docker-compose up -d mysql
-
Run the application
pipenv run dev
The API will be available at http://localhost:8000
GET /- Welcome messageGET /health- Health check with database connection status
POST /users/- Create a new userGET /users/- List all users (with pagination)GET /users/{user_id}- Get user by ID
POST /users/{user_id}/items/- Create item for a userGET /items/- List all items (with pagination)
FastAPI automatically generates interactive API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
curl -X POST http://localhost:8000/users/ \
-H "Content-Type: application/json" \
-d '{
"email": "user_1@example.com",
"username": "johndoe",
"password": "secure_password_123"
}'curl -X POST http://localhost:8000/users/1/items/ \
-H "Content-Type: application/json" \
-d '{
"title": "My 1st Item",
"description": "This is a test item"
}'curl http://localhost:8000/users/Access phpMyAdmin at http://localhost:8080 for visual database management.
# Create a new migration
pipenv run alembic revision --autogenerate -m "description"
# Apply migrations
pipenv run migratepipenv run dev # Start development server with auto-reload
pipenv run start # Start production server
pipenv run test # Run tests
pipenv run format # Format code with Black
pipenv run lint # Lint code with Flake8
pipenv run type-check # Type check with mypy- Defining tables using declarative base
- Column types and constraints
- Relationships (one-to-many)
- Cascade delete operations
- Request/response validation
- Data serialization
- Type hints and validation rules
- Separating create/read schemas
- Connection pooling
- Session management with dependency injection
- Transaction handling
- Connection health checks
- Bcrypt hashing
- Secure password storage
- Never storing plain text passwords
- RESTful endpoint structure
- HTTP status codes
- Error handling
- Request/response models
The application uses environment variables for configuration:
# Database
DB_USER=
DB_PASSWORD=
DB_HOST=localhost
DB_PORT=3306
DB_NAME=fastapi_db
# Application
APP_NAME='FastAPI MySQL App'
DEBUG=True
# Security
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30- Ensure MySQL container is running:
docker ps - Check database credentials in
.env - Verify port 3306 is not in use
The application handles special characters in database passwords by URL encoding them automatically.
MIT License - feel free to use this project for learning purposes.