Bird
Raised Fist0
Flaskframework~20 mins

API versioning with blueprints in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
API Versioning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Flask route versioning setup?

Given two blueprints registered with different URL prefixes for API versions, what URL will respond with 'Hello from v1'?

Flask
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 v1'

v2 = Blueprint('v2', __name__, url_prefix='/api/v2')

@v2.route('/hello')
def hello_v2():
    return 'Hello from v2'

app.register_blueprint(v1)
app.register_blueprint(v2)
AGET request to /hello returns 'Hello from v1'
BGET request to /api/v1/hello returns 'Hello from v1'
CGET request to /api/hello returns 'Hello from v1'
DGET request to /api/v2/hello returns 'Hello from v1'
Attempts:
2 left
💡 Hint

Think about how the url_prefix affects the route URL.

📝 Syntax
intermediate
2:00remaining
Which option correctly registers two versioned blueprints in Flask?

Choose the code snippet that correctly registers two blueprints v1 and v2 with URL prefixes /api/v1 and /api/v2 respectively.

A
app.register_blueprint(v1)
app.register_blueprint(v2)

v1.url_prefix = '/api/v1'
v2.url_prefix = '/api/v2'
B
app.register_blueprint(v1, url_prefix='/api/v1')
app.register_blueprint(v2, url_prefix='/api/v2')
C
v1 = Blueprint('v1', __name__)
v2 = Blueprint('v2', __name__)
app.register_blueprint(v1, url_prefix='/api/v1')
app.register_blueprint(v2, url_prefix='/api/v2')
D
v1 = Blueprint('v1', __name__, url_prefix='/api/v1')
v2 = Blueprint('v2', __name__, url_prefix='/api/v2')
app.register_blueprint(v1)
app.register_blueprint(v2)
Attempts:
2 left
💡 Hint

Remember where the url_prefix should be set: at blueprint creation or registration.

❓ state_output
advanced
2:00remaining
What is the response when accessing an unregistered API version route?

Given blueprints registered for /api/v1 and /api/v2, what happens when a client requests /api/v3/hello?

Flask
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 v1'

v2 = Blueprint('v2', __name__, url_prefix='/api/v2')
@v2.route('/hello')
def hello_v2():
    return 'Hello from v2'

app.register_blueprint(v1)
app.register_blueprint(v2)
A404 Not Found error
B500 Internal Server Error
C'Hello from v1' response
D'Hello from v2' response
Attempts:
2 left
💡 Hint

Think about what happens if no route matches the requested URL.

🔧 Debug
advanced
2:00remaining
Why does this Flask app return 404 for /api/v1/hello despite blueprint registration?

Consider this code snippet. The route /hello is defined inside blueprint v1 with prefix /api/v1. But accessing /api/v1/hello returns 404. What is the likely cause?

Flask
from flask import Flask, Blueprint

app = Flask(__name__)

v1 = Blueprint('v1', __name__, url_prefix='/api/v1')

@app.route('/hello')
def hello():
    return 'Hello from v1'

app.register_blueprint(v1)
AThe route is registered on app, not on blueprint v1
BThe blueprint url_prefix is incorrect
CThe blueprint was not registered
DThe route function name conflicts with blueprint name
Attempts:
2 left
💡 Hint

Check where the route decorator is applied.

🧠 Conceptual
expert
3:00remaining
How does Flask blueprint versioning help in API maintenance?

Why is using blueprints with URL prefixes for API versioning a good practice in Flask applications?

AIt merges all API versions into a single route for simplicity
BIt automatically upgrades old API versions to the latest without code changes
CIt allows separating code for different API versions cleanly and enables running multiple versions simultaneously
DIt forces all clients to use the newest API version only
Attempts:
2 left
💡 Hint

Think about how versioning affects code organization and client support.

Practice

(1/5)
1. What is the main purpose of using Blueprints for API versioning in Flask?
easy
A. To store user session data securely
B. To speed up the Flask server automatically
C. To organize different API versions with separate URL prefixes
D. To create database models for each API version

Solution

  1. Step 1: Understand Blueprints role in Flask

    Blueprints allow grouping routes and related code logically.
  2. Step 2: Connect Blueprints to API versioning

    Using url_prefix in Blueprints lets you separate API versions by URL path.
  3. Final Answer:

    To organize different API versions with separate URL prefixes -> Option C
  4. Quick Check:

    Blueprints + url_prefix = versioned APIs [OK]
