Skip to content

Sohail342/fastapi-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPI Template Logo

FastAPI Template

A powerful CLI tool for generating production-ready FastAPI projects with best practices, integrated authentication, and flexible ORM options.

Features

  • πŸš€ Production Ready: Pre-configured with security, logging, and deployment best practices
  • πŸ” Integrated Authentication: FastAPI-Users integration with JWT authentication
  • πŸ—„οΈ Flexible ORM: Choose between SQLAlchemy (PostgreSQL) or Beanie (MongoDB)
  • 🐳 Docker Support: Complete Docker setup with docker-compose
  • πŸ“¦ Celery Integration: Background task processing
  • πŸ§ͺ Testing Ready: Pre-configured testing setup
  • πŸ“Š API Documentation: Auto-generated OpenAPI/Swagger documentation
  • 🎯 CLI Driven: Simple command-line interface for project generation

Quick Start

Installation

pip install fastapi-template-cli

Create a New Project

# Create an API-only project with SQLAlchemy
fastapi-template-cli new my-api --orm sqlalchemy --type api

# Create a modular project with MongoDB
fastapi-template-cli new my-app --orm beanie --type modular

# Create with project description
fastapi-template-cli new my-project --orm sqlalchemy --type modular \
  --description "My awesome FastAPI project" --author "Your Name"

Project Types

API-Only Projects

  • Lightweight FastAPI backend
  • Database integration (SQLAlchemy or Beanie)
  • FastAPI-Users authentication
  • No frontend or background tasks

Modular Projects

  • Complete backend with FastAPI
  • Database integration
  • FastAPI-Users authentication
  • Celery for background tasks
  • Redis for caching and task queue
  • Docker setup with docker-compose

ORM Options

SQLAlchemy (PostgreSQL)

  • Database: PostgreSQL
  • ORM: SQLAlchemy 2.0 with async support
  • Migrations: Alembic
  • Connection: asyncpg driver

Beanie (MongoDB)

  • Database: MongoDB
  • ODM: Beanie (async MongoDB ODM)
  • Driver: Motor
  • Schema: Pydantic-based documents

Usage

Basic Commands

# List available templates
fastapi-template-cli list-templates

# Create a new project
fastapi-template-cli new myproject

# Show version
fastapi-template-cli version

Project Structure

Generated projects follow a clean architecture:

myproject/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── v1/
β”‚   β”‚       β”œβ”€β”€ api.py
β”‚   β”‚       └── endpoints/
β”‚   β”‚           └── users.py
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py
β”‚   β”‚   └── security.py
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   β”œβ”€β”€ session.py (SQLAlchemy) or mongo.py (Beanie)
β”‚   β”‚   └── base_class.py (SQLAlchemy)
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── user.py
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   └── user.py
β”‚   β”œβ”€β”€ users.py (FastAPI-Users config)
β”‚   └── main.py
β”œβ”€β”€ docker/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── docker-compose.yml (modular only)
β”œβ”€β”€ alembic/ (SQLAlchemy only)
β”œβ”€β”€ workers/ (modular only)
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ .env
└── .gitignore

Template Comparison

Modular Template

  • Includes Redis as Celery broker and result backend
  • Separate Celery worker for long-running tasks
  • Celery Beat for scheduled tasks
  • Complete frontend integration
  • Production-ready Docker setup

API Template

  • Lightweight FastAPI core
  • Database-only backend
  • Minimal dependencies
  • Optimized for microservices
  • Simplified Docker setup

Project Structure Details

API Template

e-commerce/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/v1/
β”‚   β”‚   β”œβ”€β”€ api.py              # Main API router
β”‚   β”‚   └── endpoints/
β”‚   β”‚       └── users.py        # User endpoints
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py           # Application configuration
β”‚   β”‚   β”œβ”€β”€ security.py         # Security utilities
β”‚   β”‚   └── users.py           # User management
β”‚   β”œβ”€β”€ database/
β”‚   β”‚   β”œβ”€β”€ base.py            # Database base setup
β”‚   β”‚   β”œβ”€β”€ base_class.py      # Base model class
β”‚   β”‚   └── session.py         # Database session
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── users.py           # User models
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   └── user.py            # Pydantic schemas
β”‚   β”œβ”€β”€ users/
β”‚   β”‚   β”œβ”€β”€ dependencies.py    # User dependencies
β”‚   β”‚   └── manager.py         # User manager
β”‚   └── main.py                # FastAPI application
β”œβ”€β”€ alembic/                   # Database migrations
β”œβ”€β”€ docker/                    # Docker configuration
β”œβ”€β”€ tests/                     # Test files
β”œβ”€β”€ .env.dev                   # Development environment
β”œβ”€β”€ .env.prod                  # Production environment
└── pyproject.toml            # Project dependencies

