Query parameter versioning helps you manage different versions of an API by adding a version number in the URL query. This way, clients can ask for the version they want.
Query parameter versioning in Rest API
GET /api/resource?version=1 GET /api/resource?version=2
The version is passed as a query parameter, usually named version.
This method keeps the base URL the same and only changes the query part.
GET /users?version=1GET /users?version=2GET /products?version=1This simple Flask app shows how to use query parameter versioning. It checks the version parameter and returns different data based on it.
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/api/data') def get_data(): version = request.args.get('version', '1') if version == '1': return jsonify({'message': 'This is version 1 data'}) elif version == '2': return jsonify({'message': 'This is version 2 data with extra info', 'extra': 123}) else: return jsonify({'error': 'Version not supported'}), 400 if __name__ == '__main__': app.run(debug=True)
Query parameter versioning is easy to implement but can clutter URLs if many parameters are used.
Clients must remember to add the version parameter to get the right API version.
It works well when you want to keep the URL path clean and use parameters for version control.
Query parameter versioning uses a URL query like ?version=1 to select API versions.
It helps keep old and new API versions available without changing the main URL path.
Clients specify the version they want by adding the version number in the query string.