Complete the code to import the Flask class.
from flask import [1]
The Flask class is imported from the flask module to create the app.
Complete the code to get the API key from request headers.
api_key = request.headers.get('[1]')
The API key is usually sent in a custom header like 'X-API-KEY'.
Fix the error in the code to check if the API key matches the expected key.
if api_key != [1]: return {'error': 'Unauthorized'}, 401
The expected API key should be a string literal in quotes.
Fill both blanks to create a Flask route that requires API key authentication.
@app.route('/data', methods=[[1]]) def get_data(): api_key = request.headers.get('X-API-KEY') if api_key != 'mysecretkey': return [2], 401 return {'data': 'Here is your data'}
The route should accept GET requests and return an error dictionary with 401 if unauthorized.
Fill all three blanks to complete the Flask app with API key check and run the server.
from flask import Flask, request app = Flask(__name__) @app.route('/info', methods=[[1]]) def info(): key = request.headers.get('X-API-KEY') if key != [2]: return [3], 401 return {'info': 'Secret info'} if __name__ == '__main__': app.run(debug=True)
The route uses GET method, checks the API key string 'supersecret', and returns an error dictionary with 401 if unauthorized.
