# supabase/supabase-py architecture

> Architecture diagram of the GitHub repository supabase/supabase-py, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/supabase/supabase-py
- Repository: https://github.com/supabase/supabase-py

## Overview

##### Project Type and Purpose
- This is an open-source Python SDK (“supabase-py”) that provides a unified, high-level client for interacting with the Supabase backend (PostgREST, GoTrue auth, Realtime websockets, Storage, Edge Functions).
- It acts as a façade/adapter over lower-level HTTP and WebSocket client libraries (e.g., postgrest-py, auth-py, realtime-py, storage-py, functions-py) and exposes both synchronous and asynchronous APIs.

##### Key Architectural Components
a. Client Application (your Flask/Django/FastAPI/etc. code)
b. Initialization Layer
- create_client(factory function) → builds a Client instance
- reads SUPABASE_URL, SUPABASE_KEY (env or .env)
c. supabase-py Core Package
- supabase/client.py → central Client façade
- supabase/_sync/* → synchronous service clients (AuthClient, PostgrestClient)
- supabase/_async/* → asynchronous counterparts
- supabase/lib/client_options.py → option processing (timeouts, HTTP2, keep-alive, SSL verify)
- supabase/types.py → shared type definitions and data models
d. Underlying Service Wrappers
- postgrest-py → CRUD over RESTful PostgREST API
- auth-py → server-side authentication (GoTrue)
- realtime-py → WebSocket subscriptions/events
- storage-py → file upload/download to Supabase Storage buckets
- functions-py → invoke Edge Functions via HTTP
e. HTTP/WebSocket Transport
- requests (sync) or httpx (async) under the hood
- WebSocket libs for realtime
f. Supabase Backend (external)
- PostgREST API (database)
- GoTrue Auth Service
- Realtime WebSocket Gateway
- Storage Buckets (S3 compatible)
- Edge Functions API

##### Relationships and Data Flow
- The Client façade exposes methods: auth.sign_in(), table(...).select().execute(), storage.from_(…).upload(), functions.invoke(), realtime.subscribe().
- Calls are routed to the appropriate service client (sync vs async) based on how the Client was instantiated.
- Service clients issue HTTP requests or maintain WebSocket connections to Supabase endpoints.
- Data (JSON) flows back through client-side models to the application.

##### Architectural Patterns and Principles
- Façade/Adapter Pattern: abstracts multiple backends under a unified interface.
- Layered Architecture:
- API layer (create_client + Client facade)
- Service layer (auth, postgrest, realtime, storage, functions modules)
- Transport layer (HTTP/WebSocket)
- Separation of Sync vs Async: parallel module trees under supabase/_sync and supabase/_async.
- Configuration as Code: client_options handles timeouts, HTTP2, keep-alive, SSL verifying.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Application Layer"
        App["Client Application"]:::app
    end

    App -->|"env vars: SUPABASE_URL, SUPABASE_KEY"| Init["create_client()"]:::sdk
    click Init "https://github.com/supabase/supabase-py/blob/main/supabase/client.py"

    subgraph "SDK Core"
        subgraph "Initialization & Config"
            Options["client_options"]:::sdk
            Types["types.py"]:::sdk
            Version["version.py"]:::sdk
            InitMod["__init__.py"]:::sdk
        end
        subgraph "Client Facade"
            SyncClient["Sync ClientFacade"]:::sdk
            AsyncClient["Async ClientFacade"]:::sdk
        end
    end

    click Options "https://github.com/supabase/supabase-py/blob/main/supabase/lib/client_options.py"
    click Types "https://github.com/supabase/supabase-py/blob/main/supabase/types.py"
    click Version "https://github.com/supabase/supabase-py/blob/main/supabase/version.py"
    click InitMod "https://github.com/supabase/supabase-py/blob/main/supabase/__init__.py"
    click SyncClient "https://github.com/supabase/supabase-py/blob/main/supabase/_sync/client.py"
    click AsyncClient "https://github.com/supabase/supabase-py/blob/main/supabase/_async/client.py"

    Init --> SyncClient
    Init --> AsyncClient
    Options -- config --> SyncClient
    Options -- config --> AsyncClient

    SyncClient -->|"auth.sign_in()"| SyncAuth["Sync AuthClient"]:::service
    SyncClient -->|"table().select()"| SyncPost["Sync PostgrestClient"]:::service
    SyncClient -->|"storage.upload()"| SyncStorage["Sync StorageClient"]:::service
    SyncClient -->|"functions.invoke()"| SyncFunc["Sync FunctionsClient"]:::service
    SyncClient -->|"realtime.subscribe()"| SyncRealtime["Sync RealtimeClient"]:::service

    AsyncClient -->|"auth.sign_in()"| AsyncAuth["Async AuthClient"]:::service
    AsyncClient -->|"table().select()"| AsyncPost["Async PostgrestClient"]:::service
    AsyncClient -->|"storage.upload()"| AsyncStorage["Async StorageClient"]:::service
    AsyncClient -->|"functions.invoke()"| AsyncFunc["Async FunctionsClient"]:::service
    AsyncClient -->|"realtime.subscribe()"| AsyncRealtime["Async RealtimeClient"]:::service

    click SyncAuth "https://github.com/supabase/supabase-py/blob/main/supabase/_sync/auth_client.py"
    click AsyncAuth "https://github.com/supabase/supabase-py/blob/main/supabase/_async/auth_client.py"

    subgraph "Underlying Wrappers"
        PostPy["postgrest-py"]:::ext
        AuthPy["auth-py"]:::ext
        RealtimePy["realtime-py"]:::ext
        StoragePy["storage-py"]:::ext
        FunctionsPy["functions-py"]:::ext
    end

    SyncAuth --> AuthPy
    SyncPost --> PostPy
    SyncStorage --> StoragePy
    SyncFunc --> FunctionsPy
    SyncRealtime --> RealtimePy

    AsyncAuth --> AuthPy
    AsyncPost --> PostPy
    AsyncStorage --> StoragePy
    AsyncFunc --> FunctionsPy
    AsyncRealtime --> RealtimePy

    subgraph "Transport Layer"
        HTTP["HTTP (requests/httpx)"]:::transport
        WS["WebSocket"]:::transport
    end

    PostPy --> HTTP
    AuthPy --> HTTP
    FunctionsPy --> HTTP
    StoragePy --> HTTP
    RealtimePy --> WS

    subgraph "Supabase Backend"
        BackendDB["PostgREST API"]:::backend
        BackendAuth["GoTrue Auth Service"]:::backend
        BackendRT["Realtime Gateway"]:::backend
        BackendStorage["Storage Buckets"]:::backend
        BackendFunc["Edge Functions API"]:::backend
    end

    HTTP --> BackendDB
    HTTP --> BackendAuth
    HTTP --> BackendStorage
    HTTP --> BackendFunc
    WS --> BackendRT

    classDef app fill:#e0f7fa,stroke:#006064;
    classDef sdk fill:#bbdefb,stroke:#0d47a1;
    classDef service fill:#c8e6c9,stroke:#2e7d32;
    classDef ext fill:#ffffff,stroke:#9e9e9e,stroke-dasharray:5 5;
    classDef transport fill:#ffe0b2,stroke:#e65100;
    classDef backend fill:#ffccbc,stroke:#bf360c;
```
