Skip to content

Python SDK

bash
pip install ai2in

Stdlib-only — no third-party dependencies. Python 3.9+.

Sandbox

python
from ai2in import Sandbox

sbx = Sandbox(
    api_key="ai2in_live_…",
    base_url="https://api.ai2in.dev",  # default
    region="ap-south-1",
    timeout=300,                        # optional hard lifetime (seconds)
    metadata={"session": "agent-42"},  # optional tags (≤2KB)
)

A sandbox is created on construction. Use it as a context manager to auto-kill:

python
with Sandbox(api_key="…") as sbx:
    sbx.run_code("print('hi')")

Methods

MethodReturnsDescription
run_code(code, on_stdout=…, on_stderr=…, on_result=…, on_error=…)ExecutionRun a cell in the stateful kernel; pass callbacks to stream.
Sandbox.connect(id, api_key=…)SandboxReconnect to an existing sandbox by id.
Sandbox.list(api_key=…)listAll sandboxes on the engine (running + paused).
get_info()dictStatus, end_at, paused_at, owning node_id.
set_timeout(seconds)dictAuto-kill after N seconds from now (max 24h).
pause()dictStop the container, keep the filesystem.
resume()dictStart it again.
get_host(port)strPublic HTTPS URL for a bound port.
get_metrics()dictLive CPU / memory / pids / uptime.
kill()NoneDestroy the sandbox.
commands_CommandsShell + background processes (below).
pty_PtyInteractive terminals — pty.start()PtyHandle (send/read/resize/kill).
files_FilesFilesystem access (below).

Execution

python
execution.text        # main result text, or None
execution.results     # list[Result]
execution.logs        # Logs(stdout=[...], stderr=[...])
execution.error       # {"name", "value", "traceback"} or None

Result fields: text, html, markdown, svg, png, jpeg, pdf, latex, json, javascript, is_main_result.

commands

python
sbx.commands.run(cmd)      # blocking -> {"stdout", "stderr", "exit_code"}
sbx.commands.start(cmd)    # background -> CommandHandle
sbx.commands.list()        # -> list of {command_id, cmd, pid, status, exit_code}

CommandHandle:

python
proc = sbx.commands.start("npm run dev")
proc.id            # "cmd_…"
proc.pid           # int
proc.logs()        # {"stdout", "stderr", "status", "exit_code"}
proc.kill()        # SIGTERM to the process group

files

python
sbx.files.list(path=".")            # -> [{"name", "type", "size"}, ...]
sbx.files.read(path)                # -> str
sbx.files.write(path, content)      # -> {"path", "written"}
sbx.files.upload(path, data: bytes)  # binary upload (≤32MB)
sbx.files.download(path) -> bytes    # binary download (≤32MB)
sbx.files.watch(path=".", recursive=True)  # -> Watcher

Watcher:

python
w = sbx.files.watch(".")
w.poll()   # -> [{"type": "create|modify|remove", "path": "..."}]
w.stop()

Full example

python
from ai2in import Sandbox

with Sandbox(api_key="ai2in_live_…") as sbx:
    sbx.files.write("app.py", "print('built by an agent')")
    print(sbx.commands.run("python3 app.py")["stdout"])

    sbx.commands.start("python3 -m http.server 3000")
    print("preview:", sbx.get_host(3000))

Sovereign compute for Indian AI — hosted in Mumbai (ap-south-1).