Hint: Blueprints + url_prefix separate API versions clearly [OK]
Common Mistakes:
  • Thinking Blueprints speed up the server
  • Confusing Blueprints with session management
  • Assuming Blueprints create database models
2. Which of the following is the correct way to create a Blueprint for API version 1 with prefix '/api/v1'?
easy
A. bp = Blueprint('api_v1', __name__, url_prefix='/api/v1')
B. bp = Blueprint('api_v1', url_prefix='/api/v1')
C. bp = Blueprint(__name__, 'api_v1', url_prefix='/api/v1')
D. bp = Blueprint('api_v1', __name__, prefix='/api/v1')

Solution

  1. Step 1: Recall Blueprint constructor parameters

    Blueprint(name, import_name, url_prefix=...)
  2. Step 2: Identify correct syntax

    bp = Blueprint('api_v1', __name__, url_prefix='/api/v1') uses correct order and url_prefix keyword.
  3. Final Answer:

    bp = Blueprint('api_v1', __name__, url_prefix='/api/v1') -> Option A
  4. Quick Check:

    Blueprint(name, __name__, url_prefix=...) correct [OK]
Hint: Blueprint needs name, __name__, and url_prefix keyword [OK]
Common Mistakes:
  • Omitting __name__ parameter
  • Using 'prefix' instead of 'url_prefix'
  • Swapping parameter order incorrectly
3. Given this Flask app code, what URL will return 'Hello from v2!'?
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)
medium
A. /api/v2/hello
B. /hello
C. /v2/hello
D. /api/v1/hello

Solution

  1. Step 1: Check Blueprint url_prefix for v2

    Blueprint 'v2' uses url_prefix='/api/v2'.
  2. Step 2: Check route path inside v2 Blueprint

    Route is '/hello', so full path is '/api/v2/hello'.
  3. Final Answer:

    /api/v2/hello -> Option A
  4. Quick Check:

    Blueprint prefix + route = full URL [OK]
Hint: Combine url_prefix and route path for full URL [OK]
Common Mistakes:
  • Ignoring url_prefix and using only route path
  • Confusing v1 and v2 prefixes
  • Assuming route is registered at root '/'
4. What is wrong with this Flask code for versioned API Blueprints?
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)
medium
A. Functions cannot return strings directly
B. Blueprints cannot have the same route path
C. url_prefix should be set on app, not Blueprint
D. The second route decorator uses bp_v1 instead of bp_v2

Solution

  1. Step 1: Identify route decorators and Blueprints

    The second route uses @bp_v1.route but should use @bp_v2.route.
  2. Step 2: Understand impact of wrong decorator

    This causes the second function to register on the first Blueprint, not the second.
  3. Final Answer:

    The second route decorator uses bp_v1 instead of bp_v2 -> Option D
  4. Quick Check:

    Route decorator must match Blueprint instance [OK]
Hint: Match route decorator to correct Blueprint instance [OK]
Common Mistakes:
  • Using wrong Blueprint for route decorator
  • Thinking url_prefix belongs on app
  • Believing functions can't return strings
5. You want to add a new API version 3 with a Blueprint and share a common route '/status' that returns different messages per version. Which approach correctly implements this in Flask?
hard
A. Define '/status' route only once on app without Blueprints and check version from headers
B. Create separate Blueprints for v1, v2, v3 each with url_prefix and define '/status' route inside each Blueprint
C. Create one Blueprint with url_prefix='/api' and use if-else inside '/status' route to check version
D. Use Flask extensions to automatically version APIs without Blueprints

Solution

  1. Step 1: Understand version separation with Blueprints

    Each API version should have its own Blueprint with distinct url_prefix.
  2. Step 2: Define '/status' route separately in each Blueprint

    This allows each version to return its own message cleanly and maintainably.
  3. Final Answer:

    Create separate Blueprints for v1, v2, v3 each with url_prefix and define '/status' route inside each Blueprint -> Option B
  4. Quick Check:

    Separate Blueprints per version = clean versioning [OK]
Hint: Use separate Blueprints per version for clean route separation [OK]
Common Mistakes:
  • Trying to handle versions inside one route with if-else
  • Defining routes only once without version separation
  • Relying on extensions without understanding Blueprints
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard