Python
potalab-base is the application client for Python 3.10+ (built on httpx, fully typed). It mirrors the JavaScript client: from_().select() with filters, rpc, auth, storage and functions.invoke.
pip install potalab-basefrom potalab_base import create_client
base = create_client("https://app.example.com", "pb_pub_...")base.auth.sign_in_with_password("ada@example.com", "secret")
todos = ( base.from_("todos") .select("id,title") .eq("done", False) .order("id", desc=True) .limit(20) .execute() .data)base.from_("todos").insert({"title": "buy tea"}).execute()from potalab_base import create_async_client
async with create_async_client("https://app.example.com", "pb_pub_...") as base: await base.auth.sign_in_with_password("ada@example.com", "secret") res = await base.from_("todos").select("*").eq("done", False) # awaiting the builder runs it print(res.data, res.count)- Errors are raised as
BaseErrorwithcode,status,details,hint,request_id. - Tokens refresh automatically shortly before expiry and once after a 401. A secret key (
pb_sec_..., server only) is exchanged for a short-livedservice_roletoken for you. - Storage uploads through a presigned URL:
base.storage.from_("avatars").upload("me.png", data), plusdownload,create_signed_url,list,removeand bucket management. - Functions:
base.functions.invoke("send-welcome", {"user_id": "u1"}). - MFA (TOTP):
base.auth.mfa.enroll / verify / challenge / unenroll / list_factors / regenerate_recovery_codes / get_authenticator_assurance_level.sign_in_with_passwordreturnsres.mfa_requiredandres.ticketwhen the account has a verified factor; finish withbase.auth.mfa.challenge(ticket=res.ticket, code="123456"). Secrets, tickets and recovery codes are hidden fromrepr(). - Vectors (pgvector):
base.vectors.search("docs", embedding, k=4),upsertanddelete, over thematch_<table>function with RLS. Create the table withvector_table_sql("docs", 1536). - Not yet in the Python client: passkeys and OAuth.
Transactions
Section titled “Transactions”Same protocol as the JavaScript client. Build operations with Op.insert / upsert / update / delete / select / rpc; Ref(i, "col") reads a column returned by an earlier operation and Inc(n) is an atomic col = col + n.
from potalab_base import Op, Ref, Inc, TransactionError
res = base.transaction( # one request, one atomic transaction [ Op.insert("orders", {"total": 5}, returning=["id"]), Op.insert("items", {"order_id": Ref(0, "id")}), Op.update("stock", {"qty": Inc(-1)}, filter={"sku": {"eq": "a"}}, expect=1), ], retry=2, # resend after a deadlock or serialization failure)res[0].rows[0]["id"]A failed operation rolls everything back and raises TransactionError (failed_index, pg_code, retryable).
For read, decide, then write, use an interactive transaction (at most 10 seconds). It commits on a clean exit and rolls back on an exception; the async client uses async with and await.
with base.transaction.interactive(isolation="serializable") as tx: acc = tx.select("accounts", ["balance"], filter={"id": {"eq": 1}}, lock="update").row tx.update("accounts", {"balance": acc["balance"] - 10}, filter={"id": {"eq": 1}})
# re-runs the whole callback after a serialization failurebase.transaction.run_interactive(lambda tx: tx.rpc("transfer", {"amount": 10}).rows, retries=3)Interactive transactions need a plan that includes them (PlanFeatureError otherwise); the batch form works on every plan.
Realtime
Section titled “Realtime”Async client only. Install the extra: pip install "potalab-base[realtime]".
async with create_async_client(url, key) as base: await base.auth.sign_in_with_password(email, password) ch = base.channel("room:1") ch.on_postgres_changes(on_change, table="todos", event="INSERT", filter="user_id=eq.1") ch.on_broadcast("cursor", on_cursor) ch.on_presence("sync", lambda _: print(ch.presence_state())) await ch.subscribe() await ch.send_broadcast("cursor", {"x": 1}) await ch.track({"name": "ada"})Callbacks may be sync or async. The access token is sent in the first frame, never in the URL, and is pushed again whenever it refreshes. The connection sends heartbeats and reconnects with exponential backoff, re-joining your channels; a system event with code == "resync" means changes may have been missed, so refetch. A secret key is never put in the URL.