Bird
Raised Fist0
Rest APIprogramming~20 mins

Query parameter versioning in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Query Parameter 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 version check?
Given this Python Flask snippet that checks API version from query parameters, what will be the response if the request URL is /api/resource?version=2?
Rest API
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/api/resource')
def resource():
    version = request.args.get('version', '1')
    if version == '1':
        return jsonify({'message': 'Version 1 response'})
    elif version == '2':
        return jsonify({'message': 'Version 2 response'})
    else:
        return jsonify({'error': 'Unsupported version'}), 400
A500 Internal Server Error
B{"message": "Version 2 response"}
C{"error": "Unsupported version"}
D{"message": "Version 1 response"}
Attempts:
2 left
💡 Hint
Check how the version parameter is read and matched in the code.
🧠 Conceptual
intermediate
1:30remaining
Which HTTP status code is best for unsupported API version?
When a client requests an API version that the server does not support using query parameter versioning, which HTTP status code should the server return?
A400 Bad Request
B404 Not Found
C200 OK
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Think about client errors vs server errors.
🔧 Debug
advanced
2:30remaining
Why does this version check always return unsupported version response?
This Flask code is intended to return different responses based on the 'version' query parameter. But it always returns the unsupported version response, even when version=2 is requested. What is the bug?
Rest API
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/api/data')
def data():
    version = request.args.get('version')
    if version == 1:
        return jsonify({'message': 'Version 1 data'})
    elif version == 2:
        return jsonify({'message': 'Version 2 data'})
    else:
        return jsonify({'error': 'Unsupported version'}), 400
AThe version variable is a string, but compared to integers 1 and 2, so conditions never match.
BThe else block is missing a return statement.
CThe jsonify function is used incorrectly without parentheses.
DThe route decorator is missing a trailing slash causing routing issues.
Attempts:
2 left
💡 Hint
Check the data type of the version variable and the comparisons.
📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in this version check?
Identify which code snippet will cause a syntax error when checking the API version from query parameters.
Rest API
version = request.args.get('version', '1')
if version == '1':
    return 'v1'
elif version == '2':
    return 'v2'
else:
    return 'unsupported'
A
if version == '1':
    return 'v1'
Bif version == '1': return 'v1'
Cif version = '1': return 'v1'
D
if version == '1'
    return 'v1'
Attempts:
2 left
💡 Hint
Remember the difference between assignment and comparison operators.
🚀 Application
expert
2:30remaining
How many distinct responses does this API produce for versions 1 to 3?
Consider this Flask API code snippet that uses query parameter versioning. How many distinct JSON responses can this API produce when the version parameter is 1, 2, or 3?
Rest API
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/api/item')
def item():
    version = request.args.get('version', '1')
    match version:
        case '1':
            return jsonify({'data': 'Item v1'})
        case '2':
            return jsonify({'data': 'Item v2'})
        case '3':
            return jsonify({'data': 'Item v3'})
        case _:
            return jsonify({'error': 'Unsupported version'}), 400
A4
B0
C1
D3
Attempts:
2 left
💡 Hint
Count the distinct JSON responses for versions 1, 2, and 3 only.

Practice

(1/5)
1. What is the main purpose of using query parameter versioning in REST APIs?
easy
A. To change the main URL path for each API version
B. To allow clients to specify which API version they want using a query string
C. To hide the API version from clients
D. To force all clients to use the latest API version only

Solution

  1. Step 1: Understand query parameter versioning concept

    Query parameter versioning lets clients add a version number in the URL query string, like ?version=1.
  2. Step 2: Identify the purpose of this method

    This allows clients to choose which API version to use without changing the main URL path.
  3. Final Answer:

    To allow clients to specify which API version they want using a query string -> Option B
  4. Quick Check:

    Query parameter versioning = client selects version [OK]
Hint: Version in query string means client chooses API version [OK]
Common Mistakes:
  • Thinking versioning changes the main URL path
  • Assuming versioning hides API versions
  • Believing all clients must use latest version only
