This is a beginner-friendly template for getting started with FastAPI and SQLAlchemy.
- Database connection using SQLAlchemy
- FastAPI server
- Unit testing with PyTest
- Basic CRUD for posts
- Python 3.10+
pip- PostgreSQL database
- Create and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate- Install dependencies:
pip install -r requirements.txt- Set environment variables:
| Key | Value |
|---|---|
DATABASE_URL |
postgresql://user:password@host:port/db |
Example (.env):
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/fastapi_template- Run the API:
uvicorn main:app --reloadOnce the app is running locally, open:
- Swagger UI:
http://127.0.0.1:8000/docs - ReDoc:
http://127.0.0.1:8000/redoc - OpenAPI JSON:
http://127.0.0.1:8000/openapi.json
pytestRun formatter:
black .- Create
.envfrom example:
cp .env.example .env- Start API + Postgres with Docker Compose:
docker compose up --build- API will be available at:
http://127.0.0.1:8000- Swagger:
http://127.0.0.1:8000/docs - ReDoc:
http://127.0.0.1:8000/redoc
DATABASE_URL is passed to the API container from .env through docker-compose.yml.
