FastAPI Essential, Pre-Configured FastAPI Project with all essential dependencies and configurations for a robust and scalable FastAPI application.
Follow these steps to set up your development environment for the first time.
- Python 3.12 or newer.
- UV installed. This project uses
uvfor package and environment management.
-
Clone the Repository
git clone <your-repository-url> cd fastapi-essential
-
Install All Dependencies Run the following single command. It will automatically create a virtual environment, install all production and development dependencies, and install the project itself in "editable" mode.
uv sync --dev
This command creates a
uv.lockfile, which should be committed to your repository to ensure all developers use the exact same package versions. -
Initialize Pre-Commit Hooks Set up pre-commit hooks to automatically format and lint your code before each commit. This is crucial for maintaining code quality.
uv run pre-commit install
To start the FastAPI development server with live reloading, run:
uv run uvicorn src.main:app --reload --port 7070or use the following command:
uv run fastapi dev ./src/main.py --reload --port 7070uvicornis the ASGI server that runs your FastAPI application.src.main:apppoints to theappobject inside thesrc/main.pyfile.--reloadautomatically restarts the server when you make code changes.
To ensure your local environment is perfectly synchronized with the project's lock file (uv.lock), especially after pulling changes from git, run:
uv sync --devThis command installs any new packages and, importantly, removes any that are no longer required, preventing environment conflicts.
While pre-commit handles checks automatically, you can—and should—run these tools manually to format your code and get immediate feedback.
Ruff is an extremely fast linter and code formatter.
-
Format your code: This command reformats all files in the
srcdirectory to match the project's style.uv run ruff format src
-
Check for errors and auto-fix them: This command finds potential bugs and style issues, and automatically fixes anything it can.
uv run ruff check src --fix
Pyright checks your code for type errors, helping you catch bugs before you even run the application.
-
Run a one-time check:
uv run pyright
If there are no errors, the command will finish silently. Otherwise, it will print a detailed report.
-
Run in "watch" mode for live feedback (Recommended): This command provides instant feedback in your terminal every time you save a file.
uv run pyright --watch
The project is organized using a modern src layout.
/fastapi-essential
├── .github/ # GitHub Actions workflows
├── .venv/ # Virtual environment managed by uv
├── alembic/ # Alembic database migration scripts
├── src/ # Main application source code
│ ├── configs/ # Configuration (database, settings)
│ ├── functions/ # Business logic and services
│ ├── models/ # SQLAlchemy ORM models
│ ├── routes/ # API endpoint definitions (routers)
│ │ └── v1/
│ ├── __init__.py
│ └── main.py # FastAPI application entry point
├── tests/ # Unit and integration tests
├── .gitignore
├── .pre-commit-config.yaml # Configuration for pre-commit hooks
├── pyproject.toml # Project metadata, dependencies, and tool configs (Ruff, Pyright)
├── README.md # This file
└── uv.lock # Lockfile for reproducible builds```
* **`src/`**: All the application's Python code lives here. This separation prevents common import issues.
* **`alembic/`**: Handles database schema migrations.
* **`pyproject.toml`**: The single configuration file for the entire project. It defines dependencies and configures tools like Ruff and Pyright.
* **`uv.lock`**: A lockfile that guarantees identical environments for all developers and in production.