SandboxPython SDKSandboxInstance

SandboxInstance

client.create(), client.get() and client.list() return a SandboxInstance; AsyncSandbox returns an AsyncSandboxInstance whose methods are coroutines. Each instance caches the sandbox’s details. refresh(), wait_for(), stop() and resume() update the cache; runtime calls check it and raise StateError unless the status is RUNNING.

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        result = box.exec("uname -s")
        assert result.exit_code == 0, result.stderr
        assert result.stdout.strip() == "Linux"
        print(result.stdout.strip())
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Properties

PropertyTypeDescription
idstrSandbox id
namestr | NoneDisplay name
statusSandboxStatusCached status
connectionSandboxConnection | Noneruntime_url, host_url, auth_token, auth_token_expires_at, ssh, web_terminal_url. auth_token opens the sandbox’s runtime; keep it secret
metadatadictYour metadata
created_at, started_at, last_activity_atdatetime | NoneTimestamps
expires_atdatetime | NoneNot reported yet: always None, even with a lifetime set
errorstr | NoneWhy the sandbox failed, when it did
gitGitCapabilityGit operations; see Git

Lifecycle

refresh

refresh() -> None

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        assert box.status == SandboxStatus.RUNNING
        client.fetch("POST", f"/v1/sandboxes/{box.id}/stop").raise_for_status()
        assert box.status == SandboxStatus.RUNNING
        box.refresh()
        assert box.status == SandboxStatus.STOPPED
        print("Refreshed cached state:", box.status.value)
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Reads the sandbox again and replaces the cached details. Raises NotFoundError after the sandbox is deleted. Request: GET /v1/sandboxes/{id}

wait_for

wait_for(status: SandboxStatus | list[SandboxStatus], options: WaitForOptions | None = None) -> None

from tangle_sandbox import WaitForOptions
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("resume.txt", "retained through readiness waiting\n")
        box.stop()
        box.wait_for(SandboxStatus.STOPPED)
        client.fetch("POST", f"/v1/sandboxes/{box.id}/resume").raise_for_status()
        box.wait_for([SandboxStatus.RUNNING, SandboxStatus.FAILED], WaitForOptions(timeout_ms=120_000))
        assert box.status == SandboxStatus.RUNNING
        assert box.read("resume.txt") == "retained through readiness waiting\n"
        print(box.status.value)
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Polls until the status is one of the targets. WaitForOptions(timeout_ms=120000, poll_interval_ms=2000) are the defaults. Raises StateError with the sandbox’s error when it reaches FAILED, and TimeoutError after timeout_ms. A sandbox that is EXPIRED or DELETED keeps polling until the timeout.

from tangle_sandbox import WaitForOptions
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.wait_for(SandboxStatus.RUNNING, WaitForOptions(timeout_ms=180_000))
        assert box.status == SandboxStatus.RUNNING
        box.refresh()
        assert box.status == SandboxStatus.RUNNING
        print(box.status.value)
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

stop

stop() -> None

from tangle_sandbox import StateError
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("checkpoint.txt", "stopped workspace\n")
        box.stop()
        box.wait_for(SandboxStatus.STOPPED)
        refused = False
        try:
            box.exec("cat checkpoint.txt")
        except StateError as error:
            refused = True
            assert error.code == "INVALID_STATE"
        assert refused, "A stopped sandbox accepted exec"
        print("Stopped sandbox refuses execution")
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Stops the sandbox and keeps its disk. status reads STOPPED when it returns. Request: POST /v1/sandboxes/{id}/stop

resume

resume() -> None

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("resume.json", '{"next":"publish"}\n')
        box.stop()
        box.wait_for(SandboxStatus.STOPPED)
        box.resume()
        box.wait_for(SandboxStatus.RUNNING)
        result = box.exec("python3 -c \"import json; print(json.load(open('resume.json'))['next'])\"")
        assert result.exit_code == 0, result.stderr
        assert result.stdout.strip() == "publish"
        print(result.stdout.strip())
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Starts a stopped sandbox. The status may still be provisioning; call wait_for(SandboxStatus.RUNNING) next. Request: POST /v1/sandboxes/{id}/resume

delete

delete() -> None

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        sandbox_id = box.id
        box.delete()
        remaining = client.get(sandbox_id)
        assert remaining is None or remaining.status == SandboxStatus.DELETED
        print("Deleted", sandbox_id)
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Deletes the sandbox and its disk for good. The cached status does not change. A sandbox that has live branch children is refused with CHILDREN_EXIST; delete the children first. Request: DELETE /v1/sandboxes/{id}

