0
0
Rest APIprogramming~20 mins

Versioning best practices in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Versioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this API versioning example?

Consider a REST API that uses URL path versioning. The client requests /api/v2/users. The server code routes this to the version 2 handler, which returns a JSON list of users with an added email field.

What JSON response will the client receive?

Rest API
def get_users(version):
    if version == 'v1':
        return [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
    elif version == 'v2':
        return [{'id': 1, 'name': 'Alice', 'email': 'alice@example.com'}, {'id': 2, 'name': 'Bob', 'email': 'bob@example.com'}]

response = get_users('v2')
print(response)
A[{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
BError: Version not supported
C[{'id': 1, 'name': 'Alice', 'email': 'alice@example.com'}, {'id': 2, 'name': 'Bob', 'email': 'bob@example.com'}]
D[]
Attempts:
2 left
💡 Hint

Check which version the function returns with the email field included.

🧠 Conceptual
intermediate
2:00remaining
Which versioning method is best for backward compatibility?

There are several ways to version REST APIs: URL path versioning, query parameter versioning, and header versioning.

Which method is generally considered best for maintaining backward compatibility and clean URLs?

AUsing URL path versioning like /api/v1/resource
BUsing query parameters like /api/resource?version=1
CUsing custom headers like X-API-Version: 1
DNo versioning, just update the API directly
Attempts:
2 left
💡 Hint

Think about what makes URLs clear and easy to cache.

🔧 Debug
advanced
2:00remaining
Why does this API versioning code return 'default handler' unexpectedly?

The following Python code tries to parse the API version from a header and route the request. It returns 'default handler' even though the header has value 2. Why?

Rest API
def route_request(headers):
    version = headers.get('X-API-Version')
    if version == '1':
        return 'v1 handler'
    elif version == '2':
        return 'v2 handler'
    else:
        return 'default handler'

headers = {'X-API-Version': 2}
print(route_request(headers))
ASyntaxError due to missing colon
BKeyError because 'X-API-Version' is missing
CTypeError because header value is int, not string
DReturns 'default handler' because version is int, not string
Attempts:
2 left
💡 Hint

Check the type of the header value and how it is compared.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a versioned API route in Flask?

Given Flask web framework, which route decorator correctly defines a versioned API endpoint for version 1?

Rest API
from flask import Flask
app = Flask(__name__)

@app.route('/api/v1/users')
def get_users():
    return 'Users v1'
A@app.route('/api/users?version=1')
B@app.route('/api/v1/users')
C@app.route('/api/users/v1')
D@app.route('/api/users', version='1')
Attempts:
2 left
💡 Hint

Think about how URL paths are defined in Flask routes.

🚀 Application
expert
2:00remaining
How many distinct API versions are supported by this versioning logic?

Consider this API versioning logic in pseudocode:

if 'version' in request.headers:
    if request.headers['version'] in ['1', '2', '3']:
        use that version
    else:
        use default version 1
else:
    use default version 1

How many distinct API versions can clients explicitly request?

A3
B4
C1
D0
Attempts:
2 left
💡 Hint

Count the versions listed in the allowed list.