Installation
Install the Supabase Python client library:
Initializing
Create a Supabase client to interact with your database.
from supabase import create_client, Client
import os
url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_KEY")
supabase: Client = create_client(url, key)
The unique Supabase URL for your project.
The Supabase anon key or service role key for your project.
Optional configuration parameters.
The database schema to use.
Custom headers to send with every request.
Automatically refresh the token before expiring.
Whether to persist the user session.
Database Operations
Select Data
Query data from your tables.
response = supabase.table('countries').select('*').execute()
The returned rows from the query.
The total count of rows (if count option is specified).
Select with Filters
response = supabase.table('countries') \
.select('name, capital') \
.eq('id', 1) \
.single() \
.execute()
Select with Joins
response = supabase.table('countries') \
.select('name, cities(name, population)') \
.execute()
Common Filters
# Equal to
response = supabase.table('countries').select('*').eq('name', 'Albania').execute()
# Not equal to
response = supabase.table('countries').select('*').neq('name', 'Albania').execute()
# Greater than
response = supabase.table('countries').select('*').gt('population', 1000000).execute()
# Less than
response = supabase.table('countries').select('*').lt('population', 1000000).execute()
# Like (pattern matching)
response = supabase.table('countries').select('*').like('name', '%Alba%').execute()
# In list
response = supabase.table('countries').select('*').in_('name', ['Albania', 'Algeria']).execute()
# Is null
response = supabase.table('countries').select('*').is_('capital', 'null').execute()
Insert Data
Insert rows into your tables.
data = supabase.table('countries').insert({
'name': 'Denmark',
'code': 'DK'
}).execute()
values
dict | list[dict]
required
The values to insert. Can be a single dictionary or a list of dictionaries.
Insert Multiple Rows
data = supabase.table('countries').insert([
{'name': 'Denmark', 'code': 'DK'},
{'name': 'Norway', 'code': 'NO'}
]).execute()
Update Data
Update existing rows in your tables.
data = supabase.table('countries') \
.update({'name': 'Australia'}) \
.eq('id', 1) \
.execute()
Upsert Data
Insert or update rows based on unique constraints.
data = supabase.table('countries') \
.upsert({'id': 1, 'name': 'Australia'}) \
.execute()
Specify which columns should be used for the conflict resolution.
Delete Data
Delete rows from your tables.
data = supabase.table('countries') \
.delete() \
.eq('id', 1) \
.execute()
Authentication
Sign Up
Create a new user account.
response = supabase.auth.sign_up({
'email': 'example@email.com',
'password': 'example-password'
})
The user’s email address.
Optional parameters.
Additional user metadata.
A URL to redirect to after signup.
Captcha token for verification.
Sign In
Sign in an existing user.
response = supabase.auth.sign_in_with_password({
'email': 'example@email.com',
'password': 'example-password'
})
Sign Out
Sign out the current user.
Get Session
Get the current session.
session = supabase.auth.get_session()
The current session object or None if no active session.
Get User
Get the current user.
user = supabase.auth.get_user()
The current user object or None if not authenticated.
Storage
Upload File
Upload a file to a storage bucket.
with open('avatar.png', 'rb') as f:
response = supabase.storage.from_('avatars').upload(
path='public/avatar1.png',
file=f,
file_options={'content-type': 'image/png'}
)
The file path including the file name.
Upload options.Show file_options properties
Cache control header value.
When True, overwrites existing file with the same path.
Download File
Download a file from storage.
response = supabase.storage.from_('avatars').download('public/avatar1.png')
The file path to download.
List Files
List all files in a bucket.
files = supabase.storage.from_('avatars').list('public', {
'limit': 100,
'offset': 0,
'sortBy': {'column': 'name', 'order': 'asc'}
})
Delete Files
Delete files from storage.
response = supabase.storage.from_('avatars').remove([
'public/avatar1.png',
'public/avatar2.png'
])
Get Public URL
Get the public URL for a file.
url = supabase.storage.from_('avatars').get_public_url('public/avatar1.png')
print(url)
Realtime
Subscribe to Changes
Listen to database changes in realtime.
def on_change(payload):
print('Change received!', payload)
supabase.table('countries') \
.on('*', on_change) \
.subscribe()
The database event to listen for: INSERT, UPDATE, DELETE, or * for all.
Function to call when an event occurs.
Unsubscribe
Stop listening to changes.
supabase.table('countries').unsubscribe()
Edge Functions
Invoke Function
Invoke a Supabase Edge Function.
response = supabase.functions.invoke(
'hello-world',
invoke_options={'body': {'name': 'Functions'}}
)
The name of the Edge Function to invoke.
Function invocation options.Show invoke_options properties
The request body to send to the function.
Custom headers to send with the request.
The response data from the function.
Type Hints
The Python client supports type hints:
from supabase import Client, create_client
from typing import List, Dict
supabase: Client = create_client(url, key)
response = supabase.table('countries').select('*').execute()
data: List[Dict] = response.data
Error Handling
Handle errors using try-except blocks:
try:
response = supabase.table('countries').select('*').execute()
data = response.data
except Exception as e:
print(f'Error: {e}')
Async Support
Use the async client for asynchronous operations:
import asyncio
from supabase import create_async_client
async def main():
supabase = await create_async_client(url, key)
response = await supabase.table('countries').select('*').execute()
print(response.data)
asyncio.run(main())
Additional Resources
GitHub Repository
View the source code and contribute
PyPI Package
View package details and versions