Commands

exec

exec(command: str, options: ExecOptions | None = None) -> ExecResult

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        result = box.exec("python3 -c 'import sys; print(\"partial output\"); print(\"diagnostic\", file=sys.stderr); sys.exit(7)'")
        assert result.exit_code == 7
        assert "partial output" in result.stdout.splitlines()
        assert "diagnostic" in result.stdout.splitlines()
        assert result.stderr == ""
        print(result.exit_code, result.stdout.strip(), result.stderr.strip())
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Runs command with bash and waits for it. Returns ExecResult(exit_code, stdout, stderr); a non-zero exit code is returned, not raised. stderr is merged into stdout, so stderr is empty.

ExecOptions fieldDescription
cwdDirectory to run in. A directory that does not exist is ignored and the command runs in the default directory
envExtra environment variables
timeout_msDefault 30000. A command that runs longer raises an error that names the timeout
session_idAccepted and not sent

The default directory is /home/agent, or the cloned repository when the sandbox was created with git. Request: POST /v1/sandboxes/{id}/runtime/process/spawn

import json
from tangle_sandbox import ExecOptions
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("app/package.json", json.dumps({"name": "cwd-example", "version": "1.0.0", "scripts": {"test": "node check.cjs"}}))
        box.write("app/check.cjs", "console.log(process.cwd());\n")
        result = box.exec("npm test", ExecOptions(cwd="/home/agent/app", timeout_ms=120_000))
        assert result.exit_code == 0, result.stderr
        assert "/home/agent/app" in result.stdout
        print(result.stdout[-500:])
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

For background processes, separate stderr or structured code output, see What Python can do.

Files

read

read(path: str) -> str

import json
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("unicode.json", '{"title":"Résumé","total":260}\n')
        content = box.read("unicode.json")
        assert json.loads(content) == {"title": "Résumé", "total": 260}
        print(content, end="")
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Reads a UTF-8 text file. A relative path resolves in the working directory. An absolute path outside the paths the sandbox allows is refused with a 403 error. Request: POST /v1/sandboxes/{id}/runtime/files/read

write

write(path: str, content: str) -> None

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("draft/report.md", "First draft\n")
        box.write("draft/report.md", "Approved report\n")
        assert box.read("draft/report.md") == "Approved report\n"
        print(box.read("draft/report.md"), end="")
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Writes a UTF-8 text file, replacing any content. Request: POST /v1/sandboxes/{id}/runtime/files/write

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        content = "customer,amount\nacme,1500\n"
        box.write("orders.csv", content)
        assert box.read("orders.csv") == content
        print(box.read("orders.csv"))
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

search(pattern: str, options: SearchOptions | None = None) -> list[SearchMatch]

from tangle_sandbox import SearchOptions
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("reports/tasks.txt", "TODO: export CSV\nDone: render chart\nTODO: email draft\n")
        box.write("reports/ignored.log", "TODO: exclude this extension\n")
        options = SearchOptions(cwd="/home/agent/reports", glob="*.txt", max_results=1, context=1)
        hits = box.search("TODO:", options)
        assert len(hits) == 1
        assert hits[0].path.endswith("tasks.txt")
        assert "TODO:" in hits[0].text
        print(hits[0].path, hits[0].line, hits[0].text.strip())
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Searches workspace files for a regular expression with ripgrep. SearchOptions takes glob, cwd, max_results, ignore_case and context (lines before and after). Each SearchMatch has path, line, column, text, before and after. Request: POST /v1/sandboxes/{id}/runtime/search

If search returns no matches for text you know is there, run grep -rn through exec.

Coding agents

prompt

prompt(message: str, options: PromptOptions | None = None) -> PromptResult

Sends message to the sandbox’s coding agent and waits for the whole turn. Returns PromptResult(success, response, error, trace_id, duration_ms, usage). A failed turn comes back with success=False and error set; it does not raise.

PromptOptions fieldDescription
session_idContinue a conversation
backendBackendConfig(type=..., model=..., profile=...) for this turn
timeout_msLongest wait between events
contextSent as metadata with the turn

Request: POST /v1/sandboxes/{id}/runtime/agents/run/stream

from tangle_sandbox import PromptOptions
 
run = box.prompt(
    "The test tagged null-guard fails. Find the cause and fix it.",
    PromptOptions(timeout_ms=600_000),
)
if not run.success:
    raise RuntimeError(run.error)
