API versioning helps keep different versions of your app's features separate. Blueprints let you organize these versions cleanly.
API versioning with blueprints in Flask
from flask import Blueprint v1 = Blueprint('v1', __name__, url_prefix='/api/v1') v2 = Blueprint('v2', __name__, url_prefix='/api/v2') @v1.route('/hello') def hello_v1(): return 'Hello from API v1' @v2.route('/hello') def hello_v2(): return 'Hello from API v2' app.register_blueprint(v1) app.register_blueprint(v2)
Each Blueprint has a url_prefix to separate versions in the URL.
Register each Blueprint with the main Flask app to activate routes.
/api/v1/users.v1 = Blueprint('v1', __name__, url_prefix='/api/v1') @v1.route('/users') def users_v1(): return 'Users list from v1'
/api/v2/users that can return more data.v2 = Blueprint('v2', __name__, url_prefix='/api/v2') @v2.route('/users') def users_v2(): return 'Users list from v2 with extra info'
This Flask app uses two Blueprints to serve two API versions. Visiting /api/v1/hello returns a message from version 1, and /api/v2/hello returns a message from version 2.
from flask import Flask, Blueprint app = Flask(__name__) v1 = Blueprint('v1', __name__, url_prefix='/api/v1') @v1.route('/hello') def hello_v1(): return 'Hello from API v1' v2 = Blueprint('v2', __name__, url_prefix='/api/v2') @v2.route('/hello') def hello_v2(): return 'Hello from API v2' app.register_blueprint(v1) app.register_blueprint(v2) if __name__ == '__main__': app.run(debug=True)
Use clear URL prefixes like /api/v1 to avoid confusion.
Keep your versioned code separate to make updates easier.
Test each version independently to avoid breaking changes.
API versioning with Blueprints helps organize different API versions cleanly.
Use url_prefix in Blueprints to separate versions in URLs.
Register each version Blueprint with your Flask app to activate routes.
Practice
Solution
Step 1: Understand Blueprints role in Flask
Blueprints allow grouping routes and related code logically.Step 2: Connect Blueprints to API versioning
Usingurl_prefixin Blueprints lets you separate API versions by URL path.Final Answer:
To organize different API versions with separate URL prefixes -> Option CQuick Check:
Blueprints + url_prefix = versioned APIs [OK]
- Thinking Blueprints speed up the server
- Confusing Blueprints with session management
- Assuming Blueprints create database models
Solution
Step 1: Recall Blueprint constructor parameters
Blueprint(name, import_name, url_prefix=...)Step 2: Identify correct syntax
bp = Blueprint('api_v1', __name__, url_prefix='/api/v1') uses correct order andurl_prefixkeyword.Final Answer:
bp = Blueprint('api_v1', __name__, url_prefix='/api/v1') -> Option AQuick Check:
Blueprint(name, __name__, url_prefix=...) correct [OK]
- Omitting __name__ parameter
- Using 'prefix' instead of 'url_prefix'
- Swapping parameter order incorrectly
from flask import Flask, Blueprint
app = Flask(__name__)
bp_v1 = Blueprint('v1', __name__, url_prefix='/api/v1')
@bp_v1.route('/hello')
def hello_v1():
return 'Hello from v1!'
bp_v2 = Blueprint('v2', __name__, url_prefix='/api/v2')
@bp_v2.route('/hello')
def hello_v2():
return 'Hello from v2!'
app.register_blueprint(bp_v1)
app.register_blueprint(bp_v2)Solution
Step 1: Check Blueprint url_prefix for v2
Blueprint 'v2' uses url_prefix='/api/v2'.Step 2: Check route path inside v2 Blueprint
Route is '/hello', so full path is '/api/v2/hello'.Final Answer:
/api/v2/hello -> Option AQuick Check:
Blueprint prefix + route = full URL [OK]
- Ignoring url_prefix and using only route path
- Confusing v1 and v2 prefixes
- Assuming route is registered at root '/'
bp_v1 = Blueprint('v1', __name__, url_prefix='/api/v1')
@bp_v1.route('/data')
def data_v1():
return 'Data v1'
app.register_blueprint(bp_v1)
bp_v2 = Blueprint('v2', __name__, url_prefix='/api/v2')
@bp_v1.route('/data')
def data_v2():
return 'Data v2'
app.register_blueprint(bp_v2)Solution
Step 1: Identify route decorators and Blueprints
The second route uses@bp_v1.routebut should use@bp_v2.route.Step 2: Understand impact of wrong decorator
This causes the second function to register on the first Blueprint, not the second.Final Answer:
The second route decorator uses bp_v1 instead of bp_v2 -> Option DQuick Check:
Route decorator must match Blueprint instance [OK]
- Using wrong Blueprint for route decorator
- Thinking url_prefix belongs on app
- Believing functions can't return strings
Solution
Step 1: Understand version separation with Blueprints
Each API version should have its own Blueprint with distinct url_prefix.Step 2: Define '/status' route separately in each Blueprint
This allows each version to return its own message cleanly and maintainably.Final Answer:
Create separate Blueprints for v1, v2, v3 each with url_prefix and define '/status' route inside each Blueprint -> Option BQuick Check:
Separate Blueprints per version = clean versioning [OK]
- Trying to handle versions inside one route with if-else
- Defining routes only once without version separation
- Relying on extensions without understanding Blueprints
Ready to prove it?
Pass the test and this pattern moves unknown
