0
0
FastAPIframework~20 mins

API versioning strategies in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Versioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing the v1 endpoint?

Consider this FastAPI app with two versions of the same endpoint using path versioning:

from fastapi import FastAPI

app = FastAPI()

@app.get('/v1/items')
async def get_items_v1():
    return {'version': 'v1', 'items': [1, 2, 3]}

@app.get('/v2/items')
async def get_items_v2():
    return {'version': 'v2', 'items': ['a', 'b', 'c']}

What will be the JSON response when a client requests /v1/items?

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get('/v1/items')
async def get_items_v1():
    return {'version': 'v1', 'items': [1, 2, 3]}

@app.get('/v2/items')
async def get_items_v2():
    return {'version': 'v2', 'items': ['a', 'b', 'c']}
A404 Not Found error
B{"version": "v2", "items": [1, 2, 3]}
C{"version": "v1", "items": [1, 2, 3]}
D{"version": "v2", "items": ["a", "b", "c"]}
Attempts:
2 left
💡 Hint

Check which function handles the /v1/items path.

📝 Syntax
intermediate
2:00remaining
Which option correctly implements header-based API versioning in FastAPI?

You want to create a FastAPI endpoint that returns different responses based on a custom header X-API-Version. Which code snippet correctly reads this header and returns the version?

A
from fastapi import FastAPI, Header

app = FastAPI()

@app.get('/items')
async def get_items(x_api_version: str = Header(...)):
    return {"version": x_api_version}
B
from fastapi import FastAPI

app = FastAPI()

@app.get('/items')
async def get_items(x_api_version: str):
    return {"version": x_api_version}
C
from fastapi import FastAPI, Header

app = FastAPI()

@app.get('/items')
async def get_items(x_api_version: Header):
    return {"version": x_api_version}
D
from fastapi import FastAPI

app = FastAPI()

@app.get('/items')
async def get_items():
    version = request.headers.get('X-API-Version')
    return {"version": version}
Attempts:
2 left
💡 Hint

Use FastAPI's Header dependency to read headers.

state_output
advanced
2:00remaining
What is the response when using query parameter versioning with missing version?

Given this FastAPI app using query parameter for versioning:

from fastapi import FastAPI, Query, HTTPException

app = FastAPI()

@app.get('/items')
async def get_items(version: str = Query(None)):
    if version == '1':
        return {'version': 'v1', 'items': [1, 2]}
    elif version == '2':
        return {'version': 'v2', 'items': ['a', 'b']}
    else:
        raise HTTPException(status_code=400, detail='Invalid or missing version')

What happens if a client calls /items without the version query parameter?

FastAPI
from fastapi import FastAPI, Query, HTTPException

app = FastAPI()

@app.get('/items')
async def get_items(version: str = Query(None)):
    if version == '1':
        return {'version': 'v1', 'items': [1, 2]}
    elif version == '2':
        return {'version': 'v2', 'items': ['a', 'b']}
    else:
        raise HTTPException(status_code=400, detail='Invalid or missing version')
A{"version": "v1", "items": [1, 2]}
BHTTP 404 Not Found error
C{"version": "v2", "items": ["a", "b"]}
DHTTP 400 error with detail 'Invalid or missing version'
Attempts:
2 left
💡 Hint

Check the else branch when version is None.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI app fail to route requests correctly with prefix versioning?

Examine this FastAPI app using APIRouter with prefix for versioning:

from fastapi import FastAPI, APIRouter

app = FastAPI()

v1_router = APIRouter(prefix='/v1')

@v1_router.get('/items')
async def get_items_v1():
    return {'version': 'v1'}

app.include_router(v1_router)

@app.get('/items')
async def get_items_default():
    return {'version': 'default'}

Requests to /v1/items return 404. What is the likely cause?

FastAPI
from fastapi import FastAPI, APIRouter

app = FastAPI()

v1_router = APIRouter(prefix='/v1')

@v1_router.get('/items')
async def get_items_v1():
    return {'version': 'v1'}

app.include_router(v1_router)

@app.get('/items')
async def get_items_default():
    return {'version': 'default'}
AThe route path in the router should be '/v1/items' instead of '/items'
BThe prefix should be set in include_router, not in APIRouter constructor
CThe router was included without specifying the prefix in include_router
DThe app variable was overwritten after including the router
Attempts:
2 left
💡 Hint

Check how prefix is applied when including routers.

🧠 Conceptual
expert
3:00remaining
Which API versioning strategy best supports backward compatibility and gradual client migration?

Among these API versioning strategies, which one is best suited to maintain backward compatibility while allowing clients to migrate gradually?

  • Path versioning (e.g., /v1/resource)
  • Header versioning (e.g., X-API-Version header)
  • Query parameter versioning (e.g., ?version=1)
  • Content negotiation (Accept header with version)
AHeader versioning, because it hides version info from URLs and allows flexible version negotiation
BPath versioning, because each version has a separate URL and clients can choose which to call
CQuery parameter versioning, because it is easy to implement and clients can switch versions by changing parameters
DContent negotiation, because it uses standard HTTP headers and supports smooth version transitions
Attempts:
2 left
💡 Hint

Think about how clients can keep using old versions while moving to new ones.