# Add a backend to a Python app

> Give a Python service, script or notebook a Postgres database, auth, storage and realtime with supabase-py, and generate TypedDict shapes with one command.

Source: https://scribase.com/frameworks/python

A Python app uses supabase-py, which works with Scribase unchanged. Create the project in the console, copy two values, and generate TypedDict shapes for your tables.

## Environment file: .env

- SUPABASE_URL
- SUPABASE_ANON_KEY

## Create the backend

Sign up in the console. Confirm your email; your first project is then created automatically and the "Your backend is live" screen shows the Project URL and the publishable key with one-click copy.

## Install the package

The package is named supabase on PyPI (supabase-py).

```bash
pip install supabase
```

## Create your first table

Open your project in the console, go to Editor, then SQL, and run this. Or ask your coding agent: with the Scribase MCP server connected it applies the same SQL as a migration. Row-level security is on, and the policy names a column, so the public key reads only the rows marked is_public.

```sql
create table public.todos (
  id bigint generated always as identity primary key,
  title text not null,
  done boolean not null default false,
  is_public boolean not null default false,
  created_at timestamptz not null default now()
);

alter table public.todos enable row level security;

create policy "todos are readable" on public.todos
  for select to anon, authenticated using (is_public);

insert into public.todos (title, is_public) values ('Ship the backend', true), ('Tell a friend', true);
```

## Generate TypedDict shapes

One Row, Insert and Update shape per table, from the live schema. Re-run after schema changes.

```bash
scribase gen types <org> <project> production --lang python --out database_types.py
```

## Query with the shapes

The shapes check your code in the editor; the values are plain dicts.

```python
import os
from supabase import create_client
from database_types import TodosRow

client = create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_ANON_KEY"])
todos: list[TodosRow] = client.table("todos").select("*").execute().data
```

## Agent prompt

```
Add a Scribase backend to this Python project.
1. `pip install supabase` and read SUPABASE_URL and SUPABASE_ANON_KEY from the environment (.env, never committed).
2. Create a `todos` table with row-level security on and a read policy using the Scribase MCP server or a SQL migration.
3. Run `scribase gen types <org> <project> production --lang python --out database_types.py` and type the query results with TodosRow.
```

## FAQ

**Can a server use the secret key?**

Yes, on a server only: it bypasses row-level security, so it never goes into a browser, an app or a notebook you share.