0
0
FastAPIframework~10 mins

Why production readiness matters in FastAPI - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic FastAPI app instance.

FastAPI
from fastapi import [1]

app = [1]()
Drag options to blanks, or click blank then click option'
AFastAPI
BRequest
CResponse
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI.
Forgetting to add parentheses when creating the app instance.
2fill in blank
medium

Complete the code to define a GET endpoint that returns a welcome message.

FastAPI
@app.[1]("/")
async def read_root():
    return {"message": "Welcome to production-ready FastAPI!"}
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or PUT instead of GET for a simple read endpoint.
Misspelling the decorator name.
3fill in blank
hard

Fix the error in the code to correctly run the FastAPI app with Uvicorn.

FastAPI
import uvicorn

if __name__ == "__main__":
    uvicorn.run("main:[1]", host="127.0.0.1", port=8000, reload=True)
Drag options to blanks, or click blank then click option'
Aapp_instance
Bapplication
Capp
Dserver
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong app instance name that does not exist.
Forgetting the colon between module and app name.
4fill in blank
hard

Fill both blanks to add middleware that logs requests and responses.

FastAPI
from fastapi.middleware.[1] import [2]

app.add_middleware([2])
Drag options to blanks, or click blank then click option'
Alogging
Bcors
CLoggerMiddleware
DCORSMiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing CORS middleware with logging middleware.
Using middleware class names that don't exist in FastAPI.
5fill in blank
hard

Fill all three blanks to create a dependency that checks an API key in headers.

FastAPI
from fastapi import Header, HTTPException, [1]

async def verify_api_key(api_key: str = [2](alias="X-API-Key")):
    if api_key != "secret123":
        raise [3](status_code=401, detail="Invalid API Key")
Drag options to blanks, or click blank then click option'
ADepends
BHeader
CHTTPException
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using Request instead of Depends for dependency injection.
Not raising HTTPException for unauthorized access.