Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

129 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPI Production API

CI Python FastAPI License

A production-ready FastAPI backend template designed for building secure, scalable, and maintainable APIs.

Built with:

  • FastAPI
  • PostgreSQL
  • SQLAlchemy ORM
  • Alembic migrations
  • JWT Authentication
  • Automated testing
  • Docker
  • GitHub Actions CI/CD

Maintained by HoungDev.

This project provides a production-focused backend foundation with clean architecture, security best practices, and developer-friendly workflows.


πŸ’— Support Open Source Development

FastAPI Production API is an open-source project maintained by HoungDev.

Maintaining a production-ready backend template requires continuous work including:

  • πŸ”’ Security updates and dependency maintenance
  • πŸ§ͺ Improving automated tests and reliability
  • πŸ“š Maintaining documentation and examples
  • πŸš€ Adding new features and developer improvements

If this project helps you build better FastAPI applications, you can support the project by:

  • ⭐ Starring the repository
  • πŸ› Reporting issues
  • πŸ’‘ Suggesting improvements
  • 🀝 Contributing code
  • πŸ’— Becoming a GitHub Sponsor

Every contribution helps keep this project actively maintained and improved.

Thank you for supporting open-source software.

πŸ› οΈ Tech Stack

Backend

  • ⚑ FastAPI - Modern Python web framework for building APIs
  • 🐘 PostgreSQL - Reliable relational database
  • πŸ”— SQLAlchemy ORM - Database abstraction layer
  • πŸ—ƒοΈ Alembic - Database migration management
  • πŸ” JWT Authentication - Secure authentication system

Development & Quality

  • 🐍 Python 3.12
  • πŸ§ͺ Pytest - Automated testing
  • 🐳 Docker - Containerized development and deployment
  • βš™οΈ GitHub Actions - CI/CD automation

Production

  • πŸš€ Production-ready configuration
  • πŸ”’ Security-focused architecture
  • πŸ“¦ Environment-based configuration
  • πŸ“– Developer-friendly documentation

Features

Backend

  • FastAPI framework
  • PostgreSQL database
  • SQLAlchemy ORM
  • Alembic database migrations
  • Pydantic Settings configuration
  • Gunicorn + Uvicorn production server

Authentication & Security

  • JWT Authentication
  • OAuth2 Password Bearer authentication
  • Access Token authentication
  • Refresh Token authentication
  • Refresh Token hashing
  • Refresh Token rotation
  • Refresh Token revocation
  • bcrypt password hashing
  • JWT issuer validation
  • JWT audience validation
  • Role-based authorization

Middleware

  • CORS configuration
  • Security headers middleware
  • Rate limiting middleware
  • Request logging middleware

Reliability

  • Database health check
  • Global exception handling
  • Database transaction rollback safety
  • Environment-based configuration

Testing & CI/CD

  • Pytest automated testing
  • Authentication tests
  • JWT security tests
  • Refresh token rotation tests
  • Rate limit tests
  • GitHub Actions CI pipeline
  • Dependency security audit

Project Structure

fastapi-production-api/
β”‚
β”œβ”€β”€ .github/                    # GitHub workflows and repository configuration
β”‚
β”œβ”€β”€ src/                        # Application source code
β”‚
β”œβ”€β”€ tests/                      # Automated test suite
β”‚
β”œβ”€β”€ alembic/                    # Database migration scripts
β”‚
β”œβ”€β”€ docker-compose.yml          # Container configuration
β”œβ”€β”€ gunicorn.conf.py            # Production server configuration
β”œβ”€β”€ alembic.ini                 # Alembic configuration
β”‚
β”œβ”€β”€ pyproject.toml              # Project dependencies and metadata
β”œβ”€β”€ uv.lock                     # Locked dependency versions
β”‚
β”œβ”€β”€ .env.example                # Environment template
β”‚
β”œβ”€β”€ README.md
β”œβ”€β”€ CHANGELOG.md
β”œβ”€β”€ ROADMAP.md
β”œβ”€β”€ DEPLOYMENT.md
β”œβ”€β”€ SECURITY.md
β”œβ”€β”€ CONTRIBUTING.md
β”œβ”€β”€ CODE_OF_CONDUCT.md
└── LICENSE

