This repo contains are starter template for FastAPI applications. The project structure may feel a bit opionionated where it matters, but it's flexible enough and follows the repository pattern.
I found myself spending countless hours to setup my FastAPI projects over and over again whenever i needed to run a production-grade application. Authentication, Databases, and other features became a pain to repeatedly setup so I decided to create this template to take away the effort and time wasted.
- 🔄 Modular CRUD System – Reusable base for all entities
- 🔑 OAuth Authentication – Google login included, extendable to other providers
- 📄 Auto-generated OpenAPI Docs –
/docsand/redoc - 📦 Docker Support – Preconfigured
Dockerfile&docker-compose - 🗄️ SQLModel ORM – A hybrid of SQLAlchemy and Pydantic, all your models and schemas as one entity
- 🛠️ Background Tasks – Async task execution support with celery workers
- ⏲️ Monitoring and Observability -
PrometheusandGrafana - 🔒 JWT-Based Authentication – Secure access control
- ✅ Pre-configured Linting & Formatting – Uses
ruff
fastapi-starter/
│── data/ # Data storage
│── docker-compose.yaml # Docker setup
│── docker-compose.override.yaml
│── Dockerfile # Containerization
│── Makefile # Task automation
│── pyproject.toml # Dependency management
│── README.md # Documentation
│── run.sh # Script to start the application
│── uv.lock # Dependency lock file
│── nginx/ # Nginx configuration with SSL certificates
│── prometheus/ # Prometheus configuration
│── src/ # Application source code
│ ├── api/ # API route definitions
│ ├── core/ # Core configurations & settings
│ ├── models/ # Database models using SQLModel
│ ├── repositories/ # Database interaction logic
│ ├── services/ # Business logic
│ ├── utils/ # Utility functions
│ ├── main.py # Application entry point
│ ├── __init__.py
│ ├── __pycache__/
git clone https://github.com/yourusername/fastapi-starter.git
cd fastapi-starteruv venv .venv
source .venv/bin/activate # On macOS/Linux
.venv\Scripts\activate # On Windowsuv syncpre-commit installWhy? This ensures comprehensive checks before commits to maintain code quality.
make certs./run.sh- 📜 OpenAPI: http://localhost:8000/docs
- 📜 Redoc: http://localhost:8000/redoc
If you cloned this repo but want to use it as your own without linking to ours , run:
rm -rf .git
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/YOUR_USERNAME/YOUR_REPO.git
git push -u origin mainThe items endpoints serve as a base template for all CRUD-like entities . To add a new resource, simply:
- Copy the
itemsendpoints - Modify the entity models
- Adjust the repository and service logic
- Register the endpoint in the base router
This ensures consistency across all API resources while reducing development time.
- POST
/auth/signup– Register a new user - POST
/auth/login– Log in to get a JWT token - POST
/auth/login/access-token– Get OAuth access token - GET
/auth/{provider}/init– Initiate OAuth login with Google or another provider. Visit OAuth provider developer page to obtain details. - GET
/auth/{provider}/callback– Handle OAuth callback. Set callback url tohttps://
docker-compose up --buildruff check
# fix linting issues
ruff check --fix
ruff format-
Generate ssl certificates with a trusted certificates authority or use a self-signed certificate by running the oppenssl command below
openssl req -x509 -noenc -days 365 -newkey rsa:2048 -keyout nginx/certs/server.key -out nginx/certs/server.crt
The certificates will be generated and placed in the
nginxdirectory. However, place these in the/etc/ssldirectory during deployment. -
Uncomment the line that rewrites the http request to port 8443 since it wont be needed in production.
-
Copy the nginx configuration to
/etc/nginx/sites-availableto run the nginx server.NB: this stressful configuration won't really be needed after integrating an IaaC tool, lol😅
Get to know more about ssl certificates here.
- Access the Grafana UI on
http://localhost:3001and create a new dashboard - Enter
host.docker.internal:9090to access Prometheus on the host server sincelocalhostwill point to the grafana containerTIP: a custom grafana dashboard will be made available to import into your project
Get to know more about setting up Grafana dashboards here.