Modular Template

full-erp/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py           # Application configuration
β”‚   β”‚   β”œβ”€β”€ security.py         # Security utilities
β”‚   β”‚   └── users.py           # User management
β”‚   β”œβ”€β”€ database/
β”‚   β”‚   β”œβ”€β”€ base.py            # Database base setup
β”‚   β”‚   β”œβ”€β”€ base_class.py      # Base model class
β”‚   β”‚   └── session.py         # Database session
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── users.py           # User models
β”‚   β”œβ”€β”€ routers/
β”‚   β”‚   └── test.py            # Test endpoints
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   └── user.py            # Pydantic schemas
β”‚   β”œβ”€β”€ services/              # Business logic services
β”‚   β”œβ”€β”€ users/
β”‚   β”‚   β”œβ”€β”€ dependencies.py    # User dependencies
β”‚   β”‚   └── manager.py         # User manager
β”‚   β”œβ”€β”€ utils/                 # Utility functions
β”‚   β”œβ”€β”€ workers/
β”‚   β”‚   β”œβ”€β”€ celery_app.py      # Celery configuration
β”‚   β”‚   └── tasks.py           # Background tasks
β”‚   └── main.py                # FastAPI application
β”œβ”€β”€ alembic/                   # Database migrations
β”œβ”€β”€ docker/                    # Docker configuration
β”œβ”€β”€ tests/                     # Test files
β”œβ”€β”€ .env.dev                   # Development environment
β”œβ”€β”€ .env.prod                  # Production environment
β”œβ”€β”€ docker-compose.dev.yml     # Development Docker setup
β”œβ”€β”€ docker-compose.prod.yml    # Production Docker setup
└── pyproject.toml            # Project dependencies

Development

SQLAlchemy Projects

  1. Setup Database

    cd myproject
    pip install -e .
    alembic upgrade head
  2. Run Development Server

    uvicorn app.main:app --reload
  3. Create Database Migration

    alembic revision --autogenerate -m "Add new table"

Beanie Projects

  1. Setup MongoDB

    cd myproject
    pip install -e .
    # MongoDB will auto-initialize on first connection
  2. Run Development Server

    uvicorn app.main:app --reload

Modular Projects (Docker)

  1. Start All Services

    cd myproject
    docker-compose up -d
  2. Access Services

Configuration

Environment Variables

Create a .env file in your project root:

# Database
DATABASE_URL=postgresql+asyncpg://user:pass@localhost/dbname  # SQLAlchemy
MONGODB_URL=mongodb://localhost:27017/myproject  # Beanie

# Security
SECRET_KEY=your-secret-key-here
ACCESS_TOKEN_EXPIRE_MINUTES=30

# Redis (modular)
REDIS_URL=redis://localhost:6379/0

# Email (optional)
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASSWORD=your-password

Database Configuration

SQLAlchemy (PostgreSQL)

# Install PostgreSQL
# Create database
createdb myproject

# Set DATABASE_URL
export DATABASE_URL=postgresql+asyncpg://user:password@localhost/myproject

Beanie (MongoDB)

# Install MongoDB
# MongoDB will create database on first connection
export MONGODB_URL=mongodb://localhost:27017/myproject

Deployment

Docker Deployment

For modular projects:

# Build and run
docker-compose -f docker-compose.dev.yaml up -d --build

# Stop services
docker-compose -f docker-compose.dev.yaml down

Production Deployment

  1. Environment Variables

    export SECRET_KEY=your-production-secret
    export DATABASE_URL=your-production-db-url
  2. Gunicorn/Uvicorn

    gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
  3. Traefik Reverse Proxy Includes Traefik configuration in docker-compose.prod.yml:

    services:
      traefik:
        image: traefik:v3.0
        command:
          - --api.dashboard=true
          - --providers.docker=true
          - --entrypoints.web.address=:80
          - --entrypoints.websecure.address=:443
          - --certificatesresolvers.letsencrypt.acme.tlschallenge=true
          - --certificatesresolvers.letsencrypt.acme.email=your-email@domain.com
          - --certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json

Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make your changes
  4. Add tests for new functionality
  5. Run tests: pytest
  6. Commit your changes: git commit -am 'Add feature'
  7. Push to the branch: git push origin feature-name
  8. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

About

A powerful CLI tool for generating production-ready FastAPI applications with multiple template options.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors