Python SDK for SelfDB - Full Self-Hosted BaaS Built for AI Agents
Project description
SelfDB Python SDK
Python SDK for SelfDB - Full Self-Hosted BaaS Built for AI Agents.
Installation
pip install selfdb
Or install from source:
pip install -e .
Quick Start
import asyncio
from selfdb import SelfDB
async def main():
# Initialize the client
selfdb = SelfDB(
base_url="http://localhost:8000",
api_key="your-api-key"
)
# Authenticate
await selfdb.auth.login(email="user@example.com", password="password")
# Get current user
user = await selfdb.auth.me()
print(f"Logged in as: {user.email}")
# List tables
tables = await selfdb.tables.list()
print(f"Found {len(tables)} tables")
# Close the client
await selfdb.close()
asyncio.run(main())
Features
- Authentication: Login, logout, token refresh, user management
- Tables: CRUD operations, column management, data queries with fluent builder
- Storage: Bucket and file management, upload/download
- Realtime: WebSocket subscriptions for live updates
Authentication
# Login
await selfdb.auth.login(email="...", password="...")
# Get current user
user = await selfdb.auth.me()
# Refresh token
await selfdb.auth.refresh()
# Logout
await selfdb.auth.logout()
# Logout from all devices
await selfdb.auth.logout_all()
# User management (admin)
users = await selfdb.auth.users.list()
user = await selfdb.auth.users.create(UserCreate(email="...", password="..."))
await selfdb.auth.users.delete(user_id)
Tables
from selfdb.models import TableCreate, ColumnDefinition
# Create a table
table = await selfdb.tables.create(TableCreate(
name="my_table",
columns=[
ColumnDefinition(name="id", type="UUID", primary_key=True),
ColumnDefinition(name="title", type="TEXT"),
],
public=False,
))
# List tables
tables = await selfdb.tables.list(search="my", sort_by="created_at")
# Insert data
await selfdb.tables.data.insert(table.id, {"id": "...", "title": "Hello"})
# Query with fluent builder
result = await selfdb.tables.data.query(table.id) \
.search("hello") \
.sort("created_at", "desc") \
.page(1) \
.page_size(25) \
.execute()
# Update row
await selfdb.tables.data.update_row(table.id, row_id, {"title": "Updated"})
# Delete row
await selfdb.tables.data.delete_row(table.id, row_id)
# Delete table
await selfdb.tables.delete(table.id)
Storage
from selfdb.models import BucketCreate
# Create a bucket
bucket = await selfdb.storage.buckets.create(BucketCreate(
name="my-bucket",
public=False,
))
# Upload a file
with open("file.pdf", "rb") as f:
result = await selfdb.storage.files.upload(bucket.id, "file.pdf", f)
# Download a file
content = await selfdb.storage.files.download("my-bucket", "file.pdf")
# List files
files = await selfdb.storage.files.list(bucket_id=bucket.id)
# Delete file
await selfdb.storage.files.delete(file_id)
# Delete bucket
await selfdb.storage.buckets.delete(bucket.id)
Realtime
# Connect to realtime
await selfdb.realtime.connect()
# Create a channel for a topic
channel = selfdb.realtime.channel("table:users")
# Register callbacks (chainable)
channel.on("INSERT", lambda p: print("New user:", p.new))
channel.on("UPDATE", lambda p: print("Updated:", p.new, "from:", p.old))
channel.on("DELETE", lambda p: print("Deleted:", p.old))
channel.on("*", lambda p: print("Any event:", p.event))
# Subscribe to start receiving events
await channel.subscribe()
# Access payload properties
# p.event - "INSERT", "UPDATE", or "DELETE"
# p.table - table name
# p.new - new row data (dict or None)
# p.old - old row data (dict or None)
# Unsubscribe from channel
await channel.unsubscribe()
# Disconnect from realtime
await selfdb.realtime.disconnect()
Error Handling
from selfdb.exceptions import (
SelfDBError,
APIConnectionError,
BadRequestError,
AuthenticationError,
PermissionDeniedError,
NotFoundError,
ConflictError,
InternalServerError,
)
try:
await selfdb.tables.get("nonexistent-id")
except NotFoundError:
print("Table not found")
except PermissionDeniedError:
print("Access denied")
except SelfDBError as e:
print(f"API error: {e}")
Context Manager
async with SelfDB(base_url="...", api_key="...") as selfdb:
await selfdb.auth.login(email="...", password="...")
# ... use the client
# Automatically closes when exiting context
Development
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Type check
mypy selfdb
# Lint
ruff check selfdb
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
selfdb-0.0.5.tar.gz
(43.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
selfdb-0.0.5-py3-none-any.whl
(18.2 kB
view details)
File details
Details for the file selfdb-0.0.5.tar.gz.
File metadata
- Download URL: selfdb-0.0.5.tar.gz
- Upload date:
- Size: 43.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0rc1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18718207908a8287fbba21714f5d8291178bd9f6cf989b7fe85cb1dec26aae0c
|
|
| MD5 |
3e1c1089e32e4b4788889ec48e6654bf
|
|
| BLAKE2b-256 |
5f690ec72f91599020b08a8365660874161090e6a3b36973985d638956ec264f
|
File details
Details for the file selfdb-0.0.5-py3-none-any.whl.
File metadata
- Download URL: selfdb-0.0.5-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0rc1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32024316ea2a25007dbee24218742ec221fbd45f518b81354a0df1fb6579e784
|
|
| MD5 |
ef3dac2d3dca82bf160778968bcaa27b
|
|
| BLAKE2b-256 |
be777a6a2f618da7f5f85a6cd451ea1d9041e6ef5bbc16e38974d6b2afe798e8
|