A production-ready FastAPI template with modern Python development practices, database integration, background tasks, and comprehensive tooling.
- FastAPI Framework: Modern, fast web framework for building APIs with Python 3.12+
- Database Integration: PostgreSQL with SQLModel ORM and Alembic migrations
- Background Tasks: Celery with Redis broker for asynchronous task processing
- API Documentation: Automatic OpenAPI documentation with Scalar UI
- Configuration Management: Pydantic Settings with environment variable support
- Code Quality: Ruff for linting and formatting
- Development Tools: Hot reload, CORS middleware, static file serving
- LLM Integration: Built-in support for OpenAI and Mistral APIs
- Framework: FastAPI 0.116.1+
- Database: PostgreSQL with SQLModel
- Task Queue: Celery with Redis
- Migration: Alembic
- Validation: Pydantic
- Code Quality: Ruff
- Server: Uvicorn (development) / Gunicorn (production)
- Documentation: Scalar FastAPI
fastapi-template/
├── app/
│ ├── core/
│ │ ├── extended_settings/
│ │ │ ├── app_settings.py # Application configuration
│ │ │ ├── database_settings.py # Database and Redis settings
│ │ │ └── llm_settings.py # LLM API configurations
│ │ ├── models.py # Base SQLModel classes
│ │ └── settings.py # Main settings aggregator
│ ├── database/
│ │ ├── engine.py # Database engine setup
│ │ └── models.py # Database models
│ ├── router/
│ │ └── example_router.py # API route definitions
│ ├── services/ # Business logic services
│ ├── tasks/
│ │ └── example_tasks.py # Celery background tasks
│ ├── utils/
│ │ └── generate_ids.py # Utility functions
│ ├── celery.py # Celery configuration
│ └── main.py # FastAPI application entry point
├── .files/ # Deployment configuration files
│ ├── nginx-domain.com # Nginx reverse proxy configuration
│ ├── service-api.service # Systemd service for FastAPI app
│ └── service-worker.service # Systemd service for Celery worker
├── alembic/ # Database migration files
├── bin/ # Setup and deployment scripts
│ ├── setup.sh # Initial project setup script
│ └── update.sh # Production update script
├── public/ # Static files directory
├── pyproject.toml # Project dependencies and configuration
├── alembic.ini # Alembic configuration
└── Makefile # Development commands
- Python 3.12+
- PostgreSQL
- Redis
- UV package manager (recommended)
- Clone the repository:
git clone <repository-url>
cd fastapi-template- Install dependencies:
uv sync- Create environment file:
cp .env.example .env- Configure your environment variables in
.env:
# Application Settings
APP_NAME=FastAPI Template
VERSION=0.1.0
DESCRIPTION=FastAPI Template API
DEBUG=true
# Database Settings
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=postgres
# Redis Settings
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0
# LLM Settings
OPENAI_API_KEY=your_openai_api_key
OPENAI_BASE_URL=https://api.openai.com/v1/
# Logger Settings (optional - defaults will be used if not specified)
LOGGER_LEVEL=INFO
LOGGER_FILE_ENABLED=true
LOGGER_FILE_PATH=logs/app.log
LOGGER_CONSOLE_ENABLED=true- Run database migrations:
uv run alembic upgrade headStart the development server:
make dev
# or
uv run uvicorn app.main:app --reloadStart the Celery worker:
make worker
# or
uv run celery -A app.celery worker --pool=threads -c 2Format and lint code:
make format
# or
uv run ruff format .
uv run ruff check . --fixThe application uses a modular configuration system with four main settings classes:
APP_NAME: Application nameVERSION: Application versionDESCRIPTION: Application descriptionDEBUG: Debug mode toggleALLOW_ORIGINS: CORS allowed originsALLOW_METHODS: CORS allowed methodsALLOW_HEADERS: CORS allowed headers
DB_HOST,DB_PORT,DB_USER,DB_PASSWORD,DB_NAME: PostgreSQL connectionREDIS_HOST,REDIS_PORT,REDIS_DB: Redis connection- Auto-generated
DATABASE_URLandREDIS_URLproperties
OPENAI_API_KEY: OpenAI API keyOPENAI_BASE_URL: OpenAI API base URL (default: https://api.openai.com/v1/)
LOGGER_LEVEL: Log level (default: INFO)LOGGER_FILE_ENABLED: Enable file logging (default: true)LOGGER_FILE_PATH: Log file path (default: logs/app.log)LOGGER_FILE_ROTATION: Log file rotation (default: 10 MB)LOGGER_FILE_RETENTION: Log file retention (default: 30 days)LOGGER_CONSOLE_ENABLED: Enable console logging (default: true)LOGGER_CONSOLE_COLORIZE: Colorize console output (default: true)
Once the server is running, you can access:
- Scalar Documentation:
http://localhost:8000/scalar - OpenAPI JSON:
http://localhost:8000/openapi.json - Example Endpoint:
http://localhost:8000/example/
The template includes a base model with common fields:
class BaseModel(SQLModel):
id: str = Field(default_factory=generate_id, primary_key=True)
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)
is_deleted: bool = Field(default=False)Celery is configured for background task processing. Tasks are auto-discovered from the app.tasks module.
- Follow the existing project structure
- Use type hints throughout the codebase
- Run
make formatbefore committing - Add tests for new features
- Update documentation as needed
The template includes production-ready deployment configurations:
- Run the setup script:
./bin/setup.sh- Configure environment variables for production:
cp .env.example .env
# Edit .env with production values- Set up systemd services:
# Copy service files to systemd directory
sudo cp .files/service-api.service /etc/systemd/system/
sudo cp .files/service-worker.service /etc/systemd/system/
# Edit service files to match your paths and user
sudo systemctl daemon-reload
sudo systemctl enable service-api.service
sudo systemctl enable service-worker.service
sudo systemctl start service-api.service
sudo systemctl start service-worker.service- Configure Nginx (optional):
# Copy and modify nginx configuration
sudo cp .files/nginx-domain.com /etc/nginx/sites-available/your-domain
# Edit the file to match your domain
sudo ln -s /etc/nginx/sites-available/your-domain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxUse the update script for seamless deployments:
./bin/update.sh.files/nginx-domain.com: Nginx reverse proxy configuration.files/service-api.service: Systemd service for the FastAPI application.files/service-worker.service: Systemd service for Celery background workersbin/setup.sh: Automated setup script that installs uv, dependencies, and runs migrationsbin/update.sh: Production update script that pulls changes, syncs dependencies, and restarts services
- Set
DEBUG=falsein environment variables - Configure proper PostgreSQL and Redis instances
- Set up proper logging and monitoring
- Configure SSL certificates for HTTPS
- Set appropriate user permissions for service files
- Monitor application logs via systemd journals
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
For questions and support, please open an issue in the repository.