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
client (app / browser) → Flask API → Supabase (Postgres)
Steps
- Create a Supabase project and a table, e.g.
notes(id, title, body, created_at). - Put
SUPABASE_URLandSUPABASE_KEYin a.envfile and load them withpython-dotenv. - Add routes:
GET /notes,GET /notes/<id>,POST /notes. - Validate input and return proper status codes (
201,400,404). - Write pytest tests for the routes using Flask's test client.
- Deploy (Render, Railway, Fly.io) with gunicorn.
Security checklist 🔐
- Never commit
.env. - Turn on Row Level Security in Supabase.
- Only use the
service_rolekey 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
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)