Complete the code to import FastAPI and create an app instance.
from fastapi import [1] app = [1]()
We import FastAPI and create an app instance by calling FastAPI().
Complete the code to define a GET endpoint at path '/' that returns a welcome message.
@app.[1]("/") def read_root(): return {"message": "Welcome to FastAPI!"}
The @app.get decorator defines a GET HTTP method endpoint.
Fix the error in the code to correctly accept a path parameter 'item_id' as an integer.
@app.get("/items/{item_id}") def read_item(item_id: [1]): return {"item_id": item_id}
Path parameters can be typed. Here, item_id should be an integer, so use int.
Fill both blanks to define a POST endpoint '/items/' that accepts a JSON body with a Pydantic model 'Item'.
from pydantic import BaseModel class Item(BaseModel): name: str price: float @app.[1]("/items/") def create_item(item: [2]): return item
Use @app.post to define a POST endpoint and type the parameter as the Pydantic model Item to accept JSON body.
Fill all three blanks to run the FastAPI app with uvicorn on host '127.0.0.1' and port 8000.
import uvicorn if __name__ == "__main__": uvicorn.run([1], host=[2], port=[3])
To run the app, use uvicorn.run("app:app", host="127.0.0.1", port=8000). The string "app:app" means module app and variable app.