# Requirements

- Python 3.13+
- PostgreSQL
- uv package manager

---

# Installation

Clone repository:

```bash
git clone https://github.com/HoungDev/fastapi-production-api.git

cd fastapi-production-api

Install dependencies:

uv sync

Environment Configuration

Create environment file:

cp .env.example .env

Example:

APP_NAME=FastAPI Production API

ENVIRONMENT=development

DEBUG=false

LOG_LEVEL=INFO

DATABASE_URL=postgresql+psycopg://user:password@localhost:5432/database

SECRET_KEY=your-secret-key

ALGORITHM=HS256

ACCESS_TOKEN_EXPIRE_MINUTES=30

REFRESH_TOKEN_EXPIRE_DAYS=7

Database Migration

Run migrations:

uv run alembic upgrade head

Create migration:

uv run alembic revision --autogenerate -m "migration message"

Development Server

Run:

uv run uvicorn src.app.main:app --reload

Server:

http://localhost:8000

Swagger:

http://localhost:8000/docs

ReDoc:

http://localhost:8000/redoc

Production Server

Production deployment uses Gunicorn with Uvicorn workers.

Run:

uv run gunicorn \
-c gunicorn.conf.py \
src.app.main:app

Architecture:

Nginx
 |
Gunicorn
 |
FastAPI
 |
PostgreSQL

Testing

Run:

uv run pytest

Current test status:

34 passed

Coverage includes:

Authentication
β”œβ”€β”€ Register
β”œβ”€β”€ Login
β”œβ”€β”€ JWT validation
β”œβ”€β”€ Protected routes
└── Current user


Token Security
β”œβ”€β”€ Access token
β”œβ”€β”€ Refresh token
β”œβ”€β”€ Token expiration
β”œβ”€β”€ Token issuer
β”œβ”€β”€ Token audience
└── Refresh rotation


System
β”œβ”€β”€ Health check
β”œβ”€β”€ Rate limiting
└── Exception handling

CI/CD Pipeline

GitHub Actions runs automatically on:

  • Push to main branch
  • Pull requests to main branch

Pipeline:

Checkout repository
        |
Setup Python
        |
Install uv
        |
Install dependencies
        |
Run migrations
        |
Run tests
        |
Security audit

API Endpoints

Health

GET /health

Database health:

GET /health/db

Authentication

Register:

POST /register/

Login:

POST /login/

Refresh token:

POST /auth/refresh

Logout:

POST /auth/logout

User

Current user:

GET /auth/me

Security Implementation

Implemented security features:

  • Password hashing with bcrypt
  • JWT authentication
  • JWT issuer validation
  • JWT audience validation
  • Refresh token hashing
  • Refresh token rotation
  • Refresh token revocation
  • Rate limiting
  • Security headers
  • Exception isolation
  • Database transaction rollback

Open Source

This project is built with the goal of helping developers learn and build secure FastAPI backend systems.

Contributions are welcome.

Please read:


Maintainer

Maintained by:

HoungDev

Open Source Maintainer focused on:

  • Python backend development
  • FastAPI architecture
  • API security
  • Production engineering

Production Status

Current release:

v1.0.0

Completed:

βœ… Authentication system

βœ… JWT security

βœ… Refresh token rotation

βœ… Database migrations

βœ… Automated testing

βœ… CI/CD pipeline

βœ… Security middleware

βœ… Logging system


Roadmap

Future improvements:

  • Redis integration
  • Background task processing
  • Monitoring system
  • Metrics collection
  • Cloud deployment examples
  • Improved developer experience

See:

ROADMAP.md

Community

Contributions and discussions are welcome.

Please check:

  • Issues
  • Pull Requests
  • Feature Requests
  • Discussions

License

MIT License

About

Production-ready FastAPI backend template with PostgreSQL, authentication, testing, Docker, security best practices, and scalable architecture.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Contributors

Languages