Python

Use HTTPX to call the Sandbox API from Python. A public Python SDK package is not available yet.

Install

bash
pip install httpx

Get a Tangle API key and set TANGLE_API_KEY in your environment. Add credit at id.tangle.tools before creating a sandbox.

Run a command

Save this program as example.py and run python example.py. It waits for the sandbox to start and deletes it when the block exits.

example.py
import os
import time
from contextlib import ExitStack
import httpx

with httpx.Client(
    base_url="https://sandbox.tangle.tools", timeout=180,
    headers={"Authorization": f"Bearer {os.environ['TANGLE_API_KEY']}"},
) as tangle, ExitStack() as cleanup:
    box = tangle.post("/v1/sandboxes", json={
        "maxLifetimeSeconds": 900,
    }).raise_for_status().json()
    path = f"/v1/sandboxes/{box['id']}"
    cleanup.callback(lambda: tangle.delete(path).raise_for_status())
    for _ in range(60):
        if box["status"] == "running":
            break
        if box["status"] not in ("pending", "provisioning"):
            raise RuntimeError(box.get("error") or box["status"])
        time.sleep(2)
        box = tangle.get(path).raise_for_status().json()
    else:
        raise TimeoutError("Sandbox did not start")

    result = tangle.post(f"{path}/runtime/terminals/commands", json={
        "command": "node --version", "timeout": 30000,
    }).raise_for_status().json()
    print(result["result"]["stdout"])

Every request sends your key as an Authorization: Bearer header. raise_for_status() reports failed HTTP requests before the example reads their responses.

Use files and agents