An advanced Python REST API (Flask RESTful)

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.

Lesson 14 of 15 · Python projects

This is a real backend project — the next step towards professional programming.

In this project we make an advanced REST API using Flask and Flask-RESTful.

Unlike the previous lesson, here we introduce:

  • working with a database (SQLite)
  • organising the code
  • working with IDs
  • realistic API design
This is the first step towards the real backend systems used in industry.

Tools you need

  • Python 3
  • Flask
  • Flask-RESTful
  • SQLite (a built-in database)
  • Postman or a browser

Installation


pip install flask
pip install flask-restful

Project structure


project/
│
├── app.py
├── database.db
In bigger projects you will use several files (models, routes...), but here we keep everything simple.

An advanced REST API with a database


from flask import Flask, request
from flask_restful import Resource, Api
import sqlite3

app = Flask(__name__)
api = Api(app)

def get_db():
    return sqlite3.connect("database.db")

# Creating the table (run once)
def create_table():
    conn = get_db()
    cursor = conn.cursor()
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS books (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            title TEXT,
            author TEXT
        )
    """)
    conn.commit()
    conn.close()

create_table()

class BookList(Resource):

    def get(self):
        conn = get_db()
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM books")
        rows = cursor.fetchall()
        conn.close()

        books = []
        for row in rows:
            books.append({
                "id": row[0],
                "title": row[1],
                "author": row[2]
            })

        return books

    def post(self):
        data = request.get_json()

        if not data.get("title"):
            return {"error": "Title is required"}, 400

        conn = get_db()
        cursor = conn.cursor()
        cursor.execute("INSERT INTO books (title, author) VALUES (?, ?)",
                       (data["title"], data.get("author", "")))
        conn.commit()
        conn.close()

        return {"message": "Book added"}, 201


class Book(Resource):

    def get(self, book_id):
        conn = get_db()
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM books WHERE id=?", (book_id,))
        row = cursor.fetchone()
        conn.close()

        if row:
            return {
                "id": row[0],
                "title": row[1],
                "author": row[2]
            }

        return {"error": "Not found"}, 404

    def delete(self, book_id):
        conn = get_db()
        cursor = conn.cursor()
        cursor.execute("DELETE FROM books WHERE id=?", (book_id,))
        conn.commit()
        conn.close()

        return {"message": "Deleted"}

api.add_resource(BookList, '/books')
api.add_resource(Book, '/books/<int:book_id>')

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

How does this API work?

  • /books (GET) → returns all the books
  • /books (POST) → adds a new book
  • /books/<id> (GET) → returns one book
  • /books/<id> (DELETE) → deletes a book

The key difference from the basic API

  • the data is stored in a database (not in a list)
  • the data is persistent
  • we use SQL queries
  • we work with IDs

Testing the API


# GET
http://127.0.0.1:5000/books

# POST
{
  "title": "Python",
  "author": "Mark"
}

Common mistakes

  • ❌ the database has not been created
  • ❌ a wrong JSON format
  • ❌ the title is missing
  • ❌ a wrong URL

How can you improve the project further?

  • add PUT (update)
  • add authentication (login)
  • split the code into several files
  • use an ORM (SQLAlchemy)
  • deploy to a server
These are the things real backend developers do.

Mini challenges

  • Add an update (PUT) method
  • Add a "year" field
  • Add searching by title

Related lessons and projects


Frequently asked questions

What is the difference between this API and the previous one?

Here we use a database and build a more realistic backend system.

Is Flask good enough for serious projects?

Yes — Flask is used in many real applications.

What should I learn next?

Authentication, databases, deployment and connecting a frontend.