REST API (Flask)
A REST API in Flask exposes resources over HTTP using JSON and standard verbs: GET to read, POST to create, PUT/PATCH to update, DELETE to remove. Routes return jsonify() data and appropriate status codes instead of HTML.
Flask
REST is a convention for building APIs around resources identified by URLs. /api/posts is the collection, /api/posts/5 is one item, and the HTTP method expresses the action. Clients send and receive JSON rather than full web pages.
In Flask you build one by adding routes that read request.json, do the work, and return jsonify(result), often with an explicit status code like 201 for created or 404 for not found. For larger APIs, Blueprints group endpoints and extensions like Flask-RESTful or Marshmallow add structure and serialization.
Because APIs are usually stateless and consumed by other programs, they typically use token authentication (such as JWT) rather than cookie-based sessions, and they enable CORS so browsers from other origins can call them.
Syntax
@app.route("/api/items", methods=["GET", "POST"])Examples
Python
from flask import jsonify, request
items = []
@app.route("/api/items", methods=["GET", "POST"])
def items_api():
if request.method == "POST":
items.append(request.json)
return jsonify(request.json), 201
return jsonify(items)