|
| 1 | +--- |
| 2 | +title: Sandbox SDK (Beta) |
| 3 | +order: 0 |
| 4 | + |
| 5 | +pcx_content_type: overview |
| 6 | +sidebar: |
| 7 | + order: 1 |
| 8 | + badge: |
| 9 | + text: Beta |
| 10 | + group: |
| 11 | + label: sandbox |
| 12 | +head: |
| 13 | + - tag: title |
| 14 | + content: Overview |
| 15 | +--- |
| 16 | + |
| 17 | +import { |
| 18 | + CardGrid, |
| 19 | + Description, |
| 20 | + Feature, |
| 21 | + LinkTitleCard, |
| 22 | + Plan, |
| 23 | + RelatedProduct, |
| 24 | + LinkButton, |
| 25 | + Tabs, |
| 26 | + TabItem, |
| 27 | +} from "~/components"; |
| 28 | + |
| 29 | +<Description> |
| 30 | + |
| 31 | +Build secure, isolated code execution environments |
| 32 | + |
| 33 | +</Description> |
| 34 | + |
| 35 | +<Plan type="workers-paid" /> |
| 36 | + |
| 37 | +The Sandbox SDK enables you to run untrusted code securely in isolated environments. Built on [Containers](/containers/), Sandbox SDK provides a simple API for executing commands, managing files, running background processes, and exposing services — all from your [Workers](/workers/) applications. |
| 38 | + |
| 39 | +Sandboxes are ideal for building AI agents that need to execute code, interactive development environments, data analysis platforms, CI/CD systems, and any application that needs secure code execution at the edge. Each sandbox runs in its own isolated container with a full Linux environment, providing strong security boundaries while maintaining performance. |
| 40 | + |
| 41 | +With Sandbox, you can execute Python scripts, run Node.js applications, analyze data, compile code, and perform complex computations — all with a simple TypeScript API and no infrastructure to manage. |
| 42 | + |
| 43 | +<Tabs> |
| 44 | + <TabItem label="Execute Commands"> |
| 45 | + ```typescript |
| 46 | + import { getSandbox } from '@cloudflare/sandbox'; |
| 47 | + |
| 48 | + export default { |
| 49 | + async fetch(request: Request, env: Env): Promise<Response> { |
| 50 | + const sandbox = getSandbox(env.Sandbox, 'user-123'); |
| 51 | + |
| 52 | + // Execute a command and get the result |
| 53 | + const result = await sandbox.exec('python --version'); |
| 54 | + |
| 55 | + return Response.json({ |
| 56 | + output: result.stdout, |
| 57 | + exitCode: result.exitCode, |
| 58 | + success: result.success |
| 59 | + }); |
| 60 | + } |
| 61 | + }; |
| 62 | + ``` |
| 63 | + </TabItem> |
| 64 | + <TabItem label="Code Interpreter"> |
| 65 | + ```typescript |
| 66 | + import { getSandbox } from '@cloudflare/sandbox'; |
| 67 | + |
| 68 | + export default { |
| 69 | + async fetch(request: Request, env: Env): Promise<Response> { |
| 70 | + const sandbox = getSandbox(env.Sandbox, 'user-123'); |
| 71 | + |
| 72 | + // Create a Python execution context |
| 73 | + const ctx = await sandbox.createCodeContext({ language: 'python' }); |
| 74 | + |
| 75 | + // Execute Python code with automatic result capture |
| 76 | + const result = await sandbox.runCode(` |
| 77 | +import pandas as pd |
| 78 | +data = {'product': ['A', 'B', 'C'], 'sales': [100, 200, 150]} |
| 79 | +df = pd.DataFrame(data) |
| 80 | +df['sales'].sum() # Last expression is automatically returned |
| 81 | + `, { context: ctx }); |
| 82 | + |
| 83 | + return Response.json({ |
| 84 | + result: result.results?.[0]?.text, |
| 85 | + logs: result.logs |
| 86 | + }); |
| 87 | + } |
| 88 | + }; |
| 89 | + ``` |
| 90 | + </TabItem> |
| 91 | + <TabItem label="File Operations"> |
| 92 | + ```typescript |
| 93 | + import { getSandbox } from '@cloudflare/sandbox'; |
| 94 | + |
| 95 | + export default { |
| 96 | + async fetch(request: Request, env: Env): Promise<Response> { |
| 97 | + const sandbox = getSandbox(env.Sandbox, 'user-123'); |
| 98 | + |
| 99 | + // Create a project structure |
| 100 | + await sandbox.mkdir('/workspace/project/src', { recursive: true }); |
| 101 | + |
| 102 | + // Write files |
| 103 | + await sandbox.writeFile( |
| 104 | + '/workspace/project/package.json', |
| 105 | + JSON.stringify({ name: 'my-app', version: '1.0.0' }) |
| 106 | + ); |
| 107 | + |
| 108 | + // Read a file back |
| 109 | + const content = await sandbox.readFile('/workspace/project/package.json'); |
| 110 | + |
| 111 | + return Response.json({ content }); |
| 112 | + } |
| 113 | + }; |
| 114 | + ``` |
| 115 | + </TabItem> |
| 116 | +</Tabs> |
| 117 | + |
| 118 | +<LinkButton variant="primary" href="/sandbox/get-started/"> |
| 119 | + Get started |
| 120 | +</LinkButton> |
| 121 | +<LinkButton variant="secondary" href="/sandbox/api/"> |
| 122 | + API Reference |
| 123 | +</LinkButton> |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Features |
| 128 | + |
| 129 | +<Feature header="Execute commands securely" href="/sandbox/guides/execute-commands/" cta="Learn about command execution"> |
| 130 | + |
| 131 | +Run shell commands, Python scripts, Node.js applications, and more in isolated containers with streaming output support and automatic timeout handling. |
| 132 | + |
| 133 | +</Feature> |
| 134 | + |
| 135 | +<Feature header="Manage files and processes" href="/sandbox/guides/manage-files/" cta="Learn about file operations"> |
| 136 | + |
| 137 | +Read, write, and manipulate files in the sandbox filesystem. Run background processes, monitor output, and manage long-running operations. |
| 138 | + |
| 139 | +</Feature> |
| 140 | + |
| 141 | +<Feature header="Expose services with preview URLs" href="/sandbox/guides/expose-services/" cta="Learn about preview URLs"> |
| 142 | + |
| 143 | +Expose HTTP services running in your sandbox with automatically generated preview URLs, perfect for interactive development environments and application hosting. |
| 144 | + |
| 145 | +</Feature> |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## Use Cases |
| 150 | + |
| 151 | +Build powerful applications with Sandbox: |
| 152 | + |
| 153 | +### AI Code Execution |
| 154 | + |
| 155 | +Execute code generated by Large Language Models safely and reliably. Perfect for AI agents, code assistants, and autonomous systems that need to run untrusted code. |
| 156 | + |
| 157 | +### Data Analysis & Notebooks |
| 158 | + |
| 159 | +Create interactive data analysis environments with Python, pandas, and visualization libraries. Build notebook-like experiences at the edge. |
| 160 | + |
| 161 | +### Interactive Development Environments |
| 162 | + |
| 163 | +Build cloud IDEs, coding playgrounds, and collaborative development tools with full Linux environments and preview URLs. |
| 164 | + |
| 165 | +### CI/CD & Build Systems |
| 166 | + |
| 167 | +Run tests, compile code, and execute build pipelines in isolated environments with parallel execution and streaming logs. |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Related products |
| 172 | + |
| 173 | +<RelatedProduct header="Containers" href="/containers/" product="containers"> |
| 174 | + |
| 175 | +Serverless container runtime that powers Sandbox, enabling you to run any containerized workload on the edge. |
| 176 | + |
| 177 | +</RelatedProduct> |
| 178 | + |
| 179 | +<RelatedProduct header="Workers AI" href="/workers-ai/" product="workers-ai"> |
| 180 | + |
| 181 | +Run machine learning models and LLMs on the network. Combine with Sandbox for secure AI code execution workflows. |
| 182 | + |
| 183 | +</RelatedProduct> |
| 184 | + |
| 185 | +<RelatedProduct header="Durable Objects" href="/durable-objects/" product="durable-objects"> |
| 186 | + |
| 187 | +Stateful coordination layer that enables Sandbox to maintain persistent environments with strong consistency. |
| 188 | + |
| 189 | +</RelatedProduct> |
| 190 | + |
| 191 | +--- |
| 192 | + |
| 193 | +## More resources |
| 194 | + |
| 195 | +<CardGrid> |
| 196 | + |
| 197 | +<LinkTitleCard title="Examples" href="/sandbox/examples/" icon="pen"> |
| 198 | + Explore complete examples including AI code execution, data analysis, and |
| 199 | + interactive environments. |
| 200 | +</LinkTitleCard> |
| 201 | + |
| 202 | +<LinkTitleCard |
| 203 | + title="Beta Information" |
| 204 | + href="/sandbox/platform/beta-info/" |
| 205 | + icon="seti:shell" |
| 206 | +> |
| 207 | + Learn about the Sandbox Beta, current status, and upcoming features. |
| 208 | +</LinkTitleCard> |
| 209 | + |
| 210 | +<LinkTitleCard title="Limits" href="/sandbox/platform/limits/" icon="document"> |
| 211 | + Understand Sandbox resource limits, quotas, and best practices for working |
| 212 | + within them. |
| 213 | +</LinkTitleCard> |
| 214 | + |
| 215 | +<LinkTitleCard |
| 216 | + title="Best Practices" |
| 217 | + href="/sandbox/best-practices/" |
| 218 | + icon="seti:lock" |
| 219 | +> |
| 220 | + Learn security best practices, performance optimization, and production |
| 221 | + deployment patterns. |
| 222 | +</LinkTitleCard> |
| 223 | + |
| 224 | +<LinkTitleCard |
| 225 | + title="GitHub Repository" |
| 226 | + href="https://github.com/cloudflare/sandbox-sdk" |
| 227 | + icon="github" |
| 228 | +> |
| 229 | + View the SDK source code, report issues, and contribute to the project. |
| 230 | +</LinkTitleCard> |
| 231 | + |
| 232 | +<LinkTitleCard |
| 233 | + title="Developer Discord" |
| 234 | + href="https://discord.cloudflare.com" |
| 235 | + icon="discord" |
| 236 | +> |
| 237 | + Connect with the Workers community on Discord. Ask questions, share what |
| 238 | + you're building, and get help from other developers. |
| 239 | +</LinkTitleCard> |
| 240 | + |
| 241 | +</CardGrid> |
0 commit comments