Python / FastAPI Basics Interview Questions
How do you create and run a minimal FastAPI application?
A FastAPI app needs just a few lines. You create a FastAPI() instance and decorate Python functions with HTTP method decorators. The app is served by an ASGI server — Uvicorn is the standard choice.
# main.py from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"message": "Hello, FastAPI!"} @app.get("/items/{item_id}") def read_item(item_id: int, q: str | None = None): return {"item_id": item_id, "q": q}
# Run the server uvicorn main:app --reload # main = the module (main.py) # app = the FastAPI() instance variable # --reload = auto-restart on code changes (dev only)
After starting, visit http://127.0.0.1:8000/docs for the interactive Swagger UI, which FastAPI generates automatically from your code. The --reload flag is for development only — never use it in production.
| Flag | Purpose |
|---|---|
| --reload | Auto-restart on file change (dev) |
| --host 0.0.0.0 | Listen on all interfaces |
| --port 8080 | Custom port (default: 8000) |
| --workers 4 | Multiple worker processes (prod) |
More Related questions...