FastAPI + SQLAlchemy 2.0 async CRUD API with JWT auth, pagination, Alembic migrations, and Docker support
Project description
FastAPI CRUD API
A modern, production-ready CRUD API built with FastAPI, SQLAlchemy 2.0, Alembic, and PostgreSQL (easily swappable with SQLite/MySQL).
Features:
- Full CRUD operations
- Async SQLAlchemy 2.0 + Pydantic v2
- Automatic OpenAPI (Swagger) documentation
- JWT authentication (optional, ready to enable)
- Pagination, filtering, and sorting
- Docker & Docker Compose support
- Alembic migrations
- Pre-commit hooks & Ruff linting
- Testing with pytest + httpx
๐ Quick Start
Prerequisites
- Python 3.11+
- Docker & Docker Compose (optional but recommended)
1. Clone the repository
git clone https://github.com/yosefalemu/fastapi-crud-api.git
cd fastapi-crud-api
2. Create and activate virtual environment
python -m venv venv
source venv/bin/activate # Linux/Mac
# or
venv\Scripts\activate # Windows
3. Install dependencies
pip install -r requirements.txt
4. Copy environment file
cp .env.example .env
Then edit .env with your settings (database URL, secret key, etc.)
5. Run migrations
alembic upgrade head
6. Start the server
uvicorn app.main:app --reload
Server will be available at: http://127.0.0.1:8000
Interactive docs:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
๐ณ Using Docker (Recommended for production-like environment)
docker compose up --build
The API will be available at http://localhost:8000
๐ API Endpoints (Example: Item resource)
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /items/ |
Create item | No |
| GET | /items/ |
List items (paginated) | No |
| GET | /items/{id} |
Get single item | No |
| PUT | /items/{id} |
Update item | Yes (admin) |
| DELETE | /items/{id} |
Delete item | Yes (admin) |
| POST | /auth/token |
Login & get JWT token | No |
Full interactive documentation available at
/docs
๐งช Running Tests
pytest
Or with coverage:
pytest --cov=app
๐ง Environment Variables (.env)
DATABASE_URL=postgresql+asyncpg://user:password@localhost:5432/fastapi_crud
SECRET_KEY=your-super-secret-jwt-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
ENVIRONMENT=development
๐๏ธ Database Migrations (Alembic)
# Create a new migration
alembic revision --autogenerate -m "description"
# Apply migrations
alembic upgrade head
# Downgrade
alembic downgrade -1
๐ช Pre-commit (recommended)
pre-commit install
This project uses:
- Ruff (linter & formatter)
- Black
- mypy
- flake8
๐ Project Structure
.
โโโ app/
โ โโโ api/ # All routers & endpoints
โ โโโ core/ # Config, security, dependencies
โ โโโ crud/ # CRUD operations
โ โโโ models/ # SQLAlchemy models
โ โโโ schemas/ # Pydantic schemas
โ โโโ main.py # FastAPI app instance
โ โโโ database.py # DB session & engine
โโโ migrations/ # Alembic migrations
โโโ tests/ # pytest tests
โโโ .env.example
โโโ requirements.txt
โโโ docker-compose.yml
โโโ README.md
๐ค Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐จโ๐ป Author
Your Name โ @yosefalemu
โญ Show your support
Give a โญ๏ธ if this project helped you!
Made with โค๏ธ using FastAPI
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ada_backend-0.1.0.tar.gz.
File metadata
- Download URL: ada_backend-0.1.0.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Pop!_OS","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcc87eb9d5a96dbaccd3f90886b8eb0d4dc4d66c3e9af15c9e390ea5a29fc0ac
|
|
| MD5 |
f26df12a91859fe8e27226fbdc534b73
|
|
| BLAKE2b-256 |
7d9ec9f3a715a203d52851b2dae064d8c66b2992ae12c565550990aea9e3f488
|
File details
Details for the file ada_backend-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ada_backend-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Pop!_OS","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66e9f087508b9aa20e68d397e34f92cc9df0e5545d5d6d9d2c71dd56bff475d9
|
|
| MD5 |
e069e73e9f528cef47de3de7a9f0aba3
|
|
| BLAKE2b-256 |
2b4b1c376f0b32f6fb8ea568d83bb5203648990385cf28aaaf1eab6e17784d8c
|