This is a reusable, production-minded FastAPI starter template designed for building new API services with minimal setup.
- Automatic Docs: FastAPI automatically provides OpenAPI docs and interactive documentation.
- Multi-File Architecture: Multi-file organization scales better for real production projects.
- Docker Ready: Docker makes the template reproducible, consistent, and easy to deploy.
- Scalable app structure
- Versioned API routing (
/api/v1) - Health checks and readiness endpoints
- Example CRUD routes with dependency injection
- Example API key security for protected endpoints
- Consistent, centralized error handling
- Pre-configured logging
- Docker and Docker Compose setup for local development
- Full test suite via
pytest - Code quality tools (
ruff,black,mypy) - Render deployment configuration
- Interactive docs available at
/docsand/redoc
app/: The core application logic.core/: Configurations, logging, and security definitions.api/v1/: Version 1 API routers and endpoints.schemas/: Pydantic models for input validation and output formatting.services/: Business logic, keeping handlers thin.utils/: Shared utilities.
tests/: Pytest suite covering endpoints and business logic.scripts/: Development and CI bash scripts.Dockerfile/docker-compose.yml: Containerization configuration.
Linux / macOS:
python -m venv .venv
source .venv/bin/activateWindows:
python -m venv .venv
.\.venv\Scripts\Activate.ps1pip install -r requirements.txt -r requirements-dev.txtcp .env.example .envuvicorn app.main:app --reload# Run tests
pytest
# Lint the codebase
ruff check .
# Format code
black .
# Type checking
mypy appConvenient bash scripts are located in scripts/:
# Run local dev server with auto-reload
bash scripts/dev.sh
# Run full test suite
bash scripts/test.sh
# Lint with Ruff
bash scripts/lint.sh
# Format with Black and Ruff
bash scripts/format.sh
# Run MyPy
bash scripts/typecheck.sh
# Build and run Docker locally
bash scripts/run_docker.shYou can use Make for streamlined execution if you have it installed:
make dev: Run dev server.make test: Run pytest.make lint: Run ruff.make format: Run black & ruff fix.make typecheck: Run mypy.make docker-build: Build image.make docker-run: Run container.
FastAPI dynamically generates the OpenAPI schema:
- Swagger UI: Interactive UI at
/docs - ReDoc: Static, highly readable docs at
/redoc - OpenAPI Schema: Raw JSON schema at
/api/v1/openapi.json
Root and Health Info
curl http://127.0.0.1:8000/
curl http://127.0.0.1:8000/health
curl http://127.0.0.1:8000/readySample Ping
curl http://127.0.0.1:8000/api/v1/examples/pingProtected Endpoint
curl -H "X-API-Key: change_this_secret_key_in_production" \
http://127.0.0.1:8000/api/v1/examples/protectedItem CRUD Flow
# Create Item
curl -X POST http://127.0.0.1:8000/api/v1/items/ \
-H "Content-Type: application/json" \
-d '{"name": "Apple", "description": "Fresh fruit"}'
# List Items
curl http://127.0.0.1:8000/api/v1/items/
# Update Item
curl -X PUT http://127.0.0.1:8000/api/v1/items/1 \
-H "Content-Type: application/json" \
-d '{"name": "Green Apple"}'
# Delete Item
curl -X DELETE http://127.0.0.1:8000/api/v1/items/1To easily bootstrap a new project based on this template, you can run:
bash scripts/new_service_checklist.shStep-by-Step:
- Copy the
template/folder. - Rename project-specific values in
.env.example,pyproject.toml, andrender.yaml. - Update the app name and version in
app/core/config.py. - Delete exampleroutes (
examples.py,items.py) and replace them with your business-specific routes. - Add new internal services to
app/services/and schemas toapp/schemas/. - Update your test suite in
tests/. - Adjust Dockerfile details if you have unique operating system dependencies.
- Commit to Git.
Containerized deployment is standard for FastAPI applications. This template includes a slim and production-minded Dockerfile using the official python:3.11-slim image alongside layered caching best practices.
Using standard Docker:
docker build -t fastapi-template .
docker run -p 8000:8000 --env-file .env fastapi-templateUsing Docker Compose:
docker compose up --buildYou can then test the running container on your host machine:
curl http://127.0.0.1:8000/health
curl http://127.0.0.1:8000/docs
curl http://127.0.0.1:8000/redocThis template includes render.yaml for infrastructure-as-code deployment on Render using their native Python environments. Set your APP_ENV and APP_DEBUG variables on your Render dashboard securely.
Expected startup command used in Render configuration:
uvicorn app.main:app --host 0.0.0.0 --port $PORT
Strong test coverage and static analysis ensure a service doesn't break in production. This reusable starter provides you with the right tools preconfigured:
- Tests: Use
pytestfor unit and integration testing. - Linting: Use
ruffto identify structural rule violations. - Formatting: Use
blackto keep all developers writing visually unified code. - Typing: Use
mypyto detect implicit runtime type errors before you ever deploy it.
You can confidently scale this setup by extending it with:
- Databases: Add SQLAlchemy models in an
app/models/folder and implement database sessions inapp/api/deps.py. - Authentication: Swap the X-API-Key approach for OAuth2 with JWT tokens utilizing FastAPI's native security features.
- Background Tasks: Pass FastAPI's
BackgroundTasksvia dependency injection into handlers, or implement Celery for robust async messaging. - File Uploads: Include
UploadFileschemas and map mounted volumes viadocker-compose.ymlinto your runtime container.
When building new services, aim to adhere to the following principles:
- Keep route handlers thin. Call services.
- Keep services decoupled and reusable.
- Validate inputs rigorously with Pydantic schemas. Corrupt data must fail early.
- Document endpoint parameters explicitly for Swagger UI.
- Never commit secrets to Git. Always inject configuration via environment variables.
- Write tests for every core flow.
- Ensure health routes remain fast and do not rely on slow external resources.
Built with ❤️ with FastAPI.