0
0
Flaskframework~30 mins

Why APIs matter in Flask - See It in Action

Choose your learning style9 modes available
Why APIs Matter with Flask
📖 Scenario: You are building a simple web service that shares information about books. This service will let other programs ask for book details easily.
🎯 Goal: Create a basic Flask API that returns book information as JSON data when requested.
📋 What You'll Learn
Create a dictionary called books with three books and their authors
Create a variable called api_prefix set to "/api"
Create a Flask route at "/api/books" that returns the books dictionary as JSON
Run the Flask app with debug mode enabled
💡 Why This Matters
🌍 Real World
APIs let different programs talk to each other easily. For example, a mobile app can ask your Flask API for book info to show to users.
💼 Career
Understanding how to build and run APIs with Flask is a key skill for backend web developers and software engineers.
Progress0 / 4 steps
1
Create the book data dictionary
Create a dictionary called books with these exact entries: "1984": "George Orwell", "To Kill a Mockingbird": "Harper Lee", and "The Great Gatsby": "F. Scott Fitzgerald".
Flask
Need a hint?

Use curly braces {} to create a dictionary with keys as book titles and values as authors.

2
Add the API prefix variable
Create a variable called api_prefix and set it to the string "/api".
Flask
Need a hint?

Just assign the string "/api" to the variable api_prefix.

3
Create the Flask route to return books
Import Flask and jsonify from flask. Create a Flask app called app. Then create a route using @app.route(f"{api_prefix}/books") that returns the books dictionary as JSON using jsonify(books).
Flask
Need a hint?

Use @app.route with the path built from api_prefix. Return the dictionary wrapped in jsonify().

4
Run the Flask app with debug mode
Add the code to run the Flask app with debug=True inside the if __name__ == '__main__': block.
Flask
Need a hint?

Use the standard Flask pattern to run the app only when the script is executed directly.