This is the first step towards becoming a backend developer.

A Python project – A REST API in Flask

Lesson 12 of 15 · Python projects
Who this project is for:
an advanced project for students who have finished the basic course and the Python for the web lessons – usually secondary school. For grades 5–8, projects with graphics and the micro:bit are a better choice.

In this project we will make a simple REST API using Python and the Flask framework.

An API makes communication between applications possible through HTTP requests:

  • GET — reading data
  • POST — adding new data
  • PUT — changing data
  • DELETE — deleting data
In this project we will use:
  • the Flask framework
  • the JSON format
  • working with routes
  • the basics of backend programming

What is a REST API?

A REST API is a way of communicating between a client and a server.

  • the frontend (a web or mobile application) sends a request
  • the backend (a Python server) returns data
A REST API is the basis of modern web, mobile and IoT applications.

Installing Flask


pip install flask

An example REST API application


from flask import Flask, jsonify, request

app = Flask(__name__)

# list of data
items = [
    {"id": 1, "name": "Laptop"},
    {"id": 2, "name": "Phone"}
]

# helper function for the ID
def next_id():
    return max(item["id"] for item in items) + 1 if items else 1

# GET - all the data
@app.route('/items', methods=['GET'])
def get_items():
    return jsonify(items)

# GET - one item
@app.route('/items/<int:item_id>', methods=['GET'])
def get_item(item_id):
    for item in items:
        if item["id"] == item_id:
            return jsonify(item)
    return jsonify({"error": "Not found"}), 404

# POST - adding
@app.route('/items', methods=['POST'])
def add_item():
    data = request.json
    new_item = {
        "id": next_id(),
        "name": data.get("name", "No name")
    }
    items.append(new_item)
    return jsonify(new_item), 201

# PUT - changing
@app.route('/items/<int:item_id>', methods=['PUT'])
def update_item(item_id):
    data = request.json
    for item in items:
        if item["id"] == item_id:
            item["name"] = data.get("name", item["name"])
            return jsonify(item)
    return jsonify({"error": "Not found"}), 404

# DELETE - deleting
@app.route('/items/<int:item_id>', methods=['DELETE'])
def delete_item(item_id):
    global items
    items = [item for item in items if item["id"] != item_id]
    return jsonify({"message": "Deleted"})

if __name__ == '__main__':
    app.run(debug=True)

How can you test the API?

You can test the API in several ways:

  • a browser (GET requests)
  • Postman or Insomnia
  • Python (the requests library)
An example URL:

http://127.0.0.1:5000/items

How does this code work?

  • Flask() creates the application
  • @app.route defines the routes (URLs)
  • jsonify() returns a JSON response
  • request.json receives data
  • the methods (GET, POST...) decide the action
Each route represents one feature of the API.

Common mistakes

  • ❌ A wrong URL (e.g. /item instead of /items)
  • ❌ A forgotten JSON body in POST/PUT
  • ❌ A wrong Content-Type (it should be application/json)
  • ❌ The ID you are trying to change does not exist

Small improvements (the next step)

  • add fields: price, description
  • data validation
  • filtering and searching
  • pagination (many results)

What next?

An advanced REST API project

This is the basic version of the API (without a database).

In the advanced project you will learn:

  • working with a database (SQLite)
  • organising code (modules)
  • user authentication
  • a real backend system

➡ The advanced REST API project →


Mini challenges

  • Add a price field
  • Add searching by name
  • Add an endpoint for the number of elements

Where is a REST API used?

  • web applications
  • mobile applications
  • frontend (React, Vue)
  • IoT systems

Next project

How to make the project your own:
1) first run the given code without changes, 2) change one thing and predict what will happen, 3) add at least one feature of your own, 4) write down in a notebook what you changed and why. A work log like that is the basis of a creative programming paper.