GoTrue Python Client
The `gotrue` Python client is the official library for interacting with Supabase Auth (GoTrue), allowing Python applications to manage users, sessions, and authentication flows. It's a key component of the broader `supabase-py` client. Currently at version 2.12.4, the library maintains an active release cadence with frequent updates for bug fixes and new features.
Warnings
- gotcha When initializing `GoTrueClient`, ensure you provide the correct base URL for your Supabase Auth endpoint (e.g., `YOUR_SUPABASE_URL/auth/v1`). The `apikey` header should typically contain your Supabase 'anon' key for client-side operations, or a 'service_role' key for administrative tasks.
- gotcha The `gotrue` client does not automatically persist user sessions across application restarts. You must implement your own session storage (e.g., in a database, local storage, or a secure cookie) and load/save sessions manually using methods like `set_session` and `get_session`.
- gotcha Authentication methods that involve redirects (e.g., OAuth, email magic links) often require a `redirect_to` parameter. Ensure this URL is correctly configured in your Supabase project's Authentication settings and matches the URL provided in your client calls.
- deprecated The `refresh_session` method and related internal session refresh logic were updated. While the core functionality remains, direct calls to `refresh_session` might behave differently or be part of a larger internal flow. Rely on the client's internal session management when possible.
Install
-
pip install gotrue
Imports
- GoTrueClient
from gotrue import GoTrueClient
- GoTrueAPIError
from gotrue.exceptions import GoTrueAPIError
Quickstart
import os
from gotrue import GoTrueClient, GoTrueAPIError
# Initialize with your Supabase URL and (anon or service_role) key
SUPABASE_URL = os.environ.get('SUPABASE_URL', 'YOUR_SUPABASE_URL')
SUPABASE_ANON_KEY = os.environ.get('SUPABASE_ANON_KEY', 'YOUR_SUPABASE_ANON_KEY')
try:
client = GoTrueClient(
url=f"{SUPABASE_URL}/auth/v1",
headers={
"apikey": SUPABASE_ANON_KEY
}
)
# Example: Sign up a new user
email = "test@example.com"
password = "verysecretpassword"
response = client.sign_up(email=email, password=password)
print(f"Sign-up successful: {response.user.id}")
# You can also sign in existing users
# response = client.sign_in(email=email, password=password)
# print(f"Sign-in successful: {response.session.access_token}")
except GoTrueAPIError as e:
print(f"GoTrue API Error: {e.message}")
except Exception as e:
print(f"An unexpected error occurred: {e}")