API Reference

A simple Python API for self-hosting stateless servers

Server Creation

ss.create()

Create a new server from a dictionary of endpoints. This is the primary method for turning Python functions into HTTP endpoints with automatic process management.

Parameters
name str Unique name for the server
endpoints dict[str, Callable] Dictionary mapping URL paths to Python functions
dependencies list[str] Optional list of pip packages to install
port int Optional specific port (default: auto-assign)
expiration_seconds int Auto-cleanup after N seconds (default: 86400, -1 = never)
force bool Replace existing server with same name (default: False)
Returns
Server Server object with url, status, logs, and control methods
Example
import syft_serve as ss

def predict(data: dict):
    return {"prediction": "positive", "confidence": 0.95}

# Create a server
server = ss.create(
    name="ml_api",
    endpoints={"/predict": predict},
    dependencies=["numpy", "scikit-learn"]
)

print(server.url)  # http://localhost:8000

Server Management

ss.list()

List all currently running servers. Returns a list of Server objects that you can inspect and control.

Parameters
No parameters
Returns
list[Server] List of all active Server objects
Example
# List all servers
servers = ss.list()

for server in servers:
    print(f"{server.name}: {server.url} - {server.status}")
ss.get()

Get a specific server by name. Useful for retrieving a server created in a previous session or different notebook.

Parameters
name str Name of the server to retrieve
Returns
Server Server object if found
Example
# Get a specific server
server = ss.get("ml_api")

# Access server properties
print(f"URL: {server.url}")
print(f"Status: {server.status}")
print(f"Uptime: {server.uptime}")
Server Display in Jupyter

Server objects have rich display representations in Jupyter notebooks that show status, logs, and control options at a glance.

Jupyter Notebook Display

When you evaluate a server object in a Jupyter cell, you get an interactive display:

🚀 hello_api

Status: ✅ Running
URL: http://localhost:8000
Endpoints:
  • /hello
  • /status
Uptime: 2h 15m
Expires In: ⏰ 21h 45m
PID: 12345
Recent logs:
INFO: Started server process [12345]
INFO: Uvicorn running on http://0.0.0.0:8000
INFO: Application startup complete.
Recent errors:
No recent errors
Try: server.stdout.tail(20), server.stderr.tail(20), server.env, server.terminate()
# In Jupyter, just evaluate the server object
server

# Or get multiple servers
servers = ss.list()
servers  # Shows a list with each server's display

Logging & Debugging

server.stdout.tail()

Get the most recent lines from the server's standard output logs. Useful for debugging and monitoring server activity.

Parameters
n int Number of lines to return (default: 10)
Returns
str Last n lines from stdout
Example
# Get recent logs
logs = server.stdout.tail(20)
print(logs)

# Check for errors
errors = server.stderr.tail(20)
server.stdout.follow()

Stream logs in real-time as they are generated. This creates a continuous output that follows the log file, similar to `tail -f`.

Parameters
No parameters
Returns
Generator Yields log lines as they appear
Example
# Follow logs in real-time
for line in server.stdout.follow():
    print(line)
    if "error" in line.lower():
        break
server.stderr.tail()

Get the most recent lines from the server's standard error logs. Useful for debugging errors and tracking warnings.

Parameters
n int Number of lines to return (default: 10)
Returns
str Last n lines from stderr
Example
# Check for errors
errors = server.stderr.tail(20)
if errors:
    print("Recent errors:", errors)
server.stderr.follow()

Stream error logs in real-time as they are generated. Perfect for monitoring errors and warnings as they occur.

Parameters
No parameters
Returns
Generator Yields error log lines as they appear
Example
# Monitor errors in real-time
for error in server.stderr.follow():
    print(f"ERROR: {error}")
    # Send alert or take action
server.env

Access the server's environment information including Python version, installed packages, and environment variables.

Properties
python str Python version used by the server
packages list[str] List of installed packages with versions
variables dict Environment variables
Example
# Check server environment
print(f"Python: {server.env.python}")
print(f"Packages: {len(server.env.packages)}")

# Find specific package
for pkg in server.env.packages:
    if "numpy" in pkg:
        print(pkg)

Lifecycle Control

server.terminate()

Gracefully terminate a server. This stops the server process, cleans up resources, and removes it from the active servers list.

Parameters
timeout float Maximum seconds to wait for graceful shutdown (default: 5.0)
Returns
None Server is terminated and cleaned up
Example
# Terminate a specific server
server = ss.get("ml_api")
server.terminate()

# Or with custom timeout
server.terminate(timeout=10.0)
ss.terminate_all()

Terminate all running servers at once. This is useful for cleanup in notebooks or when shutting down a development environment.

Parameters
force bool Use force termination if graceful shutdown fails (default: True)
Returns
dict Summary of termination results
Example
# Clean up all servers
results = ss.terminate_all()

print(f"Terminated {results['tracked_terminated']} servers")
if results['orphaned_discovered'] > 0:
    print(f"Also found and terminated {results['orphaned_terminated']} orphaned processes")
Auto-Expiring Servers

Servers can be configured to automatically terminate after a specified time period. This prevents resource leaks and is perfect for temporary endpoints.

Configuration
expiration_seconds int Seconds until auto-termination (default: 86400 = 24 hours, -1 = never expire)
Example
# Create a temporary server (5 minutes)
temp_server = ss.create(
    name="temp_endpoint",
    endpoints={"/process": process_data},
    expiration_seconds=300
)

# Create a permanent server
permanent_server = ss.create(
    name="always_on",
    endpoints={"/health": lambda: "OK"},
    expiration_seconds=-1
)

# Check time remaining
print(f"Expires in: {temp_server.expiration_info}")