Skip to main content

Python SDK for interacting with SyftHub API

Project description

SyftHub SDK

Python SDK for interacting with the SyftHub API programmatically.

Installation

# Using pip
pip install syfthub-sdk

# Using uv
uv add syfthub-sdk

# From source
cd sdk
uv sync

Quick Start

from syfthub_sdk import SyftHubClient

# Initialize client
client = SyftHubClient(base_url="https://hub.syft.com")

# Register a new user
user = client.auth.register(
    username="john",
    email="john@example.com",
    password="secret123",
    full_name="John Doe"
)

# Login
user = client.auth.login(username="john", password="secret123")
print(f"Logged in as {user.username}")

# Get current user
me = client.auth.me()

Managing Your Endpoints

# List your endpoints (with lazy pagination)
for endpoint in client.my_endpoints.list():
    print(f"{endpoint.name} ({endpoint.visibility})")

# Get just the first page
first_page = client.my_endpoints.list().first_page()

# Create an endpoint
endpoint = client.my_endpoints.create(
    name="My Cool API",
    visibility="public",
    description="A really cool API",
    readme="# My API\n\nThis is my API documentation."
)
print(f"Created: {endpoint.slug}")

# Update an endpoint
endpoint = client.my_endpoints.update(
    endpoint_id=endpoint.id,
    description="Updated description"
)

# Delete an endpoint
client.my_endpoints.delete(endpoint_id=endpoint.id)

Browsing the Hub

# Browse public endpoints
for endpoint in client.hub.browse():
    print(f"{endpoint.path}: {endpoint.name}")

# Get trending endpoints
for endpoint in client.hub.trending(min_stars=10):
    print(f"{endpoint.name} - {endpoint.stars_count} stars")

# Get a specific endpoint by path
endpoint = client.hub.get("alice/cool-api")
print(endpoint.readme)

# Star/unstar endpoints (requires auth)
client.hub.star("alice/cool-api")
client.hub.unstar("alice/cool-api")

# Check if you've starred an endpoint
if client.hub.is_starred("alice/cool-api"):
    print("You've starred this!")

User Profile

# Update profile
user = client.users.update(
    full_name="John D.",
    avatar_url="https://example.com/avatar.png"
)

# Check username availability
if client.users.check_username("newusername"):
    print("Username is available!")

# Change password
client.auth.change_password(
    current_password="old123",
    new_password="new456"
)

Accounting

# Get account balance
balance = client.accounting.balance()
print(f"Credits: {balance.credits} {balance.currency}")

# List transactions
for tx in client.accounting.transactions():
    print(f"{tx.created_at}: {tx.amount} - {tx.description}")

Token Persistence

# Get tokens for saving
tokens = client.get_tokens()
if tokens:
    # Save to file, database, etc.
    save_tokens(tokens.access_token, tokens.refresh_token)

# Later, restore session
from syfthub_sdk import AuthTokens

tokens = AuthTokens(
    access_token=load_access_token(),
    refresh_token=load_refresh_token()
)
client.set_tokens(tokens)

Environment Variables

Variable Description
SYFTHUB_URL SyftHub API base URL
SYFTHUB_ACCOUNTING_URL Accounting service URL
SYFTHUB_ACCOUNTING_EMAIL Accounting auth email
SYFTHUB_ACCOUNTING_PASSWORD Accounting auth password

Error Handling

from syfthub_sdk import (
    SyftHubError,
    AuthenticationError,
    AuthorizationError,
    NotFoundError,
    ValidationError,
    ConfigurationError,
)

try:
    client.auth.login(username="john", password="wrong")
except AuthenticationError as e:
    print(f"Login failed: {e}")
except SyftHubError as e:
    print(f"API error [{e.status_code}]: {e.message}")

Context Manager

with SyftHubClient(base_url="https://hub.syft.com") as client:
    client.auth.login(username="john", password="secret123")
    # ... do work ...
# Client is automatically closed

Pagination

All list methods return a PageIterator for lazy pagination:

# Iterate through all items (fetches pages as needed)
for endpoint in client.my_endpoints.list():
    print(endpoint.name)

# Get just the first page
first_page = client.my_endpoints.list().first_page()

# Get all items as a list
all_items = client.my_endpoints.list().all()

# Get first N items
top_10 = client.my_endpoints.list().take(10)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

syfthub_sdk-0.1.0.tar.gz (87.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

syfthub_sdk-0.1.0-py3-none-any.whl (40.1 kB view details)

Uploaded Python 3

File details

Details for the file syfthub_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: syfthub_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 87.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for syfthub_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0612713e9bc9ade06a54dc630c4f22697cfca91b8a2f4f5ee0339feb67c9d5ad
MD5 0d4b0dbe49516dcea22650105517b22f
BLAKE2b-256 ab0f2f8ffef83560862447fa4a7802b8ecb7a5508c68a1302069aeff4e4cff4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for syfthub_sdk-0.1.0.tar.gz:

Publisher: release-python-sdk.yml on OpenMined/syfthub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file syfthub_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: syfthub_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 40.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for syfthub_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45f66560fde1dbfee57619392183f3d4a41fbe7d12ac6f68e623fd4355702d18
MD5 d0008ddd0d6771df7a605481dcaed21b
BLAKE2b-256 9c53c9af9830da98c514d4501958471adbbd77b9aede3ecaa6152470d6c45b76

See more details on using hashes here.

Provenance

The following attestation bundles were made for syfthub_sdk-0.1.0-py3-none-any.whl:

Publisher: release-python-sdk.yml on OpenMined/syfthub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page