Python / FastAPI 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) |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
