Setup

FastAPI interview question · Easy difficulty

How do you scaffold, write, and run a minimal FastAPI app?easy

Status
✓
Type
conceptual
Topic
setup
Frequency
common
Tags
FastAPI, uvicorn, setup, Swagger UI, ReDoc
Answer

Create a virtual environment, install fastapi and uvicorn[standard], define an app = FastAPI() instance with route decorators like @app.get, then run it with uvicorn main:app --reload. FastAPI automatically serves interactive docs at /docs (Swagger UI) and /redoc (ReDoc).

Explanation

A typical setup is python3 -m venv venv, activating it, then pip install fastapi "uvicorn[standard]". uvicorn[standard] pulls in extras like uvloop and httptools for better performance, though plain uvicorn also works for learning or dev. app is the core application object, and every route is registered on it with a decorator matching the HTTP method (@app.get, @app.post, @app.put, @app.delete). Returning a plain Python dict from a route is enough — FastAPI serializes it to JSON automatically. uvicorn main:app --reload means "in main.py, use the object named app," and --reload restarts the server on code changes for local development only, never in production.

Follow-upWhat does uvicorn main:app --reload actually do?
Follow-upWhy does uvicorn[standard] install extras like uvloop?
Follow-upWhat's the difference between the Swagger UI and ReDoc pages FastAPI generates?