# 1. Prerequisites

## 1. Create a Supabase Instance

Refer to [Creating a Supabase Instance](guide.md).

## 2. Obtain API Credentials

1. Select the Supabase instance and click "Details."
2. Enable external network access and add the client IP address to the allowlist.
3. Click "Go to View" to open the Supabase login page.
4. Log in using the default username Supabase and the corresponding password.
5. Click Connect. In the Connect to your project dialog, select App Frameworks to retrieve the Supabase URL and Supabase Key.

## 3. Prepare the Data Table

```sql
create table todos (
  id bigint generated by default as identity primary key,
  task text not null,
  is_complete boolean default false,
  inserted_at timestamp with time zone default timezone('utc'::text, now()) not null
);
```

# 2. Install the SDK

```bash
pip install supabase
```

# 3. Code Implementation

```python
import os
from supabase import create_client, Client
from dotenv import load_dotenv

load_dotenv()

url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_KEY")
supabase: Client = create_client(url, key)

def run_demo():
    # --- Insert data ---
    print("Inserting data...")
    data, count = supabase.table("todos").insert({"task": "Learn the Supabase Python SDK"}).execute()

    # --- Query data ---
    print("Fetching all tasks:")
    response = supabase.table("todos").select("*").execute()
    for row in response.data:
        print(f"ID: {row['id']} | Task: {row['task']} | Completed: {row['is_complete']}")

    # --- Update data ---
    if response.data:
        todo_id = response.data[0]['id']
        supabase.table("todos").update({"is_complete": True}).eq("id", todo_id).execute()
        print(f"\nMarked ID {todo_id} as complete.")

    # --- Query data again ---
    print("Fetching all tasks:")
    response = supabase.table("todos").select("*").execute()
    for row in response.data:
        print(f"ID: {row['id']} | Task: {row['task']} | Completed: {row['is_complete']}")

if __name__ == "__main__":
    run_demo()
```

For more information, refer to the [Supabase official documentation](https://supabase.com/docs).
