Notes API in Flask

EasyFlask · REST API

Build a small notes service in Flask. Keep the Flask object named app; notes live in memory (a dict is enough).

POST /notes with a JSON body {"text": "..."} creates a note and returns status 201 with {"id": 1, "text": "..."}. Ids start at 1 and go up by one for each note created.

If the body isn't JSON, or text is missing, isn't a string, or is empty after trimming spaces, return status 400 with {"error": "text is required"}. A rejected request doesn't use up an id.

GET /notes/<id> returns status 200 with the note, or 404 with {"error": "not found"}.

The checker doesn't start a real server: it sends requests through Flask's test client, the same way you'd test an API in a real project. Each test starts from a fresh app.

Examples

Before you start: read the basics
Show hint

request.get_json(silent=True) returns None instead of raising an error when the body isn't JSON. A view can return a pair: return jsonify(error="not found"), 404.

Your solution

Ctrl/⌘ + Enter runs the examples, Ctrl/⌘ + Shift + Enter submits
Loading Python…