0
0
FastAPIframework~10 mins

Automatic API documentation (Swagger UI) in FastAPI - Interactive Code Practice

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

Complete the code to import the FastAPI class needed to create an app.

FastAPI
from fastapi import [1]

app = [1]()
Drag options to blanks, or click blank then click option'
AResponse
BFastAPI
CRequest
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI.
Trying to create app without importing FastAPI.
2fill in blank
medium

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

FastAPI
@app.[1]("/")
async def root():
    return {"message": "Welcome to FastAPI!"}
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using @app.post or other HTTP methods instead of @app.get.
Missing the decorator entirely.
3fill in blank
hard

Fix the error in the code to enable automatic API docs with Swagger UI.

FastAPI
from fastapi import FastAPI

app = FastAPI(docs_url=[1])

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}
Drag options to blanks, or click blank then click option'
ANone
B"/swagger"
C"/docs"
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting docs_url to None disables docs.
Using incorrect URL like '/swagger' which is not default.
Setting docs_url to False disables docs.
4fill in blank
hard

Fill both blanks to customize the OpenAPI title and version in the FastAPI app.

FastAPI
app = FastAPI(title=[1], version=[2])
Drag options to blanks, or click blank then click option'
A"My API"
B"1.0.0"
C"API Version"
D"v1"
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-string values for title or version.
Confusing title with version strings.
5fill in blank
hard

Fill all three blanks to add a description, contact email, and license info to the OpenAPI docs.

FastAPI
app = FastAPI(
    description=[1],
    contact=[2],
    license_info=[3]
)
Drag options to blanks, or click blank then click option'
A"This API manages items."
B{"email": "support@example.com"}
C{"name": "MIT"}
D"API for items management"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing contact or license_info as strings instead of dictionaries.
Omitting required keys in contact or license_info.