How supabase/supabase-py works
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.