Flask API Fundamentals · Structure and Error Handling

Error Responses That Help

Return clear status codes and JSON error bodies.

Error Responses That Help

Good APIs fail predictably. Return a useful status code and a JSON body that a client can act on.

from flask import Flask, jsonify

app = Flask(__name__)


@app.errorhandler(404)
def not_found(_error):
    return jsonify(error='resource not found'), 404

Prefer consistent shapes like {"error": "..."} or {"detail": "..."} across your API so clients do not need special-case parsing for each endpoint.

Open in the app to mark lessons complete, take notes and track your progress.