Complete the code to create a basic FastAPI app instance.
from fastapi import [1] app = [1]()
The FastAPI class is used to create the app instance.
Complete the code to define a GET endpoint that returns a greeting.
@app.[1]("/") async def read_root(): return {"message": "Hello World"}
The @app.get decorator defines a GET HTTP method endpoint.
Fix the error in the dependency injection by completing the code.
from fastapi import Depends def common_parameters(q: str = None): return {"q": q} @app.get("/items/") async def read_items(params: dict = [1](common_parameters)): return params
The Depends function is used to declare dependencies in FastAPI.
Fill both blanks to create a reusable router and include it in the main app.
from fastapi import APIRouter router = [1]() @router.get("/users") async def get_users(): return ["Alice", "Bob"] app.[2](router)
APIRouter() creates a router instance. app.include_router() adds it to the main app.
Fill all three blanks to create a Pydantic model, use it in a POST endpoint, and return the data.
from pydantic import BaseModel from fastapi import FastAPI app = FastAPI() class Item([1]): name: str price: float @app.post("/items/") async def create_item(item: [2]): return {"item_name": item.[3]
The model class inherits from BaseModel. The endpoint parameter uses the model Item. The returned value accesses the name attribute.