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
Query Parameter Versioning in a REST API
📖 Scenario: You are building a simple REST API that returns a greeting message. You want to support different versions of the API using query parameters.For example, /greet?version=1 returns a basic greeting, and /greet?version=2 returns a more detailed greeting.
🎯 Goal: Create a REST API endpoint /greet that reads the version query parameter and returns the correct greeting message based on the version number.
📋 What You'll Learn
Create a dictionary called greetings with keys as version numbers (integers) and values as greeting messages (strings).
Create a variable called default_version set to 1.
Write a function called get_greeting that takes a version parameter and returns the greeting message from the greetings dictionary or a default message if the version is not found.
Print the greeting message for the version passed as a query parameter.
💡 Why This Matters
🌍 Real World
APIs often need to support multiple versions so that older clients can still work while new features are added. Using query parameters for versioning is one simple way to do this.
💼 Career
Understanding how to handle API versioning is important for backend developers and anyone working with web services to ensure smooth upgrades and backward compatibility.
Progress0 / 4 steps
1
Create the greetings dictionary
Create a dictionary called greetings with these exact entries: 1: 'Hello!', 2: 'Hello, welcome to version 2 of our API!'
Rest API
Hint
Use curly braces {} to create a dictionary with keys 1 and 2 and their greeting messages.
2
Set the default version
Create a variable called default_version and set it to 1.
Rest API
Hint
Just assign the number 1 to the variable default_version.
3
Write the get_greeting function
Write a function called get_greeting that takes a parameter version and returns the greeting message from the greetings dictionary if the version exists, or returns 'Hello!' if the version is not found.
Rest API
Hint
Use the dictionary get method to return the greeting or a default message.
4
Print the greeting for a query parameter version
Assume a variable query_version holds the version number from the query parameter. Set query_version to 2. Then print the greeting message by calling get_greeting(query_version).
Rest API
Hint
Assign 2 to query_version and print the result of get_greeting(query_version).
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
Query parameter versioning lets clients add a version number in the URL query string, like ?version=1.
Step 2: Identify the purpose of this method
This allows clients to choose which API version to use without changing the main URL path.
Final Answer:
To allow clients to specify which API version they want using a query string -> Option B
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
Step 1: Recall query parameter syntax in URLs
Query parameters come after a question mark ? and use key=value pairs, e.g., ?version=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.
Final Answer:
GET /api/resource?version=2 -> Option C
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
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'.
Step 2: Check version conditions
Since version is '2', the code returns 'Data from v2'.
Final Answer:
"Data from v2" -> Option A
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
Step 1: Understand how version is accessed
The code uses request.GET['version'] which raises KeyError if 'version' is missing.
Step 2: Analyze call without version parameter
Calling without ?version=... means 'version' key is missing, causing KeyError.
Final Answer:
KeyError because 'version' key is missing -> Option A
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}]