Stop 13 · Choose a Path: Web, Data or AIProjects · 1/2

REST API with FastAPI

Part of the Python Roadmap

In short

This is the project that proves you can build a real backend: an API with validation, a database, tests and a Docker image. It is the kind of thing backend interviewers love to open and explore.

A complete REST API, such as a college events or expense API, built with FastAPI, Pydantic validation and a SQLite or PostgreSQL database through SQLAlchemy, with tests and Docker. It is a strong portfolio piece for backend roles.

The jaadu works as a station master on a small sunny railway platform, checking tickets at the gate and waving a green flag at a little train.

Think of it like this

Running a small railway station

Tickets are checked at the gate (Pydantic validation), the station master looks up and updates the reservation chart (the database through SQLAlchemy), and inspectors test every route before the trains run (pytest). Each part is simple. Running all of them together, smoothly, is the skill.

Watch it happen

Project layout

events-api/
app/
main.py (routes)
models.py (tables)
schemas.py (Pydantic)
database.py (engine, session)
tests/test_events.py
Dockerfile
README.md

1/5Keep app code in its own package so tests can import it.

The big ideas

What you need to know

Pick a small real domain

College events, a hostel mess menu or a personal expense tracker. Two or three related resources are plenty.

Dependency injection

Depends(get_db) gives each request its own database session and closes it afterwards.

Test every endpoint

Write tests for success, not found and invalid input. CI on GitHub Actions can run them on every push.

Deploy it

Put it on a free tier of Render, Railway or a similar host so reviewers can click a live /docs link.

How it works

Build order

  1. 1

    Design

    Write the endpoints and JSON shapes on paper first.

  2. 2

    Database

    Create SQLAlchemy models and a session dependency.

  3. 3

    Endpoints

    Add CRUD routes with Pydantic schemas and proper status codes.

  4. 4

    Tests

    Use pytest and TestClient with a separate test database.

  5. 5

    Package

    Add a Dockerfile, a README with screenshots of /docs and deploy.

See it in code

Walk through the code

python
1from fastapi import Depends, FastAPI, HTTPException2from pydantic import BaseModel, ConfigDict3from sqlalchemy import create_engine4from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column5 6engine = create_engine("sqlite:///events.db", connect_args={"check_same_thread": False})7 8class Base(DeclarativeBase):9    pass10 11class EventRow(Base):12    __tablename__ = "events"13    id: Mapped[int] = mapped_column(primary_key=True)14    title: Mapped[str]15    city: Mapped[str]16 17Base.metadata.create_all(engine)18 19class EventIn(BaseModel):20    title: str21    city: str22 23class EventOut(EventIn):24    model_config = ConfigDict(from_attributes=True)25    id: int26 27def get_db():28    with Session(engine) as session:29        yield session30 31app = FastAPI(title="College Events API")32 33@app.post("/events", status_code=201, response_model=EventOut)34def create_event(data: EventIn, db: Session = Depends(get_db)):35    row = EventRow(**data.model_dump())36    db.add(row)37    db.commit()38    db.refresh(row)39    return row40 41@app.get("/events/{event_id}", response_model=EventOut)42def get_event(event_id: int, db: Session = Depends(get_db)):43    row = db.get(EventRow, event_id)44    if row is None:45        raise HTTPException(status_code=404, detail="Event not found")46    return row

1/6SQLite needs check_same_thread=False because FastAPI may use a session from a different thread.

The core of the project in one file. Split it into the layout above as it grows, and add list, update and delete routes.

Save yourself the pain

Common mistakes

Oops

Returning SQLAlchemy objects with internal fields straight to the client.

Do this instead

Always set a response_model or return type so only the fields you choose leave the API.

Oops

Running tests against the same events.db you use for development.

Do this instead

Override get_db in tests with app.dependency_overrides to point at a fresh test database.

Oops

A README that just says 'FastAPI project'.

Do this instead

Explain what it does, how to run it locally and with Docker, list the endpoints and link the live /docs.

Your mission

Build and ship it

~240 min

Quick check

What does Depends(get_db) do in a FastAPI endpoint?

Go deeper

  1. 1Implement full create, read, update and delete endpoints with validation.
  2. 2Write pytest tests using FastAPI's TestClient.
  3. 3Containerise it with Docker and document how to run it in the README.

More projects at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge