Description: This project serves as a base template for building FastAPI applications. It provides a ready-to-use structure with database integration and development tools like Alembic for migrations and pre-commit hooks for code formatting. Use this as a starting point to quickly set up new FastAPI projects.
- Create a new project directory and navigate into it:
mkdir project-name
cd project-name- Clone the repository:
git clone git@github.com:osha890/fastapi-base.git .- Remove the default git remote:
git remote remove origin- Rename the project:
Edit the
pyproject.tomlfile and update the project name:
[project]
name = "project-name"- Install dependencies using Poetry:
poetry install- Activate the virtual environment:
poetry env activate- Set Python interpreter in IDE (optional):
Get the Python interpreter path:
poetry run which pythonThen in your IDE:
File → Settings → Project → Python Interpreter → ⚙️ → Add Interpreter → Add Local Interpreter → Select existing → Paste the provided path
- Configure your database environment variables:
DB__NAME=<your_db_name>
DB__USER=<your_db_user>
DB__PASSWORD=<your_db_password>Note: If there is no
.envfile, the variables will be taken from.env.template
- Install pre-commit hook:
pre-commit install- Start the database and Adminer using Docker:
docker compose up -d- Default ports:
- PostgreSQL:
5432 - Adminer:
8080
- Add new models in the
db.modelspackage. - Include the new models in
db.models.__init__.pyso Alembic can detect them. - All models should inherit from
Base. - To use an integer primary key for the
idfield, inherit fromIdIntPkMixin.
- Create migrations:
alembic revision --autogenerate -m "create some table"- Apply migrations:
alembic upgrade head- Rollback migrations:
- Rollback the last migration:
alembic downgrade -1- Rollback all migrations to the base state:
alembic downgrade base- Create new routers inside the
apipackage and include them in the main API router inapi.__init__.py. - Create new schemas inside the
schemapackage. - Implement CRUD functions inside the
db.repositoriespackage.
- The project uses
blackandisortfor code formatting. - If you add a new package in the root project, add it to
known_first_partyinpyproject.toml. - To run pre-commit manually:
pre-commit run --all-filesFollow these instructions to set up, develop, and maintain the FastAPI base project efficiently.