Home/Learn/FastAPI/First App — FastAPI + Uvicorn in Ten Lines

First App — FastAPI + Uvicorn in Ten Lines

Beginner
Fundamentals

pip install fastapi[standard], write main.py with an app and a path operation, run fastapi dev — you get hot reload, a live server, and Swagger docs at /docs in under a minute.

Overview

A FastAPI application is one object (app = FastAPI()) plus functions decorated with HTTP methods and paths — called path operations. The decorator names the method (@app.get, @app.post, @app.put, @app.delete) and the path; the function returns dicts/lists/Pydantic models that FastAPI serializes to JSON automatically. You run it with the fastapi dev command (or uvicorn main:app --reload) and develop against the live Swagger UI at /docs instead of a REST client. This chapter is the muscle-memory setup you will repeat for every service, including the venv discipline from the Python track.

Setup → main.py → Run

One venv, one install, one file. fastapi dev main.py runs Uvicorn with auto-reload — save the file and the server restarts. The JSON conversion of your return value is automatic.

venv → install → main.py → fastapi dev: the whole loop
# Setup (once) — venv first, always
python -m venv .venv
.venv\Scripts\activate                  # Windows (source .venv/bin/activate on Linux)
pip install "fastapi[standard]"

# ── main.py ─────────────────────────────────
from fastapi import FastAPI

app = FastAPI(title="Campus Placement API", version="0.1.0")

@app.get("/")
def home():
    return {"service": "placement-api", "status": "up"}

@app.get("/health")
def health():
    return {"ok": True}

# Run it:
fastapi dev main.py                      # dev mode: auto-reload, on :8000
# or explicitly:
uvicorn main:app --reload --port 8000    # main:app = file main.py, object app

# Now open:
#   http://127.0.0.1:8000          → {"service":"placement-api","status":"up"}
#   http://127.0.0.1:8000/docs     → interactive Swagger UI

Path Operations — GET, POST, PUT, DELETE

Each decorator maps an HTTP method + path to a function. Return dicts, lists, or Pydantic models; FastAPI handles JSON. Functions can be def or async def — both work (the difference matters later, in the Async chapter).

CRUD skeleton — one decorator per method + path
from fastapi import FastAPI

app = FastAPI()

students = {1: {"name": "Asha", "branch": "CS", "cgpa": 8.7}}

@app.get("/students")                    # READ all
def list_students():
    return list(students.values())

@app.get("/students/{student_id}")       # READ one
def get_student(student_id: int):
    return students.get(student_id, {"error": "not found"})

@app.post("/students")                   # CREATE
def create_student(payload: dict):       # (proper Pydantic body → next chapters)
    new_id = max(students) + 1
    students[new_id] = payload
    return {"id": new_id, **payload}

@app.put("/students/{student_id}")       # UPDATE (full replace)
def update_student(student_id: int, payload: dict):
    students[student_id] = payload
    return payload

@app.delete("/students/{student_id}")    # DELETE
def delete_student(student_id: int):
    students.pop(student_id, None)
    return {"deleted": student_id}

# Test from the terminal:
# curl -X POST http://127.0.0.1:8000/students \
#      -H "Content-Type: application/json" \
#      -d '{"name": "Ravi", "branch": "IT", "cgpa": 7.9}'

Key Points to Remember

  • 1app = FastAPI(); path operations are decorated functions (@app.get/post/put/delete)
  • 2fastapi dev main.py (or uvicorn main:app --reload) for development
  • 3Return dicts/lists/models — JSON serialization is automatic
  • 4Develop against /docs — the Swagger UI is your live REST client

Interview Questions

Sign in to ask Aria
1

What does uvicorn main:app --reload mean, piece by piece?

EasyInfosys
2

What is a path operation? How does FastAPI turn your return value into a response?

EasyMeesho
3

Both def and async def endpoints work in FastAPI — how are they executed differently?

HardRazorpay

Ask Aria about First App — FastAPI + Uvicorn in Ten Lines

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…