Python
Use HTTPX to call the Sandbox API from Python.
The tangle-sandbox SDK wraps these calls: pip install tangle-sandbox.
Install
pip install httpxSet TANGLE_API_KEY to your account API key.
Keep it in your server or local environment.
Run a command
This program creates a sandbox, waits for it to start, runs a command, and deletes it when the block exits.
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/process/spawn", json={
"executable": "/bin/sh", "args": ["-c", "node --version"],
"blocking": True, "timeoutMs": 30000,
}).raise_for_status().json()
print(result["data"]["stdout"])Choose Docker or Firecracker
"runtime": "docker" runs a container on the host's kernel; "firecracker" runs a microVM with its own kernel, for untrusted code, on an environment whose runtimes include firecracker (GET /v1/environments).
Omit it for the platform default: Docker first, Firecracker when Docker has no room.
Every sandbox read returns runtime, where the sandbox landed:
import os
import time
import httpx
with httpx.Client(
base_url="https://sandbox.tangle.tools", timeout=180,
headers={"Authorization": f"Bearer {os.environ['TANGLE_API_KEY']}"},
) as tangle:
box = tangle.post(
"/v1/sandboxes", json={"runtime": "firecracker"},
).raise_for_status().json()
path = f"/v1/sandboxes/{box['id']}"
try:
while box["status"] in ("pending", "provisioning"):
time.sleep(2)
box = tangle.get(path).raise_for_status().json()
print(box["status"], box.get("runtime")) # running firecracker
finally:
tangle.delete(path).raise_for_status()The tangle-sandbox SDK takes the same choice as CreateSandboxOptions(runtime="firecracker") and reads it back as box.runtime.
List sandboxes
import os
import httpx
with httpx.Client(
base_url="https://sandbox.tangle.tools",
headers={"Authorization": f"Bearer {os.environ['TANGLE_API_KEY']}"},
timeout=30,
) as client:
response = client.get("/v1/sandboxes").raise_for_status().json()
for sandbox in response["sandboxes"]:
print(sandbox["id"], sandbox["status"])