0
0
Flaskframework~30 mins

API versioning with blueprints in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
API Versioning with Blueprints in Flask
📖 Scenario: You are building a simple web API for a bookstore. You want to support two versions of the API so clients can choose which version to use. You will use Flask blueprints to organize the code for each API version.
🎯 Goal: Create two Flask blueprints named v1 and v2 for API version 1 and version 2. Each blueprint will have a route /books that returns a JSON list of books. Then register both blueprints in the Flask app with URL prefixes /api/v1 and /api/v2.
📋 What You'll Learn
Create a Flask app instance named app
Create a blueprint named v1 with a route /books returning a JSON list of book titles for version 1
Create a blueprint named v2 with a route /books returning a JSON list of book titles for version 2
Register both blueprints on the Flask app with URL prefixes /api/v1 and /api/v2
💡 Why This Matters
🌍 Real World
API versioning is important when you want to improve or change your API without breaking existing clients. Blueprints help organize code for each version cleanly.
💼 Career
Many web developers and backend engineers use Flask blueprints to build scalable APIs that support multiple versions for different clients.
Progress0 / 4 steps
1
Create the Flask app and version 1 blueprint
Create a Flask app instance called app. Then create a blueprint called v1 with the name 'v1' and import name __name__. Add a route /books to v1 that returns a JSON list ["The Great Gatsby", "1984"].
Flask
Need a hint?

Use Flask(__name__) to create the app. Use Blueprint('v1', __name__) to create the blueprint. Use @v1.route('/books') to add the route. Return the JSON list with jsonify.

2
Create the version 2 blueprint with updated book list
Create a blueprint called v2 with the name 'v2' and import name __name__. Add a route /books to v2 that returns a JSON list ["Brave New World", "Fahrenheit 451"].
Flask
Need a hint?

Create the v2 blueprint like v1. Add the /books route returning the new list of books using jsonify.

3
Register blueprints with URL prefixes
Register the blueprint v1 on the Flask app app with the URL prefix /api/v1. Register the blueprint v2 on app with the URL prefix /api/v2.
Flask
Need a hint?

Use app.register_blueprint() with the url_prefix argument to register each blueprint.

4
Add app run block for local testing
Add the standard Flask app run block at the bottom: if __name__ == '__main__': then run app.run(debug=True).
Flask
Need a hint?

Add the standard Flask run block to start the server locally with debugging enabled.