0
0
Flaskframework~30 mins

Flask route as API endpoint - Mini Project: Build & Apply

Choose your learning style9 modes available
Flask route as API endpoint
📖 Scenario: You are building a simple web service that provides information about fruits. This service will have an API endpoint that returns fruit data in JSON format.
🎯 Goal: Create a Flask application with a route /api/fruits that returns a JSON response containing a list of fruits and their colors.
📋 What You'll Learn
Create a dictionary called fruits with fruit names as keys and their colors as values
Create a Flask app instance named app
Define a route /api/fruits using the @app.route decorator
Return the fruits dictionary as a JSON response using jsonify
💡 Why This Matters
🌍 Real World
APIs like this let apps and websites get data from servers in a simple, standard way.
💼 Career
Knowing how to create API endpoints with Flask is useful for backend web development jobs.
Progress0 / 4 steps
1
Create the fruit data dictionary
Create a dictionary called fruits with these exact entries: 'apple': 'red', 'banana': 'yellow', and 'grape': 'purple'.
Flask
Need a hint?

Use curly braces {} to create a dictionary with keys and values.

2
Create the Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Need a hint?

Use app = Flask(__name__) to create the app instance.

3
Define the API route
Use the @app.route decorator with the path /api/fruits to define a function called get_fruits.
Flask
Need a hint?

Use @app.route('/api/fruits') above the function definition.

4
Return JSON response with fruit data
Import jsonify from flask and return jsonify(fruits) inside the get_fruits function.
Flask
Need a hint?

Use return jsonify(fruits) to send the dictionary as JSON.