> ## Documentation Index
> Fetch the complete documentation index at: https://langchat.neurobrains.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Supabase Provider

> Supabase PostgreSQL integration for chat history and metrics storage.

## Usage

```python theme={null}
from langchat.providers import Supabase

db = Supabase()                                              # reads from env
db = Supabase(url="https://xxxx.supabase.co", key="eyJ...")  # explicit
```

***

## Parameters

<ParamField path="url" type="str | None" default="None">
  Supabase project URL. Falls back to `SUPABASE_URL`.
</ParamField>

<ParamField path="key" type="str | None" default="None">
  Supabase API key. Falls back to `SUPABASE_KEY` or `SUPABASE_SERVICE_ROLE_KEY`.
</ParamField>

***

## Environment variables

```bash theme={null}
SUPABASE_URL=https://xxxx.supabase.co
SUPABASE_KEY=eyJhbGc...
# or
SUPABASE_SERVICE_ROLE_KEY=eyJhbGc...   # for server-side use (bypasses RLS)
```

<Note>
  Use `SUPABASE_SERVICE_ROLE_KEY` for server deployments where you need to bypass Row Level Security. Use `SUPABASE_KEY` (anon key) for client-side or restricted access.
</Note>

***

## Tables created automatically

LangChat creates these tables on first run:

### `chat_history`

| Column       | Type          | Description                  |
| ------------ | ------------- | ---------------------------- |
| `id`         | `bigint`      | Primary key (auto-increment) |
| `user_id`    | `text`        | User identifier              |
| `platform`   | `text`        | Platform namespace           |
| `message`    | `text`        | User message                 |
| `response`   | `text`        | AI response                  |
| `created_at` | `timestamptz` | UTC timestamp                |

### `request_metrics`

| Column          | Type      | Description                   |                          |
| --------------- | --------- | ----------------------------- | ------------------------ |
| `id`            | `bigint`  | Primary key (auto-increment)  |                          |
| `user_id`       | `text`    | User identifier               |                          |
| `request_time`  | `text`    | ISO 8601 UTC timestamp        |                          |
| `response_time` | `float`   | Latency in seconds            |                          |
| `success`       | `boolean` | Whether the request succeeded |                          |
| `error_message` | \`text    | null\`                        | Error details on failure |

***

## Querying your data

You can use the Supabase dashboard or the Python client directly to query these tables:

```python theme={null}
from supabase import create_client

client = create_client(url, key)

# Get all conversations for a user
history = (
    client
    .table("chat_history")
    .select("*")
    .eq("user_id", "alice")
    .order("created_at", desc=True)
    .execute()
)

# Get average response time
metrics = (
    client
    .table("request_metrics")
    .select("response_time")
    .eq("success", True)
    .execute()
)
```

***

## Row Level Security (RLS)

If you enable RLS on Supabase tables, you must either:

1. Use the `service_role` key (bypasses RLS entirely)
2. Add appropriate RLS policies for the `langchat` operations

For most server-side deployments, using `SUPABASE_SERVICE_ROLE_KEY` is the simplest approach.