print(run.usage)
TokenUsage(input_tokens=119425, output_tokens=1265)

stream_prompt

stream_prompt(message: str, options: PromptOptions | None = None) -> Iterator[SandboxEvent]

Yields SandboxEvent(type, data, id) as the agent works. On AsyncSandboxInstance it is an async iterator.

Event typedata
message.part.updatedpart (with type such as text or tool) and, for text, delta
resultfinalText and tokenUsage
trace.idtraceId
errormessage
seen = set()
for event in box.stream_prompt("The test tagged timezone fails. Fix the cause."):
    part = event.data.get("part") or {}
    if event.type == "message.part.updated" and part.get("type") == "tool":
        if part.get("id") not in seen:
            seen.add(part.get("id"))
            print(f"{len(seen)}.", part.get("tool"))
    if event.type == "result":
        print(event.data.get("finalText"))
1. Bash
2. Bash
3. Bash
4. Read
...
Fixed. ...

Git

box.git runs git in the working directory. AsyncSandboxInstance.git has the same methods as coroutines.

MethodReturnsRequest
status()GitStatus: is_repo, branch, head, is_dirty, ahead, behind, staged, modified, untrackedGET .../runtime/git/status
log(limit=10)list[GitCommit]GET .../runtime/git/log
diff(ref=None)GitDiff: files, additions, deletions, raw. diff() shows unstaged changes; diff("HEAD") adds staged onesGET .../runtime/git/diff
add(paths)POST .../runtime/git/add
commit(message, *, amend=False)GitCommit: sha, short_sha, message, author, email, datePOST .../runtime/git/commit
push(*, force=False)POST .../runtime/git/push
pull(*, rebase=False)POST .../runtime/git/pull
branches()list[GitBranch]: name, current, upstream, commitGET .../runtime/git/branches
checkout(ref, *, create=False)POST .../runtime/git/checkout

push and pull use the credentials in the sandbox, such as a token from GitConfig(auth=GitAuth(token=...)) at create. commit takes no author, and the sandbox has no git identity, so set one first:

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
        "git": {"url": "https://github.com/tangle-network/sandbox-demo"},
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("change.md", "Change to retain in Git.\n")
        configured = box.exec('git config user.name "Docs Example" && git config user.email docs@example.com')
        assert configured.exit_code == 0, configured.stderr
        changed = [entry["path"] for entry in box.git.diff("HEAD").files]
        assert "change.md" in changed or "change.md" in box.git.status().untracked
        box.git.add(["change.md"])
        commit = box.git.commit("docs: retain a workspace change")
        assert commit.sha
        assert commit.message.startswith("docs: retain a workspace change")
        assert box.git.status().is_dirty is False
        print(commit.short_sha, commit.message)
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Snapshots

snapshot

snapshot(options: SnapshotOptions | None = None) -> SnapshotResult

from datetime import datetime
from tangle_sandbox import SnapshotOptions
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("capture.txt", "Workspace captured by snapshot\n")
        snapshot = box.snapshot(SnapshotOptions(tags=["capture-receipt"]))
        try:
            assert snapshot.snapshot_id
            assert isinstance(snapshot.created_at, datetime)
            assert snapshot.size_bytes is None or snapshot.size_bytes >= 0
            print(snapshot.snapshot_id, snapshot.created_at.isoformat())
        finally:
            client.fetch("DELETE", f"/v1/sandboxes/{box.id}/snapshots/{snapshot.snapshot_id}").raise_for_status()
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Saves the workspace and waits up to 600 s for the save. Returns snapshot_id, created_at, size_bytes and tags. With SnapshotOptions(storage=StorageConfig(...)) the snapshot goes to your own bucket. Request: POST /v1/sandboxes/{id}/snapshots

list_snapshots

list_snapshots() -> list[SnapshotInfo]

import time
from tangle_sandbox import SnapshotOptions
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("list.txt", "snapshot listing input\n")
        snapshot = box.snapshot(SnapshotOptions(tags=["listing-example"]))
        try:
            deadline = time.monotonic() + 60
            listed = box.list_snapshots()
            while not any(item.snapshot_id == snapshot.snapshot_id for item in listed):
                assert time.monotonic() < deadline, "Snapshot was not listed"
                time.sleep(0.5)
                listed = box.list_snapshots()
            own = next(item for item in listed if item.snapshot_id == snapshot.snapshot_id)
            assert own.sandbox_id == box.id
            assert "listing-example" in own.tags
            print(own.snapshot_id, own.tags, own.size_bytes)
        finally:
            client.fetch("DELETE", f"/v1/sandboxes/{box.id}/snapshots/{snapshot.snapshot_id}").raise_for_status()
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Lists the sandbox’s snapshots. A new snapshot takes a few seconds to appear. Request: GET /v1/sandboxes/{id}/snapshots

