SINAS Python SDK
Python SDK for SINAS - AI Agent & Automation Orchestration Platform.
Runtime API Client for executing agents, managing state, running webhooks, and tracking executions.
Installation
pip install sinas
With FastAPI integration:
pip install sinas[fastapi]
Quick Start
Authentication
from sinas import SinasClient
# Initialize client with base URL
client = SinasClient(base_url="http://localhost:51245")
# Step 1: Login with email (sends OTP)
login_response = client.auth.login("user@example.com")
session_id = login_response["session_id"]
# Step 2: Verify OTP and get access + refresh tokens
response = client.auth.verify_otp(session_id, "123456")
# Tokens are automatically set on the client
access_token = response["access_token"]
refresh_token = response["refresh_token"]
# Or use an API key directly
client = SinasClient(
base_url="http://localhost:51245",
api_key="your-api-key"
)
Environment Variables
Configure the client using environment variables:
export SINAS_BASE_URL="http://localhost:51245"
export SINAS_API_KEY="your-api-key"
# or
export SINAS_TOKEN="your-jwt-token"
from sinas import SinasClient
# Automatically uses environment variables
client = SinasClient()
Core Features
State Management
Store and retrieve key-value data with namespacing and visibility controls:
# Create/set a state value
state = client.state.set(
namespace="user_prefs",
key="theme",
value={"mode": "dark", "accent": "blue"},
description="User theme preferences",
visibility="private" # private, group, or public
)
# Get a specific state
state = client.state.get(state["id"])
print(state["value"]) # {"mode": "dark", "accent": "blue"}
# List states with filtering
states = client.state.list(
namespace="user_prefs",
visibility="private",
tags="settings",
search="theme"
)
# Update state
updated = client.state.update(
state["id"],
value={"mode": "light", "accent": "green"}
)
# Delete state
client.state.delete(state["id"])
Agent Chats
Create chats with agents and send messages:
# Create a chat with an agent
chat = client.chats.create(
namespace="customer-support",
agent_name="cs-agent-v1",
title="Customer Inquiry",
input={"customer_id": "12345"} # Optional input validated by agent
)
# Send a message (blocking)
response = client.chats.send(
chat_id=chat["id"],
content="What's the status of my order?"
)
print(response["content"])
# Stream a message (Server-Sent Events)
for chunk in client.chats.stream(
chat_id=chat["id"],
content="Tell me about your services"
):
# Parse SSE data
import json
try:
data = json.loads(chunk)
if "content" in data:
print(data["content"], end="", flush=True)
except:
pass
# Get chat with all messages
chat_data = client.chats.get(chat["id"])
messages = chat_data["messages"]
# List chats: one page at a time, or every chat (follows X-Next-Cursor)
page = client.chats.list_page(limit=50) # page.chats, page.next_cursor
more = client.chats.list_page(limit=50, cursor=page.next_cursor)
for listed_chat in client.chats.iter_all():
print(listed_chat["title"])
# Update chat
client.chats.update(chat["id"], title="Updated Title")
# Delete chat
client.chats.delete(chat["id"])
When the Agent Asks or Waits
An agent with the askUser system tool can stop a turn to ask the user a
question; one with background can wait for a background run. invoke() and
send() say how the turn ended in status (Sinas 0.5.0+; against older
servers it is always "completed"):
status |
Meaning |
|---|---|
completed |
The final reply is in the response |
input_required |
The agent asked; pending_inputs lists the questions |
in_progress |
Background work outlasted the request; follow channel_id |
injected |
The chat was busy, so the message went into the turn already there |
Both also return usage: the LLM calls (tokens, cost_usd) of that request.
def print_events(events):
"""Print a turn's streamed content; return the stream's last event."""
last = None
for event in events:
if event.event == "message" and event.data.get("content"):
print(event.data["content"], end="")
last = event
return last
result = client.chats.invoke("support", "triage", "Where is my order?",
session_key="slack:U1")
while result["status"] == "input_required":
question = result["pending_inputs"][0]
answer = input(f"{question['question']} {question['options'] or ''} ")
# wait=True returns the resumed turn: completed, or the next question
result = client.chats.answer(result["chat_id"], question["tool_call_id"],
answer, wait=True)
if result["status"] == "completed":
print(result["reply"])
elif result["status"] == "in_progress":
print_events(client.chats.follow_channel(result["chat_id"], result["channel_id"]))
elif result["status"] == "injected":
# The session's chat was busy: the message went into the turn already
# there, which may itself wait on a question (pending_inputs) or on
# background work (channel_id). Nothing was answered yet.
print("Still working on the previous message.")
# Open questions of a chat, e.g. after a restart
client.chats.list_pending_inputs(chat_id)
# Without wait, the answer returns the channel the resumed turn streams on
accepted = client.chats.answer(chat_id, tool_call_id, "123")
if accepted["resumed"] or accepted["resume_pending"]:
print_events(client.chats.follow_channel(chat_id, accepted["channel_id"]))
stream_events() streams a turn as parsed events with their names
(stream() yields only the data). A question arrives as input_required,
followed by round_suspended (with the round's channel_id), and the stream
ends with done {"status": "suspended"}. A tool call that needs approval
arrives as approval_required; the round resumes on the channel
approve_tool() returns once every asked call is decided:
resume_channel = None
for event in client.chats.stream_events(chat_id, "Where is my order?"):
if event.event == "message":
print(event.data.get("content", ""), end="")
elif event.event == "input_required":
print("\nAgent asks:", event.data["question"]) # answer with answer()
elif event.event == "approval_required":
# Approve (optionally for the rest of the chat) or reject a tool call
decision = client.chats.approve_tool(
chat_id, event.data["tool_call_id"], True, always_allow=True
)
if decision["resume"] == "queued": # the round's last decision
resume_channel = decision["channel_id"]
elif event.event == "done":
print("\n", event.data["status"]) # "completed" or "suspended"
if resume_channel:
print_events(client.chats.follow_channel(chat_id, resume_channel))
Webhook Execution
Execute webhooks by path:
# Execute a webhook
result = client.webhooks.run(
path="process-payment",
method="POST",
body={"amount": 100, "currency": "USD"},
headers={"X-Custom-Header": "value"},
query={"idempotency_key": "abc123"}
)
print(result["execution_id"]) # Track execution
print(result["result"]) # Function output
Executions
Track and manage function executions:
# List recent executions
executions = client.executions.list(
function_name="payment-processor",
status="completed",
limit=10
)
# Get execution details
execution = client.executions.get("execution-id")
print(execution["status"]) # running, completed, failed, awaiting_input
print(execution["output_data"])
# Get execution steps
steps = client.executions.get_steps("execution-id")
for step in steps:
print(f"{step['step_name']}: {step['status']}")
# Continue a paused execution
result = client.executions.continue_execution(
"execution-id",
input={"user_choice": "approve"}
)
Authentication Methods
# Refresh access token
refreshed = client.auth.refresh(refresh_token)
new_access_token = refreshed["access_token"]
# Exchange external OIDC token
response = client.auth.external_auth(external_oidc_token)
# Get current user info
user = client.auth.get_me()
print(user["email"])
# Check user permissions with OR logic (user needs at least one)
check = client.auth.check_permissions(
["sinas.functions.read:all", "sinas.functions.create:own"],
logic="OR"
)
print(check["result"]) # True if user has at least one permission
print(check["checks"]) # Detailed results for each permission
# Check user permissions with AND logic (user needs all)
check = client.auth.check_permissions(
["sinas.admin:all", "sinas.functions.write:all"],
logic="AND"
)
print(check["result"]) # True only if user has all permissions
# Logout (revoke refresh token)
client.auth.logout(refresh_token)
FastAPI Integration
SINAS provides two powerful approaches for FastAPI integration:
Approach 1: Ready-to-Mount Routers
Mount SINAS Runtime API endpoints directly in your FastAPI app with automatic authentication:
from fastapi import FastAPI
from sinas.integrations.routers import create_runtime_router
app = FastAPI()
# Mount ALL runtime endpoints at once
app.include_router(
create_runtime_router("http://localhost:51245", include_auth=False),
prefix="/api/runtime"
)
# This automatically creates endpoints like:
# - GET /api/runtime/states
# - POST /api/runtime/states
# - GET /api/runtime/chats
# - POST /api/runtime/agents/{namespace}/{agent}/chats
# - POST /api/runtime/webhooks/{path}
# - GET /api/runtime/executions
Or mount individual routers:
from sinas.integrations.routers import (
create_state_router,
create_chat_router,
create_webhook_router,
create_executions_router
)
app.include_router(create_state_router("http://localhost:51245"), prefix="/runtime/states")
app.include_router(create_chat_router("http://localhost:51245"), prefix="/runtime/chats")
app.include_router(create_webhook_router("http://localhost:51245"), prefix="/runtime/webhooks")
app.include_router(create_executions_router("http://localhost:51245"), prefix="/runtime/executions")
Approach 2: Custom Endpoints with SDK Client
Build custom endpoints with business logic using the authenticated client:
import asyncio
from fastapi import FastAPI, Depends
from sinas import SinasClient
from sinas.integrations.fastapi import SinasAuth
app = FastAPI()
sinas = SinasAuth(base_url="http://localhost:51245")
# Include auto-generated auth endpoints
app.include_router(sinas.router, prefix="/auth")
# Custom endpoint with auto-authentication
@app.get("/my-states")
async def get_my_states(client: SinasClient = Depends(sinas)):
"""List user's states with custom filtering."""
states = await asyncio.to_thread(client.state.list, limit=10)
return [{"id": s["id"], "key": s["key"]} for s in states]
# Combine multiple SDK calls
@app.post("/quick-chat")
async def quick_chat(
agent_namespace: str,
agent_name: str,
message: str,
client: SinasClient = Depends(sinas)
):
"""Create chat and send message in one request."""
chat = await asyncio.to_thread(client.chats.create, agent_namespace, agent_name)
response = await asyncio.to_thread(client.chats.send, chat["id"], message)
return {"chat_id": chat["id"], "response": response}
# Permission-protected endpoint
@app.post("/admin/cleanup")
async def cleanup_states(
namespace: str,
client: SinasClient = Depends(sinas.require("sinas.contexts.delete:all"))
):
"""Admin-only: Clean up states (requires permission)."""
states = await asyncio.to_thread(client.state.list, namespace=namespace)
for state in states:
await asyncio.to_thread(client.state.delete, state["id"])
return {"deleted": len(states)}
Auto-Generated Auth Endpoints
The sinas.router automatically provides:
POST /auth/login- Send OTP to emailPOST /auth/verify-otp- Verify OTP and get tokensPOST /auth/refresh- Refresh access token using refresh tokenGET /auth/me- Get current authenticated user
Permission-Based Access Control
Use sinas.require() to protect endpoints with flexible AND/OR logic:
# Require a single permission
@app.delete("/states/{state_id}")
async def delete_state(
state_id: str,
client: SinasClient = Depends(sinas.require("sinas.contexts.delete:own"))
):
await asyncio.to_thread(client.state.delete, state_id)
return {"message": "Deleted"}
# Require ANY of multiple permissions (OR logic - default)
@app.post("/moderate-content")
async def moderate_content(
client: SinasClient = Depends(
sinas.require("sinas.admin:all", "sinas.moderation.write:all")
)
):
# User needs EITHER admin:all OR moderation.write:all
return {"message": "Content moderated"}
# Require ALL permissions (AND logic)
@app.post("/admin-action")
async def admin_action(
client: SinasClient = Depends(
sinas.require("sinas.admin:all", "sinas.functions.write:all", logic="AND")
)
):
# User needs BOTH admin:all AND functions.write:all
return {"message": "Admin action completed"}
# Alternative: Use require_all() for AND logic
@app.post("/super-admin-action")
async def super_admin_action(
client: SinasClient = Depends(
sinas.require_all("sinas.admin:all", "sinas.functions.write:all", "sinas.contexts.write:all")
)
):
# User needs ALL three permissions
return {"message": "Super admin action completed"}
Permission Logic:
- OR logic (default): User needs at least one of the specified permissions
- AND logic: User needs all of the specified permissions
- Use
logic="AND"parameter orrequire_all()method for AND logic
Complete Example
See examples/fastapi_app.py for a complete working example with both approaches.
Run it with:
uvicorn examples.fastapi_app:app --reload
Then visit http://localhost:8000/docs for interactive API documentation.
Context Manager
The client can be used as a context manager for proper resource cleanup:
with SinasClient(base_url="http://localhost:51245") as client:
states = client.state.list()
# Client is automatically closed when exiting
Error Handling
from sinas import (
SinasClient,
SinasAPIError,
SinasAuthError,
SinasNotFoundError,
SinasValidationError
)
client = SinasClient(base_url="http://localhost:51245")
try:
state = client.state.get("non-existent-id")
except SinasNotFoundError as e:
print(f"State not found: {e}")
except SinasAuthError as e:
print(f"Authentication failed: {e}")
except SinasValidationError as e:
print(f"Validation error: {e}")
except SinasAPIError as e:
print(f"API error: {e}")
print(f"Status code: {e.status_code}")
print(f"Response: {e.response}")
API Reference
SinasClient
auth
login(email)- Send OTP to emailverify_otp(session_id, otp_code)- Verify OTP and get tokensexternal_auth(token)- Exchange external OIDC tokenrefresh(refresh_token)- Refresh access tokenlogout(refresh_token)- Revoke refresh tokenget_me()- Get current user infocheck_permissions(permissions, logic="AND")- Check if user has permissions with AND/OR logic
state
set(namespace, key, value, ...)- Create/update stateget(state_id)- Get state by IDlist(namespace?, visibility?, ...)- List states with filtersupdate(state_id, value?, ...)- Update statedelete(state_id)- Delete state
chats
create(namespace, agent_name, input?, title?)- Create chat with agentsend(chat_id, content)- Send message (blocking)stream(chat_id, content)- Send message (streaming SSE)get(chat_id)- Get chat with messageslist(include_archived?, q?, limit?, cursor?)- List user's chats; withoutlimit/cursorthe server returns at most its CHATS_UNPAGINATED_MAX (10,000 by default) and warns when it cut the listlist_page(include_archived?, q?, limit=50, cursor?)- One page as aChatPage(chats,next_cursor,truncated)iter_all(include_archived?, q?, page_size=200)- Iterate over every chat, followingX-Next-Cursorupdate(chat_id, title?)- Update chatdelete(chat_id)- Delete chat
webhooks
run(path, method?, body?, headers?, query?)- Execute webhook
executions
list(function_name?, status?, skip?, limit?)- List executionsget(execution_id)- Get execution detailsget_steps(execution_id)- Get execution stepscontinue_execution(execution_id, input)- Continue paused execution
FastAPI Integration
SinasAuth(base_url, auto_error=True)
FastAPI dependency for authentication and authorization.
Methods:
require(*permissions, logic="OR")- Create dependency requiring permissions with AND/OR logicrequire_all(*permissions)- Create dependency requiring ALL permissions (AND logic)
Router Functions
create_runtime_router(base_url, include_auth=False)- Complete runtime routercreate_state_router(base_url, prefix="")- State endpointscreate_chat_router(base_url, prefix="")- Chat endpointscreate_webhook_router(base_url, prefix="")- Webhook endpointscreate_executions_router(base_url, prefix="")- Execution endpoints
Development
Install Development Dependencies
pip install -e ".[dev]"
Run Examples
# Basic usage
python examples/basic_usage.py
# FastAPI app
uvicorn examples.fastapi_app:app --reload
Code Formatting
black sinas
ruff check sinas
License
MIT
Metadata
Release files for sinas 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sinas-0.3.0.tar.gz | 56.8 kB | Without provenance | Details for sinas-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sinas-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for sinas-0.3.0-py3-none-any.whl |
Total release size: 105.7 kB
Release files / sinas-0.3.0.tar.gz
| Download URL | sinas-0.3.0.tar.gz |
|---|---|
| Size | 56.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fcd48d190833ab376dfc16d04284ca87b43abdb0cb5d30010c9449c1d5cfbc6d
|
|
BLAKE2b-256 checksum How to use checksums |
ff7e1aa7ff4f1adbce15f4c0db966a8f596a244d89f5546608925100d308649a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / sinas-0.3.0-py3-none-any.whl
| Download URL | sinas-0.3.0-py3-none-any.whl |
|---|---|
| Size | 48.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
55f0b3d0f72197c5565ecefcb78bcd5f598f83915f1b9f10b0e7e61b0f5084a1
|
|
BLAKE2b-256 checksum How to use checksums |
5e06aae3422fa27c6b98999312f932d2b6913db2e6e7f4780b807a662fea48b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for sinas 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.