A FastAPI boilerplate for efficient project setup.
- Fork the repository and clone it:
git clone https://github.com/<username>/hng_boilerplate_python_fastapi_web.git
- Navigate into the project directory:
cd hng_boilerplate_python_fastapi_web - Switch to the development branch (if not already on
dev):git checkout dev
- Create a virtual environment:
python3 -m venv .venv
- Activate the virtual environment:
- On macOS/Linux:
source .venv/bin/activate - On Windows (PowerShell):
.venv\Scripts\Activate
- On macOS/Linux:
- Install project dependencies:
pip install -r requirements.txt
- Create a
.envfile from.env.sample:cp .env.sample .env
- Start the server:
python main.py
When setting up the database, you need to replace placeholders with your actual values. Below is a breakdown of where to replace them:
CREATE USER user WITH PASSWORD 'your_password';πΉ Replace:
userβ Your preferred database username (e.g.,fastapi_user).'your_password'β A secure password for the user (e.g.,'StrongP@ssw0rd').
β Example:
CREATE USER fastapi_user WITH PASSWORD 'StrongP@ssw0rd';CREATE DATABASE hng_fast_api;πΉ Replace:
hng_fast_apiβ Your preferred database name (e.g.,fastapi_db).
β Example:
CREATE DATABASE fastapi_db;GRANT ALL PRIVILEGES ON DATABASE hng_fast_api TO user;πΉ Replace:
hng_fast_apiβ The database name you used in Step 2.userβ The username you created in Step 1.
β Example:
GRANT ALL PRIVILEGES ON DATABASE fastapi_db TO fastapi_user;Edit the .env file to match your setup.
DATABASE_URL=postgresql://user:your_password@localhost/hng_fast_apiπΉ Replace:
userβ Your database username.your_passwordβ Your database password.hng_fast_apiβ Your database name.
β Example:
DATABASE_URL=postgresql://fastapi_user:StrongP@ssw0rd@localhost/fastapi_dbAfter setting up the database, test the connection:
psql -U user -d hng_fast_api -h localhostπΉ Replace:
userβ Your database username.hng_fast_apiβ Your database name.
β Example:
psql -U fastapi_user -d fastapi_db -h localhostalembic upgrade headDo NOT run alembic revision --autogenerate -m 'initial migration' initially!
alembic revision --autogenerate -m 'your migration message'
alembic upgrade headpython3 seed.py- After creating new tables or modifying models:
- Run Alembic migrations:
alembic revision --autogenerate -m "Migration message" alembic upgrade head - Ensure you import new models into
api/v1/models/__init__.py. - You do NOT need to manually import them in
alembic/env.py.
- Run Alembic migrations:
- Check if a related route file already exists in
api/v1/routes/.- If yes, add your route inside the existing file.
- If no, create a new file following the naming convention.
- Define the router inside the new route file:
- Include the prefix (without
/api/v1since it's already handled).
- Include the prefix (without
- Register the router in
api/v1/routes/__init__.py:from .new_route import router as new_router api_version_one.include_router(new_router)
Ensure pytest is installed in your virtual environment:
pip install pytestFrom the project root directory, run:
pytestThis will automatically discover and execute all test files in the tests/ directory.
To run tests in a specific model directory (e.g., tests/v1/user/):
pytest tests/v1/user/To run tests from a specific test file (e.g., test_signup.py inside tests/v1/auth/):
pytest tests/v1/auth/test_signup.pyIf you want to run a specific test inside a file, use:
pytest tests/v1/auth/test_signup.py::test_user_signupFor verbose output, add the -v flag:
pytest -vTo check test coverage, install pytest-cov:
pip install pytest-covThen run:
pytest --cov=apiIf you encounter this issue when running:
alembic revision --autogenerate -m 'your migration message'Run the following command first:
alembic upgrade headThen retry:
alembic revision --autogenerate -m 'your migration message'- Test your endpoints and models before pushing changes.
- Push Alembic migrations if database models are modified.
- Ensure your code follows project standards and passes tests before submitting a pull request.