This template provides a robust starting point for building APIs with FastAPI. It includes user authentication, CRUD operations, JWT token-based authentication, and PostgreSQL integration. The setup is streamlined with Docker and includes comprehensive documentation and testing tools.
- 🔐 User authentication with basic login and Google Auth
- 👥 User management with creation and CRUD operations
- 📄 Example endpoints for Posts, Users, and Votes
- 💓 API healthcheck endpoint
- 🔑 JWT token-based authentication
- ⚙️ Middleware support
- 🌎 CORS configuration
- 📝 Comprehensive Swagger API documentation
- 🐘 PostgreSQL database integration
- 🔒 Field encryption for sensitive data
Important
Min Python version: 3.14
Clone this repo:
git clone https://github.com/ezeparziale/fastapi-api-templateCreate virtual environment:
python -m venv envActivate environment:
- Windows:
. env/scripts/activate- Mac/Linux:
. env/bin/activateUpgrade pip:
python -m pip install --upgrade pipInstall requirements:
pip install -r requirements-dev.txtInstall pre-commit:
pre-commit installCreate .env file. Check the example .env.example
🌐 Google Auth credentials:
Create your app and obtain your client_id and secret:
https://developers.google.com/workspace/guides/create-credentials🔒 How to create a secret key:
openssl rand -base64 64🔐 How to create an encryption key:
To create an encryption key for securing sensitive data, you can use the generate_key.py script provided in the repository. Run the following command:
python generate_key.pyThis will generate a secure encryption key.
🚧 Before first run:
Run docker-compose 🐳 to start the database server
docker compose -f "compose.yaml" up -d --build adminer dband init the database with alembic:
alembic upgrade head🔑 Create a self-signed certificate with openssl:
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365uvicorn app.main:app --reload --port 8000 --ssl-keyfile key.pem --ssl-certfile cert.pemRun linter and formatter
scripts/lint.shscripts/format.shRun coverage
coverage run -m pytestcoverage report --show-missingcoverage htmlOr run all in one with:
scripts/coverage.shRun pytest with coverage
coverage run -m pytestor
scripts/test.shAlembic is used for database migrations. Below are some common commands to manage your database schema.
To autogenerate a new revision based on the changes detected in your models, run:
alembic revision --autogenerate -m "your message here"To create a blank revision for custom migrations, run:
alembic revision -m "your message here"To apply the latest migrations and upgrade the database schema, run:
alembic upgrade headTo revert the last migration and downgrade the database schema, run:
alembic downgrade -1After creating a revision, you can edit the generated script to define your custom migrations.