Loading...
Loading...
Build sandboxed applications for secure code execution. Load when building AI code execution, code interpreters, CI/CD systems, interactive dev environments, or executing untrusted code. Covers Sandbox SDK lifecycle, commands, files, code interpreter, and preview URLs.
npx skill4agent add cloudflare/skills sandbox-sdknpm install @cloudflare/sandbox
docker info # Must succeed - Docker required for local dev{
"containers": [{
"class_name": "Sandbox",
"image": "./Dockerfile",
"instance_type": "lite",
"max_instances": 1
}],
"durable_objects": {
"bindings": [{ "class_name": "Sandbox", "name": "Sandbox" }]
},
"migrations": [{ "new_sqlite_classes": ["Sandbox"], "tag": "v1" }]
}import { getSandbox } from '@cloudflare/sandbox';
export { Sandbox } from '@cloudflare/sandbox'; // Required export| Task | Method |
|---|---|
| Get sandbox | |
| Run command | |
| Run code (interpreter) | |
| Write file | |
| Read file | |
| Create directory | |
| List files | |
| Expose port | |
| Destroy | |
const sandbox = getSandbox(env.Sandbox, 'user-123');
const result = await sandbox.exec('python --version');
// result: { stdout, stderr, exitCode, success }runCode()const ctx = await sandbox.createCodeContext({ language: 'python' });
await sandbox.runCode('import pandas as pd; data = [1,2,3]', { context: ctx });
const result = await sandbox.runCode('sum(data)', { context: ctx });
// result.results[0].text = "6"pythonjavascripttypescriptawait sandbox.mkdir('/workspace/project', { recursive: true });
await sandbox.writeFile('/workspace/project/main.py', code);
const file = await sandbox.readFile('/workspace/project/main.py');
const files = await sandbox.listFiles('/workspace/project');| Need | Use | Why |
|---|---|---|
| Shell commands, scripts | | Direct control, streaming |
| LLM-generated code | | Rich outputs, state persistence |
| Build/test pipelines | | Exit codes, stderr capture |
| Data analysis | | Charts, tables, pandas |
docker.io/cloudflare/sandbox:0.7.0FROM docker.io/cloudflare/sandbox:0.7.0
# Python packages
RUN pip install requests beautifulsoup4
# Node packages (global)
RUN npm install -g typescript
# System packages
RUN apt-get update && apt-get install -y ffmpeg && rm -rf /var/lib/apt/lists/*
EXPOSE 8080 # Required for local dev port exposureconst { url } = await sandbox.exposePort(8080);
// Returns preview URL for the service*.yourdomain.com.workers.dev@cloudflare/sandbox/openaiimport { Shell, Editor } from '@cloudflare/sandbox/openai';examples/openai-agentsgetSandbox()sleepAfterdestroy()sandboxIdCommandClientFileClientsandbox.*export { Sandbox }destroy()