restore_paths

restore_paths(snapshot_id: str, paths: list[str]) -> SnapshotResult

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("src/config.json", '{"retries":3}\n')
        box.write("keep.md", "before\n")
        snapshot = box.snapshot()
        try:
            box.write("src/config.json", '{"retries":0}\n')
            box.write("keep.md", "keep this later edit\n")
            box.restore_paths(snapshot.snapshot_id, ["src"])
            assert box.read("src/config.json") == '{"retries":3}\n'
            assert box.read("keep.md") == "keep this later edit\n"
            print("Selected paths restored; other edits retained")
        finally:
            client.fetch("DELETE", f"/v1/sandboxes/{box.id}/snapshots/{snapshot.snapshot_id}").raise_for_status()
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Copies the listed workspace paths back from a snapshot into the running sandbox. Wait for the snapshot to appear in list_snapshots() first. Request: POST /v1/sandboxes/{id}/snapshots/{snapshotId}/restore

Start a sandbox from a snapshot

Pass from_snapshot with from_sandbox_id, the sandbox that took it:

import time
from tangle_sandbox import SnapshotOptions
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    owned_snapshot_id = None
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("config.json", '{"retries": 3}\n')
        clean = box.snapshot(SnapshotOptions(tags=["clean"]))
        owned_snapshot_id = clean.snapshot_id
        deadline = time.monotonic() + 60
        while clean.snapshot_id not in [item.snapshot_id for item in box.list_snapshots()]:
            assert time.monotonic() < deadline, "Snapshot did not become visible"
            time.sleep(0.5)
        box.write("config.json", '{"retries": "oops"}\n')
        box.restore_paths(clean.snapshot_id, ["config.json"])
        assert box.read("config.json") == '{"retries": 3}\n'
        copy_info = client.fetch("POST", "/v1/sandboxes", json={"agent": False, "maxLifetimeSeconds": 300, "fromSnapshot": clean.snapshot_id, "fromSandboxId": box.id}).raise_for_status().json()
        try:
            copy = client.get(copy_info["id"])
            assert copy is not None
            copy.wait_for(SandboxStatus.RUNNING)
            assert copy.read("config.json") == '{"retries": 3}\n'
            print(copy.id, copy.read("config.json"))
        finally:
            client.fetch("DELETE", f"/v1/sandboxes/{copy_info['id']}").raise_for_status()
    finally:
        try:
            if owned_snapshot_id:
                client.fetch("DELETE", f"/v1/sandboxes/{box.id}/snapshots/{owned_snapshot_id}").raise_for_status()
        finally:
            client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()
restored {"retries": 3}
copy {"retries": 3}

Reverting a whole sandbox and deleting snapshots are in the TypeScript SDK and the CLI; see Snapshots and forks.

Network rules

egress_get() -> tuple[EgressPolicy, str] egress_update(policy: EgressPolicy) -> tuple[EgressPolicy, str] egress_denials(limit: int | None = None) -> list[EgressDenial]

from tangle_sandbox import EgressPolicy
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
        "egressPolicy": {"mode": "blocked"},
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        initial, initial_source = box.egress_get()
        assert initial.mode == "blocked"
        policy, source = box.egress_update(EgressPolicy(mode="strict", allow_domains=["example.com"]))
        assert policy.mode == "strict"
        assert source == "sandbox"
        observed, observed_source = box.egress_get()
        assert observed.mode == "strict"
        assert "example.com" in observed.allow_domains
        assert observed_source == source
        assert isinstance(box.egress_denials(limit=10), list)
        print(initial_source, observed.mode, observed_source)
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

EgressPolicy(mode, allow_domains=None, include_implicit_domains=None): mode is open, strict or blocked. strict reaches the listed domains and the sandbox’s own model endpoint. The second value says where the policy comes from: sandbox, team or platform. egress_denials lists refused destinations, newest first, with host, rejected_by, count and last_at. limit is 1 to 50.

Requests: GET /v1/sandboxes/{id}, PATCH /v1/sandboxes/{id}/egress, GET /v1/sandboxes/{id}/egress/denials

