Python · Module 14 · Lesson 31 min read

Capstone: Flask + Supabase API

Project goal

A small full-stack backend: a Flask API that reads and writes data in Supabase (PostgreSQL), the same kind of database this app uses.

Skills you'll combine

Flask routes, the Supabase client, environment variables, input validation, error handling, and project structure.

Architecture

scss
client (app / browser)  →  Flask API  →  Supabase (Postgres)

Steps

  1. Create a Supabase project and a table, e.g. notes(id, title, body, created_at).
  2. Put SUPABASE_URL and SUPABASE_KEY in a .env file and load them with python-dotenv.
  3. Add routes: GET /notes, GET /notes/<id>, POST /notes.
  4. Validate input and return proper status codes (201, 400, 404).
  5. Write pytest tests for the routes using Flask's test client.
  6. Deploy (Render, Railway, Fly.io) with gunicorn.

Security checklist 🔐

  • Never commit .env.
  • Turn on Row Level Security in Supabase.
  • Only use the service_role key on the server, never in a mobile app.

💻 Needs your own Supabase project, so run it on a computer (pip install flask supabase python-dotenv).

Example

Python
import os

from dotenv import load_dotenv
from flask import Flask, request
from supabase import create_client

load_dotenv()
db = create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_KEY"])
app = Flask(__name__)

@app.get("/notes")
def list_notes():
    rows = db.table("notes").select("*").order("created_at", desc=True).execute()
    return rows.data

@app.get("/notes/<int:note_id>")
def get_note(note_id):
    rows = db.table("notes").select("*").eq("id", note_id).limit(1).execute()
    if not rows.data:
        return {"error": "Note not found"}, 404
    return rows.data[0]

@app.post("/notes")
def create_note():
    data = request.get_json(silent=True) or {}
    title = str(data.get("title", "")).strip()
    if not title:
        return {"error": "'title' is required"}, 400
    row = db.table("notes").insert(
        {"title": title, "body": data.get("body", "")}
    ).execute()
    return row.data[0], 201

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