Flask-RESTful: a CRUD API with Resource classes

Flask-RESTful is a Flask extension where each URL maps to a Resource class, and the class’s get, post, put and delete methods handle the matching HTTP verbs. Return a dict (and optionally a status code) and it is serialized to JSON for you.
Installation
Section titled “Installation”python3 -m venv envsource env/bin/activatepip install Flask Flask-RESTfulThe API
Section titled “The API”The API manages an in-memory list of tasks. It needs two resources, because the URLs differ:
/tasks: list all tasks (GET) and create one (POST). The client doesn’t know the ID yet./tasks/<id>: read (GET), update (PUT) and delete (DELETE) one task.
Put this in app.py:
from itertools import count
from flask import Flask, requestfrom flask_restful import Api, Resource
app = Flask(__name__)api = Api(app)
tasks = {}next_id = count(1)
def read_title(): data = request.get_json(silent=True) or {} title = data.get("title") if not isinstance(title, str) or not title.strip(): return None return title.strip()
class TaskList(Resource): def get(self): return list(tasks.values())
def post(self): title = read_title() if title is None: return {"error": "title is required"}, 400 task = {"id": next(next_id), "title": title} tasks[task["id"]] = task return task, 201
class Task(Resource): def get(self, task_id): task = tasks.get(task_id) if task is None: return {"error": "Task not found"}, 404 return task
def put(self, task_id): task = tasks.get(task_id) if task is None: return {"error": "Task not found"}, 404 title = read_title() if title is None: return {"error": "title is required"}, 400 task["title"] = title return task
def delete(self, task_id): if tasks.pop(task_id, None) is None: return {"error": "Task not found"}, 404 return "", 204
api.add_resource(TaskList, "/tasks")api.add_resource(Task, "/tasks/<int:task_id>")
if __name__ == "__main__": app.run(debug=True)A few details worth noting:
- Flask passes URL variables to the method as keyword arguments, so every method on
Taskmust accepttask_id. The original version of this post registeredpost(self)on/tasks/<int:task_id>, which fails with aTypeErrorbecause Flask passestask_idto it. <int:task_id>converts the ID to anintand returns 404 for non-numeric IDs before your code runs.- IDs come from a counter rather than
len(tasks) + 1, which would hand out a duplicate ID after a delete. get_json(silent=True)returnsNoneinstead of raising on a missing or invalid JSON body, so the resource can return its own 400.- The data lives in a module-level dict, so it’s lost on restart and isn’t shared between worker processes. Swap in a database for anything real.
Testing with curl
Section titled “Testing with curl”Start the server with python app.py, then:
# Createcurl -X POST -H "Content-Type: application/json" -d '{"title": "First task"}' http://localhost:5000/tasks# {"id": 1, "title": "First task"}
# Listcurl http://localhost:5000/tasks
# Read onecurl http://localhost:5000/tasks/1
# Updatecurl -X PUT -H "Content-Type: application/json" -d '{"title": "Updated task"}' http://localhost:5000/tasks/1
# Delete (204, empty body)curl -i -X DELETE http://localhost:5000/tasks/1Next steps
Section titled “Next steps”- Use
flask_restful.fieldsandmarshal_withto control which fields are returned. - For input validation, the Flask-RESTful docs suggest a library such as marshmallow instead of the deprecated
reqparse. app.run(debug=True)is the development server. Run a WSGI server such as Gunicorn in production.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)