2. Which of the following is the correct way to request version 2 of an API using query parameter versioning?
easy
A. GET /api/resource/version=2
B. GET /api/version/2/resource
C. GET /api/resource?version=2
D. GET /api/resource#version=2

Solution

  1. Step 1: Recall query parameter syntax in URLs

    Query parameters come after a question mark ? and use key=value pairs, e.g., ?version=2.
  2. Step 2: Match the correct syntax for versioning

    The correct way to specify version 2 is by adding ?version=2 after the resource path.
  3. Final Answer:

    GET /api/resource?version=2 -> Option C
  4. Quick Check:

    Query param syntax = ?key=value [OK]
Hint: Query parameters start with ? and use key=value [OK]
Common Mistakes:
  • Using slashes instead of query parameters
  • Using # instead of ? for query parameters
  • Placing version inside the URL path incorrectly
3. Given this API endpoint code snippet handling versioning:
def get_data(request):
    version = request.GET.get('version', '1')
    if version == '1':
        return 'Data from v1'
    elif version == '2':
        return 'Data from v2'
    else:
        return 'Unknown version'

What will be the output if the client calls /api/data?version=2?
medium
A. "Data from v2"
B. "Unknown version"
C. "Data from v1"
D. Error: version parameter missing

Solution

  1. Step 1: Extract version from query parameters

    The code gets the version from the query string. If missing, it defaults to '1'. Here, version='2'.
  2. Step 2: Check version conditions

    Since version is '2', the code returns 'Data from v2'.
  3. Final Answer:

    "Data from v2" -> Option A
  4. Quick Check:

    version=2 returns v2 data [OK]
Hint: Check query param value matches version condition [OK]
Common Mistakes:
  • Assuming default version is always used
  • Confusing string and integer comparison
  • Expecting error when version is provided
4. Consider this code snippet for versioning:
def api_handler(request):
    version = request.GET['version']
    if version == '1':
        return 'Version 1'
    else:
        return 'Other version'

What error will occur if the client calls /api/data without a version parameter?
medium
A. KeyError because 'version' key is missing
B. SyntaxError in code
C. Returns 'Version 1' by default
D. Returns 'Other version'

Solution

  1. Step 1: Understand how version is accessed

    The code uses request.GET['version'] which raises KeyError if 'version' is missing.
  2. Step 2: Analyze call without version parameter

    Calling without ?version=... means 'version' key is missing, causing KeyError.
  3. Final Answer:

    KeyError because 'version' key is missing -> Option A
  4. Quick Check:

    Missing key access causes KeyError [OK]
Hint: Access query keys safely to avoid KeyError [OK]
Common Mistakes:
  • Assuming default version is used automatically
  • Expecting no error when parameter is missing
  • Confusing KeyError with SyntaxError
5. You want to support versions 1 and 2 of your API using query parameter versioning. Version 1 returns a list of users as strings, and version 2 returns a list of user objects with 'name' and 'id'. Which approach correctly handles this in a single endpoint?
hard
A. Return an error if version parameter is missing or unknown
B. Change the URL path to /v1/users and /v2/users instead of query parameters
C. Ignore the version parameter and always return the latest format
D. Use ?version=1 to return ['Alice', 'Bob'], and ?version=2 to return [{'name':'Alice','id':1},{'name':'Bob','id':2}]

Solution

  1. Step 1: Understand query parameter versioning usage

    Clients specify version using ?version=1 or ?version=2 in the same URL.
  2. Step 2: Return different data formats based on version

    Return simple list of strings for version 1, and detailed objects for version 2, matching client request.
  3. Final Answer:

    Use ?version=1 to return ['Alice', 'Bob'], and ?version=2 to return [{'name':'Alice','id':1},{'name':'Bob','id':2}] -> Option D
  4. Quick Check:

    Query param version controls response format [OK]
Hint: Return data format based on version query param [OK]
Common Mistakes:
  • Changing URL path instead of using query parameters
  • Ignoring version parameter and mixing formats
  • Returning errors unnecessarily for missing versions