0
0
FastAPIframework~10 mins

API versioning strategies 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 define a FastAPI app with a versioned path prefix.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/api/v[1]/items")
def read_items():
    return {"version": "[1]", "items": ["apple", "banana"]}
Drag options to blanks, or click blank then click option'
A1
Bv2
C2
Dv1
Attempts:
3 left
💡 Hint
Common Mistakes
Including the 'v' in the version number when it should be separate in the path.
Using a string like 'v1' directly in the version placeholder.
2fill in blank
medium

Complete the code to add a header-based version check in FastAPI.

FastAPI
from fastapi import FastAPI, Header, HTTPException
app = FastAPI()

@app.get("/items")
async def read_items(x_api_version: str = Header([1])):
    if x_api_version != "1":
        raise HTTPException(status_code=400, detail="Unsupported API version")
    return {"items": ["apple", "banana"]}
Drag options to blanks, or click blank then click option'
A"1"
B"v1"
CNone
D"X-API-Version"
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the default header value to a string which makes it always that value.
Using the header name as default value instead of None.
3fill in blank
hard

Fix the error in the code to correctly implement query parameter versioning.

FastAPI
from fastapi import FastAPI, Query, HTTPException
app = FastAPI()

@app.get("/items")
async def read_items(version: str = Query([1])):
    if version != "1":
        raise HTTPException(status_code=400, detail="Unsupported API version")
    return {"items": ["apple", "banana"]}
Drag options to blanks, or click blank then click option'
A"v1"
BNone
C"1"
D"version"
Attempts:
3 left
💡 Hint
Common Mistakes
Setting default to a string makes the parameter always that value.
Using the parameter name as default value.
4fill in blank
hard

Fill both blanks to create two FastAPI routers for version 1 and version 2 with path prefixes.

FastAPI
from fastapi import FastAPI, APIRouter
app = FastAPI()

router_v[1] = APIRouter(prefix="/api/v[1]")
router_v[2] = APIRouter(prefix="/api/v[2]")

@router_v[1].get("/items")
async def get_items_v1():
    return {"version": [1], "items": ["apple"]}

@router_v[2].get("/items")
async def get_items_v2():
    return {"version": [2], "items": ["apple", "banana"]}

app.include_router(router_v[1])
app.include_router(router_v[2])
Drag options to blanks, or click blank then click option'
A1
B2
Cv1
Dv2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'v1' or 'v2' in the blanks leads to incorrect paths like '/api/vv1/items'.
Mixing version numbers and strings inconsistently.
5fill in blank
hard

Fill all three blanks to implement header versioning with dependency injection in FastAPI.

FastAPI
from fastapi import FastAPI, Header, HTTPException, Depends
app = FastAPI()

def get_api_version([3]: str = Header([1])):
    if [3] not in ["1", "2"]:
        raise HTTPException(status_code=400, detail="Unsupported API version")
    return [3]

@app.get("/items")
async def read_items(version: str = Depends([2])):
    if version == "1":
        return {"items": ["apple"]}
    elif version == "2":
        return {"items": ["apple", "banana"]}
    return {"items": []}
Drag options to blanks, or click blank then click option'
ANone
Bget_api_version
Cx_api_version
D"1"
Attempts:
3 left
💡 Hint
Common Mistakes
Setting header default to a string instead of None.
Not using Depends for dependency injection.
Using wrong parameter names that don't match header.