Skip to main content

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:

๐Ÿณ 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

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ada_backend-0.1.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ada_backend-0.1.0-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

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

Hashes for ada_backend-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fcc87eb9d5a96dbaccd3f90886b8eb0d4dc4d66c3e9af15c9e390ea5a29fc0ac
MD5 f26df12a91859fe8e27226fbdc534b73
BLAKE2b-256 7d9ec9f3a715a203d52851b2dae064d8c66b2992ae12c565550990aea9e3f488

See more details on using hashes here.

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

Hashes for ada_backend-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66e9f087508b9aa20e68d397e34f92cc9df0e5545d5d6d9d2c71dd56bff475d9
MD5 e069e73e9f528cef47de3de7a9f0aba3
BLAKE2b-256 2b4b1c376f0b32f6fb8ea568d83bb5203648990385cf28aaaf1eab6e17784d8c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page