A lightweight, ready-to-use template for building APIs with FastAPI. Get started in minutes without the hassle of complex setup!
This template is designed to streamline your FastAPI development by eliminating:
- 🛠️ Database configuration
- ⚙️ Environment setup
- 📂 Tedious project initialization and structure
- 🗂️ Manual setup of SQLAlchemy models and Pydantic schemas
- ✅ Type safety with Mypy support
- 🧹 Code quality ensured by Ruff linter and formatter
- 📦 Dependency management via Poetry
- 🗄️ Asynchronous SQLAlchemy with * AsyncPG* driver for high-performance work with * PostgreSQL*
- 🐳 Dockerized Postgres for quick database setup
- 🔄 Alembic migrations for seamless database schema management
- 🔐 JWT authentication using RS256 keys
- 🛡️ Pre-commit hooks using Ruff to automatically check code before commit
Follow these steps to set up and run the project locally.
-
Clone the Repository 📥
Using HTTPS:
git clone https://github.com/eugeneliukindev/FastAPI-Base-App-With-JWT
Using SSH (recommended):
git clone git@github.com:eugeneliukindev/FastAPI-Base-App-With-JWT
-
Install Dependencies ⬇️
This project uses Poetry for dependency management. See pyproject.toml for details.
Optional: Configure Poetry to create a virtual environment in the project root:
poetry config virtualenvs.in-project true
Install dependencies:
With
dev
dependencies (for development):poetry install
Without
dev
dependencies (for production):poetry install --without dev
-
Set Up the Database 🗄️
Use the provided Docker Compose file to spin up a Postgres database:
docker compose up -d
Note: Ensure Docker is installed and running.
-
Run Alembic Migrations 🔄
Apply an existing database migration using Alembic:
alembic upgrade head
-
Start the Application 🚀
Run the FastAPI server:
python main.py
Your API is now running! Access it at
http://localhost:8000
(or the configured port).
- 📚 API Documentation: Auto-generated by FastAPI at
/docs
(Swagger UI) or/redoc
- 🛠️ Development Tools:
- Run
mypy .
for type checking - Run
ruff check .
for linting - Run
ruff format .
for code formatting
- Run
-
Important Note About .env
The
.env
file is used to store sensitive data like database credentials. For demonstration purposes, it's not included in.gitignore
. For security reasons, we strongly recommend adding.env
to.gitignore
to prevent accidentally committing sensitive data to the repository:echo ".env" >> .gitignore
-
Generate RSA keys for JWT (RS256)
⚠️ Generate private key 🔑:
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
Extract the public key 🔑:
openssl rsa -pubout -in private_key.pem -out public_key.pem
Create a folder where these keys will be stored, for example
/jwt-keys
📁 Next add this folder to.gitignore
:echo "/jwt-keys" >> .gitignore
These keys can be used to sign and verify JWT tokens.
Do not use keys placed in the repository! This is done as an example!❗❗❗
-
Initializing pre-commit Hooks
If you installed
dev
dependencies via poetry install, pre-commit was included in the installation. To activate it, run:pre-commit install
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/YourFeature
) - Commit your changes (
git commit -m 'Add YourFeature'
) - Push to the branch (
git push origin feature/YourFeature
) - Open a Pull Request
This project is licensed under the MIT License. See the LICENSE file for details.
Happy coding! 🎉 If you have questions or need help, feel free to open an issue.