|
| 1 | +--- |
| 2 | +title: Cloudflare Sandbox SDK adds streaming, code interpreter, Git support, process control and more |
| 3 | +description: The latest release transforms the Sandbox SDK into a full edge-native code execution platform, enabling secure, containerized runtimes with real-time streaming, language interpreters, Git integration, process lifecycle management, and port forwarding. |
| 4 | +products: |
| 5 | + - agents |
| 6 | + - workers |
| 7 | +date: 2025-08-05 |
| 8 | +--- |
| 9 | + |
| 10 | +We’ve shipped a major release for the [@cloudflare/sandbox](https://github.com/cloudflare/sandbox-sdk) SDK, turning it into a full-featured, container-based execution platform that runs securely on Cloudflare Workers. |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +This makes it ideal for building AI agents, CI runners, cloud REPLs, data analysis pipelines, or full developer tools — all without managing infrastructure. |
| 15 | + |
| 16 | +### Code interpreter (Python, JS, TS) |
| 17 | + |
| 18 | +Create persistent code contexts with support for rich visual + structured outputs. |
| 19 | + |
| 20 | +#### createCodeContext(options) |
| 21 | + |
| 22 | +Creates a new code execution context with persistent state. |
| 23 | + |
| 24 | +```ts |
| 25 | +// Create a Python context |
| 26 | +const pythonCtx = await sandbox.createCodeContext({ language: "python" }); |
| 27 | + |
| 28 | +// Create a JavaScript context |
| 29 | +const jsCtx = await sandbox.createCodeContext({ language: "javascript" }); |
| 30 | +``` |
| 31 | + |
| 32 | +Options: |
| 33 | + |
| 34 | +- language: Programming language ('python' | 'javascript' | 'typescript') |
| 35 | +- cwd: Working directory (default: /workspace) |
| 36 | +- envVars: Environment variables for the context |
| 37 | + |
| 38 | +#### runCode(code, options) |
| 39 | + |
| 40 | +Executes code with optional streaming callbacks. |
| 41 | + |
| 42 | +```ts |
| 43 | +// Simple execution |
| 44 | +const execution = await sandbox.runCode('print("Hello World")', { |
| 45 | + context: pythonCtx, |
| 46 | +}); |
| 47 | + |
| 48 | +// With streaming callbacks |
| 49 | +await sandbox.runCode( |
| 50 | + ` |
| 51 | +for i in range(5): |
| 52 | + print(f"Step {i}") |
| 53 | + time.sleep(1) |
| 54 | +`, |
| 55 | + { |
| 56 | + context: pythonCtx, |
| 57 | + onStdout: (output) => console.log("Real-time:", output.text), |
| 58 | + onResult: (result) => console.log("Result:", result), |
| 59 | + }, |
| 60 | +); |
| 61 | +``` |
| 62 | + |
| 63 | +Options: |
| 64 | + |
| 65 | +- language: Programming language ('python' | 'javascript' | 'typescript') |
| 66 | +- cwd: Working directory (default: /workspace) |
| 67 | +- envVars: Environment variables for the context |
| 68 | + |
| 69 | +#### Real-time streaming output |
| 70 | + |
| 71 | +Returns a streaming response for real-time processing. |
| 72 | + |
| 73 | +```ts |
| 74 | +const stream = await sandbox.runCodeStream( |
| 75 | + "import time; [print(i) for i in range(10)]", |
| 76 | +); |
| 77 | +// Process the stream as needed |
| 78 | +``` |
| 79 | + |
| 80 | +#### Rich output handling |
| 81 | + |
| 82 | +Interpreter outputs are auto-formatted and returned in multiple formats: |
| 83 | + |
| 84 | +- text |
| 85 | +- html (e.g., Pandas tables) |
| 86 | +- png, svg (e.g., Matplotlib charts) |
| 87 | +- json (structured data) |
| 88 | +- chart (parsed visualizations) |
| 89 | + |
| 90 | +```ts |
| 91 | +const result = await sandbox.runCode( |
| 92 | + ` |
| 93 | +import seaborn as sns |
| 94 | +import matplotlib.pyplot as plt |
| 95 | +
|
| 96 | +data = sns.load_dataset("flights") |
| 97 | +pivot = data.pivot("month", "year", "passengers") |
| 98 | +sns.heatmap(pivot, annot=True, fmt="d") |
| 99 | +plt.title("Flight Passengers") |
| 100 | +plt.show() |
| 101 | +
|
| 102 | +pivot.to_dict() |
| 103 | +`, |
| 104 | + { context: pythonCtx }, |
| 105 | +); |
| 106 | + |
| 107 | +if (result.png) { |
| 108 | + console.log("Chart output:", result.png); |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### Preview URLs from Exposed Ports |
| 113 | + |
| 114 | +Start background processes and expose them with live URLs. |
| 115 | + |
| 116 | +```ts |
| 117 | +await sandbox.startProcess("python -m http.server 8000"); |
| 118 | +const preview = await sandbox.exposePort(8000); |
| 119 | + |
| 120 | +console.log("Live preview at:", preview.url); |
| 121 | +``` |
| 122 | + |
| 123 | +### Full process lifecycle control |
| 124 | + |
| 125 | +Start, inspect, and terminate long-running background processes. |
| 126 | + |
| 127 | +```ts |
| 128 | +const process = await sandbox.startProcess("node server.js"); |
| 129 | +console.log(`Started process ${process.id} with PID ${process.pid}`); |
| 130 | + |
| 131 | +// Monitor the process |
| 132 | +const logStream = await sandbox.streamProcessLogs(process.id); |
| 133 | +for await (const log of parseSSEStream<LogEvent>(logStream)) { |
| 134 | + console.log(`Server: ${log.data}`); |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +- listProcesses() - List all running processes |
| 139 | +- getProcess(id) - Get detailed process status |
| 140 | +- killProcess(id, signal) - Terminate specific processes |
| 141 | +- killAllProcesses() - Kill all processes |
| 142 | +- streamProcessLogs(id, options) - Stream logs from running processes |
| 143 | +- getProcessLogs(id) - Get accumulated process output |
| 144 | + |
| 145 | +### Git integration |
| 146 | + |
| 147 | +Clone Git repositories directly into the sandbox. |
| 148 | + |
| 149 | +```ts |
| 150 | +await sandbox.gitCheckout("https://github.com/user/repo", { |
| 151 | + branch: "main", |
| 152 | + targetDir: "my-project", |
| 153 | +}); |
| 154 | +``` |
| 155 | + |
| 156 | +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. |
0 commit comments