from tangle_sandbox import EgressPolicy
import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
        "egressPolicy": {"mode": "blocked"},
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        blocked = box.exec("curl -sS -m 5 https://example.com")
        assert blocked.exit_code != 0
        for denial in box.egress_denials():
            print("refused", denial.host, denial.count)
        policy, source = box.egress_update(EgressPolicy(mode="strict", allow_domains=["pypi.org", "example.com"]))
        assert policy.mode == "strict"
        assert "example.com" in policy.allow_domains
        assert source == "sandbox"
        print(policy.mode, policy.allow_domains, source)
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()
refused example.com 1
strict ['pypi.org', 'example.com'] sandbox

Network access explains the modes.

SSH

ssh() -> SSHCredentials

Returns SSHCredentials(host, port, username) from the cached connection, without a request. Create the sandbox with ssh_enabled=True and ssh_public_keys or ssh_key_ids, or ssh() raises StateError. For a ready-made command, use tangle ssh from the CLI.

Traces and reports

trace(*, include_intelligence=False) -> SandboxTraceBundle
intelligence() -> dict
export_trace(url: str, *, headers=None, format="tangle", service_name="tangle-sandbox", timeout=30.0) -> dict
create_intelligence_report(*, mode="deterministic", acknowledge_cost=False, budget=None, metadata=None) -> IntelligenceReport
create_agentic_intelligence_report(*, max_usd: float, metadata=None) -> IntelligenceReport

trace() returns the sandbox’s lifecycle events, timings and critical path. intelligence() returns only the derived analysis and raises RuntimeError when there is none. export_trace posts the trace to your collector, as Tangle JSON or with format="otel-json" as OpenTelemetry JSON, and raises RuntimeError when the collector answers with an error. The report methods start a report job for this sandbox; the agentic mode is billed up to max_usd.

Requests: GET /v1/sandboxes/{id}/trace, POST /v1/intelligence/reports

direct

direct() -> SandboxInstance

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        box.write("direct.txt", "read through the runtime origin\n")
        direct = box.direct()
        assert direct.id == box.id
        assert direct.read("direct.txt") == "read through the runtime origin\n"
        result = direct.exec("python3 -c 'print(17 + 25)'")
        assert result.exit_code == 0, result.stderr
        assert result.stdout.strip() == "42"
        print("Direct runtime command:", result.stdout.strip())
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

Returns an instance whose exec, file, git and prompt calls go straight to the sandbox’s runtime with its own token, skipping the Sandbox API. Network and preview calls still go through the API. Raises StateError when the sandbox has no runtime URL.

fetch

fetch(method: str, path: str, *, json=None, params=None, headers=None, timeout=None) -> httpx.Response

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        response = box.fetch("GET", f"/v1/sandboxes/{box.id}/runtime/health", timeout=10)
        response.raise_for_status()
        health = response.json()
        assert health["status"] in ("ok", "degraded")
        assert "timestamp" in health
        print(health["status"])
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

The same raw request as client.fetch, on this instance’s connection. Use it for routes the SDK does not wrap, such as the MCP endpoint:

import os
from tangle_sandbox import Sandbox, SandboxStatus
 
with Sandbox(
    api_key=os.environ["TANGLE_API_KEY"],
    base_url="https://sandbox.tangle.tools",
) as client:
    created = client.fetch("POST", "/v1/sandboxes", json={
        "agent": False, "maxLifetimeSeconds": 600,
        "capabilities": ["computer_use"], "resources": {"memoryMB": 4096},
    }).raise_for_status().json()
    try:
        box = client.get(created["id"])
        assert box is not None
        box.wait_for(SandboxStatus.RUNNING)
        response = box.fetch("GET", f"/v1/sandboxes/{box.id}/mcp?capabilities=computer_use")
        response.raise_for_status()
        endpoint = response.json()
        assert endpoint["url"].startswith("https://")
        assert endpoint["authToken"]
        assert "computer_use" in endpoint["capabilities"]
        print(response.status_code, endpoint["capabilities"])
    finally:
        client.fetch("DELETE", f"/v1/sandboxes/{created['id']}").raise_for_status()

A sandbox created without capabilities=["computer_use"] answers 400 VALIDATION_ERROR here.

collaborate

collaborate(path: str, *, access="write") -> dict
refresh_collaboration_token(document_id: str, current_token: str) -> dict

Starts a live collaborative editing session for one file through POST /v1/collaboration/bootstrap. The hosted service at sandbox.tangle.tools does not serve that route, so collaborate() raises NotFoundError there.

Next