APIs let different programs talk to each other easily. They help apps share data and work together without confusion.
Why APIs matter in Flask
from flask import Flask, jsonify, request app = Flask(__name__) @app.route('/api/data', methods=['GET']) def get_data(): data = {'message': 'Hello from API!'} return jsonify(data) if __name__ == '__main__': app.run(debug=True)
Use @app.route to define API endpoints.
jsonify() sends data in a format apps understand (JSON).
from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/hello') def hello(): return jsonify({'greeting': 'Hi there!'})
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/api/echo', methods=['POST']) def echo(): data = request.json return jsonify({'you_sent': data})
This Flask app has two API endpoints. One sends a welcome message when you visit /api/welcome. The other accepts a name in a POST request to /api/greet and replies with a personalized greeting.
from flask import Flask, jsonify, request app = Flask(__name__) # Simple API endpoint that returns a welcome message @app.route('/api/welcome') def welcome(): return jsonify({'message': 'Welcome to our API!'}) # API endpoint that accepts a name and returns a greeting @app.route('/api/greet', methods=['POST']) def greet(): data = request.json name = data.get('name', 'Guest') return jsonify({'greeting': f'Hello, {name}!'}) if __name__ == '__main__': app.run(debug=True)
APIs usually send data in JSON format because it's easy to read and use.
Use HTTP methods like GET to get data and POST to send data.
Testing APIs can be done with tools like Postman or browser for GET requests.
APIs help apps share data and features easily.
Flask makes it simple to create APIs with routes and JSON responses.
APIs use standard methods like GET and POST to communicate.
Practice
Solution
Step 1: Understand the role of APIs in app communication
APIs let different software talk to each other by sharing data and features.Step 2: Relate this to Flask's use of APIs
Flask helps build APIs that send and receive data, enabling app interaction.Final Answer:
They allow different apps to share data and features easily. -> Option BQuick Check:
APIs enable app communication = C [OK]
- Thinking APIs speed up server processing
- Believing APIs create user interfaces automatically
- Assuming APIs replace databases
Solution
Step 1: Identify correct Flask route decorator syntax
Flask routes require the '@app.route' decorator before the function.Step 2: Check the function returns JSON data properly
The function should return a dictionary (Flask converts it to JSON), not print it.Final Answer:
@app.route('/data')\ndef get_data():\n return {'key': 'value'} -> Option AQuick Check:
Correct decorator and return = B [OK]
- Missing '@' before app.route
- Using print instead of return
- Not returning data from the function
@app.route('/hello')
def hello():
return {'message': 'Hi there!'}Solution
Step 1: Understand Flask's automatic JSON conversion
Returning a dictionary from a route makes Flask send JSON response automatically.Step 2: Check the returned dictionary content
The dictionary has key 'message' with value 'Hi there!', so JSON will reflect that.Final Answer:
A JSON response: {"message": "Hi there!"} -> Option CQuick Check:
Return dict = JSON response = A [OK]
- Expecting plain text instead of JSON
- Thinking Flask returns HTML automatically
- Assuming a server error from dict return
@app.route('/data', methods=['POST'])
def data():
data = request.get_json
return {'received': data}Solution
Step 1: Check how request.get_json is used
request.get_json is a method and must be called with parentheses to get JSON data.Step 2: Understand the impact of missing parentheses
Without (), data is a method object, not the JSON content, causing wrong output.Final Answer:
request.get_json() is missing parentheses, so data is a method, not JSON. -> Option DQuick Check:
Call get_json() method properly = D [OK]
- Forgetting parentheses on get_json
- Using wrong HTTP method for route
- Using print instead of return in route
Solution
Step 1: Check the HTTP method and route setup
The API must accept POST requests, so methods=['POST'] is correct.Step 2: Verify JSON data extraction and greeting creation
Using request.get_json() gets JSON data; then safely get 'name' with default 'Guest'.Step 3: Confirm the returned JSON greeting format
Return a dictionary with the greeting message using an f-string for the name.Final Answer:
Correct POST route with JSON input and greeting response. -> Option AQuick Check:
POST + get_json() + return greeting = A [OK]
- Using GET instead of POST for JSON input
- Trying to get JSON data from request.args or form
- Not handling missing 'name' key safely
Ready to prove it?
Pass the test and this pattern moves unknown
