To create and run a FastAPI application, we install the fastapi[standard] package, define the routes (typically main.py), and start the development server using the fastapi dev command.
# main.py
from fastapi import FastAPI
app = FastAPI(title="Demo API", version="1.0.0")
@app.get("/")
def root():
return {"message": "Hello World"}pip install fastapi uvicorn
uvicorn main:app --reload # development (main = file, app = instance)
fastapi dev main.py # newer CLI alternative
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4 # production style