Sandbox-specific operations use the sandbox proxy URL:
https://<sandbox-id>.sandbox.tensorlake.aiBasic Execution
- Python
- CLI
- HTTP
Copy
Ask AI
from tensorlake.sandbox import SandboxClient
client = SandboxClient()
with client.create_and_connect() as sandbox:
result = sandbox.run("python", ["-c", "print('Hello from sandbox!')"])
print(result.stdout) # Hello from sandbox!
print(result.exit_code) # 0
Copy
Ask AI
# Run in an existing sandbox
tl sbx exec <sandbox-id> python -c 'print("Hello from sandbox!")'
# Or create, run, and tear down in one step
tl sbx run python -c 'print("Hello from sandbox!")'
Copy
Ask AI
curl -X POST https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/processes \
-H "Authorization: Bearer $TL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"command": "python",
"args": ["-c", "print(\"Hello from sandbox!\")"]
}'
201 Created):Copy
Ask AI
{
"pid": 42,
"status": "running",
"command": "python",
"args": ["-c", "print(\"Hello from sandbox!\")"],
"stdin_writable": false,
"started_at": 1710000000000
}
Shell Commands
- Python
- CLI
- HTTP
Use shell features like pipes, redirects, and command chaining:
Copy
Ask AI
with client.create_and_connect() as sandbox:
# Pipes
result = sandbox.run("bash", ["-c", "ls -la /workspace | grep '.py' | wc -l"])
print(result.stdout)
# Redirects
sandbox.run("bash", ["-c", "python script.py > output.txt 2> errors.txt"])
# Command chaining
sandbox.run("bash", ["-c", "cd /workspace && pip install -r requirements.txt && python main.py"])
Copy
Ask AI
# Pipes
tl sbx exec <sandbox-id> bash -c "ls -la /workspace | grep '.py' | wc -l"
# Redirects
tl sbx exec <sandbox-id> bash -c "python script.py > output.txt 2> errors.txt"
# Command chaining
tl sbx exec <sandbox-id> bash -c "cd /workspace && pip install -r requirements.txt && python main.py"
Copy
Ask AI
curl -X POST https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/processes \
-H "Authorization: Bearer $TL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"command": "bash",
"args": ["-c", "ls -la /workspace | grep .py | wc -l"]
}'
Get Process Output
- Python
- HTTP
Copy
Ask AI
with client.create_and_connect() as sandbox:
result = sandbox.run("python", ["-c", "print('hello')"])
print(result.stdout)
print(result.stderr)
Copy
Ask AI
# Get stdout
curl https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/processes/<pid>/stdout \
-H "Authorization: Bearer $TL_API_KEY"
# Get stderr
curl https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/processes/<pid>/stderr \
-H "Authorization: Bearer $TL_API_KEY"
# Get combined output
curl https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/processes/<pid>/output \
-H "Authorization: Bearer $TL_API_KEY"
Copy
Ask AI
{
"pid": 42,
"line_count": 1,
"lines": ["hello"]
}
Interactive Shell
- Python
- CLI
- HTTP
Copy
Ask AI
with client.create_and_connect() as sandbox:
# Create a PTY session for interactive terminal access
session = sandbox.create_pty_session(
command="/bin/bash",
rows=24,
cols=80
)
# Connect via WebSocket
ws_url = sandbox.pty_ws_url(session["session_id"], session["token"])
print(f"Terminal URL: {ws_url}")
Copy
Ask AI
# Open an interactive shell in the sandbox
tl sbx ssh <sandbox-id>
Copy
Ask AI
# 1. Create a PTY session
curl -X POST https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/pty \
-H "Authorization: Bearer $TL_API_KEY" \
-H "Content-Type: application/json" \
-d '{"command": "/bin/bash", "rows": 24, "cols": 80}'
201 Created):Copy
Ask AI
{
"session_id": "ses_abc123",
"token": "tok_xyz789"
}
Copy
Ask AI
# 2. Connect via WebSocket
wscat -c "wss://<sandbox-id>.sandbox.tensorlake.ai/api/v1/pty/<session-id>/ws?token=<token>"
Error Handling
- Python
- CLI
- HTTP
Copy
Ask AI
with client.create_and_connect() as sandbox:
result = sandbox.run("python", ["-c", "import nonexistent_module"])
if result.exit_code != 0:
print(f"Command failed with exit code {result.exit_code}")
print(f"stderr: {result.stderr}")
else:
print(f"stdout: {result.stdout}")
Copy
Ask AI
# The CLI prints stderr and returns non-zero exit codes on failure
tl sbx exec <sandbox-id> python -c "import nonexistent_module"
Copy
Ask AI
# Check exit code in process info
curl https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/processes/<pid> \
-H "Authorization: Bearer $TL_API_KEY"
Copy
Ask AI
{
"pid": 42,
"status": "exited",
"exit_code": 1,
"command": "python",
"args": ["-c", "import nonexistent_module"],
"stdin_writable": false,
"started_at": 1710000000000,
"ended_at": 1710000001000
}
Streaming Output
Stream stdout/stderr in real time for long-running commands using Server-Sent Events:- Python
- HTTP
Copy
Ask AI
with client.create_and_connect() as sandbox:
# Start a long-running process
proc = sandbox.start_process("python", ["-c", """
import time
for i in range(5):
print(f"Step {i+1}/5")
time.sleep(1)
"""])
# Stream output as it arrives
for event in sandbox.follow_output(proc.pid):
print(event.line, end="")
Copy
Ask AI
# Follow stdout via SSE
curl -N https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/processes/<pid>/stdout/follow \
-H "Authorization: Bearer $TL_API_KEY"
# Follow combined output via SSE
curl -N https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/processes/<pid>/output/follow \
-H "Authorization: Bearer $TL_API_KEY"
Copy
Ask AI
event: output
data: {"line":"Step 1/5\n","timestamp":1710000000000,"stream":"stdout"}
event: output
data: {"line":"Step 2/5\n","timestamp":1710000001000,"stream":"stdout"}
event: eof
data: {}