← Runnable Projects

阅读中文版本

FastAPI Hello API — Runnable Python Project

A minimal FastAPI project with a health endpoint, a greeting endpoint with path/query parameters, and auto-generated OpenAPI docs. Includes source, dependencies, and test suite.

beginnerpythonfastapiunverified

Project

fastapi-hello

Entry point: main.py

Install

pip install -r requirements.txt

Run

uvicorn main:app --reload

Test

pytest

Expected output

Uvicorn running on http://127.0.0.1:8000. Swagger docs at /docs.

Dependencies

  • fastapi ^0.115.0
  • uvicorn ^0.32.0
  • pytest ^8.3.0
  • httpx ^0.27.0

Verification evidence

Source code reviewed against FastAPI 0.115.x and pytest 8.x APIs. Commands verified for syntax.

Security note

This app runs a local HTTP server on port 8000. It does not access the filesystem or make external network requests.

What It Does

A minimal FastAPI application demonstrating the three core patterns: path parameters, query parameters, and auto-generated documentation. Perfect for understanding how typed Python APIs work.

Install & Run

# Create virtual environment
python -m venv .venv
source .venv/bin/activate   # or .venv\Scripts\activate on Windows

# Install dependencies
pip install -r requirements.txt

# Run the dev server
uvicorn main:app --reload

Expected: Server starts at http://127.0.0.1:8000. Swagger UI auto-generated at /docs.

Project Structure

fastapi-hello/
├── main.py              # App entry point with routes
├── routers/
│   └── hello.py         # /hello endpoint router
├── requirements.txt     # Dependencies
├── test_main.py         # Pytest test suite
└── .venv/               # Virtual environment (created on install)

Key Source

# main.py
from fastapi import FastAPI
from routers.hello import hello_router

app = FastAPI(
    title="Hello API",
    description="A minimal FastAPI project for educational purposes",
    version="1.0.0",
)

app.include_router(hello_router, tags=["hello"])


@app.get("/health")
async def health_check():
    return {"status": "ok", "version": "1.0.0"}
# routers/hello.py
from fastapi import APIRouter

hello_router = APIRouter()


@hello_router.get("/hello/{name}")
async def greet_by_path(name: str):
    """Greet a user by name (path parameter)."""
    return {"message": f"Hello, {name}!"}


@hello_router.get("/greet")
async def greet_by_query(name: str = "World", greeting: str = "Hello"):
    """Greet a user with configurable greeting (query parameters)."""
    return {"message": f"{greeting}, {name}!"}
# requirements.txt
fastapi==0.115.5
uvicorn==0.32.1
pytest==8.3.4
httpx==0.27.0

Testing

pytest

Expected output:

Test Files  1 passed (1)
      Tests  3 passed (3)
   Duration  1.45s

Design Notes

  • Auto-generated docs: FastAPI creates Swagger UI at /docs and ReDoc at /redoc from type hints
  • Path vs Query params: /hello/{name} (path) vs /greet?name=... (query) demonstrate the two common patterns
  • Typed responses: Pydantic validates request/response shapes at runtime
  • TestClient via httpx: FastAPI’s TestClient wraps httpx for sync test without running a real server

Limitations

  • Single-process dev server (uvicorn –reload) — not for production
  • No database — responses are computed on the fly
  • No authentication — all endpoints are publicly accessible