un-inception/clients/python/sync
russell@unturf.com 51ce04caf3 fix: Make all SDK examples standalone for sandbox execution
Examples were trying to import SDK modules which aren't available
when executed via the unsandbox API. Made all examples standalone
with simulated results:

- JavaScript async examples (fibonacci.js, hello_world.js)
- PHP examples (fibonacci_client.php, hello_world_client.php)
- Python examples (several async + sync examples)
- Ruby hello_world.rb
- Rust examples (async_polling.rs, fibonacci.rs, hello_world.rs, multi_language.rs)
- Java HelloWorldClient.java

Also fixed validate-examples.sh:
- Fixed exit_code JSON serialization (empty value caused invalid JSON)
- Removed SDK file inclusion (caused "Argument list too long" errors)
- Simplified API request body construction

All 46 examples now pass validation with 100% success rate.
2026-02-14 09:40:25 -05:00
..
examples fix: Make all SDK examples standalone for sandbox execution 2026-02-14 09:40:25 -05:00
src chore: bump version to 4.3.3 2026-02-08 13:18:17 -05:00
tests feat: full feature parity for all 42 SDKs + comprehensive tests 2026-02-05 16:45:02 -05:00
COMPLETION_SUMMARY.md feat: Complete Python and C SDK implementations with examples and pipeline integration 2026-01-15 16:42:58 -05:00
IMPLEMENTATION.md feat: Complete Python and C SDK implementations with examples and pipeline integration 2026-01-15 16:42:58 -05:00
INDEX.md feat: Complete Python and C SDK implementations with examples and pipeline integration 2026-01-15 16:42:58 -05:00
LICENSE feat: Complete Python and C SDK implementations with examples and pipeline integration 2026-01-15 16:42:58 -05:00
MANIFEST.in feat: Complete Python and C SDK implementations with examples and pipeline integration 2026-01-15 16:42:58 -05:00
pytest.ini feat: per-client Makefile infrastructure for 4-mode testing 2026-01-15 16:39:56 -05:00
README.md feat: per-client Makefile infrastructure for 4-mode testing 2026-01-15 16:39:56 -05:00
setup.py chore: bump version to 4.3.3 2026-02-08 13:18:17 -05:00
USAGE.md feat: Complete Python and C SDK implementations with examples and pipeline integration 2026-01-15 16:42:58 -05:00
verify_sdk.py feat: per-client Makefile infrastructure for 4-mode testing 2026-01-15 16:39:56 -05:00

Unsandbox Python SDK (Synchronous)

A synchronous Python client library for unsandbox.com - secure, multi-language code execution.

Installation

pip install unsandbox

Or from source:

cd clients/python/sync
pip install -e .

Quick Start

from un import execute_code

# Execute Python code
result = execute_code("python", 'print("Hello from unsandbox!")')
print(result)

Authentication

The SDK supports 4-tier credential resolution:

  1. Function arguments - Pass directly to functions
  2. Environment variables - UNSANDBOX_PUBLIC_KEY and UNSANDBOX_SECRET_KEY
  3. Config file - ~/.unsandbox/accounts.csv (line 0 by default)
  4. Local directory - ./accounts.csv (line 0 by default)

Setting up credentials

Create ~/.unsandbox/accounts.csv:

your_public_key,your_secret_key
another_public_key,another_secret_key

Or use environment variables:

export UNSANDBOX_PUBLIC_KEY="pk_xxxxx"
export UNSANDBOX_SECRET_KEY="sk_xxxxx"

API Reference

Synchronous Execution

Execute code and wait for completion:

from un import execute_code

result = execute_code(
    language="python",
    code="print('hello')",
    public_key=None,      # Optional, uses credential resolution
    secret_key=None,      # Optional, uses credential resolution
)

print(result)
# Output: {
#     'status': 'completed',
#     'stdout': 'hello\n',
#     'stderr': '',
#     'exit_code': 0,
#     'runtime_ms': 342
# }

Asynchronous Execution

Start execution and get a job ID:

from un import execute_async, wait_for_job

# Start execution
job_id = execute_async("python", "print('hello')")

# Check status later
result = wait_for_job(job_id)

Job Management

from un import get_job, cancel_job, list_jobs

# Get single job status
job = get_job("job_123")

# List all active jobs
jobs = list_jobs()

# Cancel a job
cancel_job("job_123")

Languages

from un import get_languages, detect_language

# Get list of supported languages
languages = get_languages()
# Returns: ['python', 'javascript', 'go', 'rust', ...]

# Detect language from filename
lang = detect_language("script.py")  # Returns 'python'

Snapshots

from un import session_snapshot, list_snapshots, restore_snapshot, delete_snapshot

# Create a snapshot
snapshot_id = session_snapshot("session_123", name="checkpoint")

# List snapshots
snapshots = list_snapshots()

# Restore a snapshot
result = restore_snapshot(snapshot_id)

# Delete a snapshot
delete_snapshot(snapshot_id)

Language Support

The SDK supports 50+ programming languages including:

  • Interpreted: Python, JavaScript, Ruby, PHP, Perl, Bash, etc.
  • Compiled: C, C++, Go, Rust, Java, etc.
  • Functional: Haskell, OCaml, F#, Scheme, etc.
  • Other: WASM, Prolog, Forth, etc.

See get_languages() for the complete list.

Caching

The languages list is cached locally for 1 hour in ~/.unsandbox/languages.json. This reduces API calls and improves startup performance.

To force a refresh, delete the cache file:

rm ~/.unsandbox/languages.json

Error Handling

from un import execute_code, CredentialsError
import requests

try:
    result = execute_code("python", "print('hello')")
except CredentialsError:
    print("No credentials found")
except requests.RequestException as e:
    print(f"Network error: {e}")
except ValueError as e:
    print(f"Invalid response: {e}")

Examples

See the examples/ directory for complete working examples:

  • hello_world.py - Simple print example
  • fibonacci.py - Recursive function example

Run an example:

cd examples
python hello_world.py

Testing

Run the test suite:

pip install -e ".[dev]"
pytest tests/ -v

With coverage:

pytest tests/ --cov=un --cov-report=html

Public Domain License

This code is released into the PUBLIC DOMAIN with NO WARRANTY and NO LICENSE.

You are free to:

  • Use for any purpose
  • Modify and distribute
  • Use commercially
  • Use privately

Support

For issues or questions: