Cloudflare Sandbox SDK adds streaming, code interpreter, Git support, process control
The @cloudflare/sandbox SDK now includes persistent Python and JavaScript code interpreters with streaming output, rich visual support, file system access, Git operations, background process control, and the ability to expose services via public URLs. This enables building AI agents, CI runners, and developer tools without managing infrastructure.
We’ve shipped a major release for the @cloudflare/sandbox ↗ SDK, turning it into a full-featured, container-based execution platform that runs securely on Cloudflare Workers.
This update adds live streaming of output, persistent Python and JavaScript code interpreters with rich output support (charts, tables, HTML, JSON), file system access, Git operations, full background process control, and the ability to expose running services via public URLs.
This makes it ideal for building AI agents, CI runners, cloud REPLs, data analysis pipelines, or full developer tools — all without managing infrastructure.
Code interpreter (Python, JS, TS)
Create persistent code contexts with support for rich visual + structured outputs.
createCodeContext(options)
Creates a new code execution context with persistent state.
// Create a Python context
const pythonCtx = await sandbox.createCodeContext({ language: "python" });
// Create a JavaScript context
const jsCtx = await sandbox.createCodeContext({ language: "javascript" });
Options:
- language: Programming language ('python' | 'javascript' | 'typescript')
- cwd: Working directory (default: /workspace)
- envVars: Environment variables for the context
runCode(code, options)
Executes code with optional streaming callbacks.
// Simple execution
const execution = await sandbox.runCode('print("Hello World")', {
context: pythonCtx,
});
// With streaming callbacks
await sandbox.runCode(
`
for i in range(5):
print(f"Step {i}")
time.sleep(1)
`,
{
context: pythonCtx,
onStdout: (output) => console.log("Real-time:", output.text),
onResult: (result) => console.log("Result:", result),
},
);
Options:
- language: Programming language ('python' | 'javascript' | 'typescript')
- cwd: Working directory (default: /workspace)
- envVars: Environment variables for the context
Real-time streaming output
Returns a streaming response for real-time processing.
const stream = await sandbox.runCodeStream(
"import time; [print(i) for i in range(10)]",
);
// Process the stream as needed
Rich output handling
Interpreter outputs are auto-formatted and returned in multiple formats:
- text
- html (e.g., Pandas tables)
- png, svg (e.g., Matplotlib charts)
- json (structured data)
- chart (parsed visualizations)
const result = await sandbox.runCode(
`
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset("flights")
pivot = data.pivot("month", "year", "passengers")
sns.heatmap(pivot, annot=True, fmt="d")
plt.title("Flight Passengers")
plt.show()
pivot.to_dict()
`,
{ context: pythonCtx },
);
if (result.png) {
console.log("Chart output:", result.png);
}
Preview URLs from Exposed Ports
Start background processes and expose them with live URLs.
await sandbox.startProcess("python -m http.server 8000");
const preview = await sandbox.exposePort(8000);
console.log("Live preview at:", preview.url);
Full process lifecycle control
Start, inspect, and terminate long-running background processes.
const process = await sandbox.startProcess("node server.js");
console.log(`Started process ${process.id} with PID ${process.pid}`);
// Monitor the process
const logStream = await sandbox.streamProcessLogs(process.id);
for await (const log of parseSSEStream<LogEvent>(logStream)) {
console.log(`Server: ${log.data}`);
}
- listProcesses() - List all running processes
- getProcess(id) - Get detailed process status
- killProcess(id, signal) - Terminate specific processes
- killAllProcesses() - Kill all processes
- streamProcessLogs(id, options) - Stream logs from running processes
- getProcessLogs(id) - Get accumulated process output
Git integration
Clone Git repositories directly into the sandbox.
await sandbox.gitCheckout("https://github.com/user/repo", {
branch: "main",
targetDir: "my-project",
});
Sandboxes are still experimental. We're using them to explore how isolated, container-like workloads might scale on Cloudflare — and to help define the developer experience around them.
Source: original entry ↗
More from Cloudflare
Follow Cloudflare to get its new changes in your feed and email digest.
Cloudflare One Client for Windows hotfix - trusted certificate chains
A new GA release for Windows Cloudflare One Client is available, addressing an issue with building trusted certificate chains.
Cloudflare One Client for macOS - Trusted Certificate Chain Fix
A new GA release of the macOS Cloudflare One Client is available. This hotfix addresses an issue with building trusted certificate chains.
Cloudflare One Client for Linux
A new GA release of Cloudflare One Client for Linux is now available. This hotfix addresses an issue with building trusted certificate chains.