Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/sandbox/README.md
1# Cloudflare Sandbox SDK23Secure isolated code execution in containers on Cloudflare's edge. Run untrusted code, manage files, expose services, integrate with AI agents.45**Use cases**: AI code execution, interactive dev environments, data analysis, CI/CD, code interpreters, multi-tenant execution.67## Architecture89- Each sandbox = Durable Object + Container10- Persistent across requests (same ID = same sandbox)11- Isolated filesystem/processes/network12- Configurable sleep/wake for cost optimization1314## Quick Start1516```typescript17import { getSandbox, proxyToSandbox, type Sandbox } from '@cloudflare/sandbox';18export { Sandbox } from '@cloudflare/sandbox';1920type Env = { Sandbox: DurableObjectNamespace<Sandbox>; };2122export default {23async fetch(request: Request, env: Env): Promise<Response> {24// CRITICAL: proxyToSandbox MUST be called first for preview URLs25const proxyResponse = await proxyToSandbox(request, env);26if (proxyResponse) return proxyResponse;2728const sandbox = getSandbox(env.Sandbox, 'my-sandbox');29const result = await sandbox.exec('python3 -c "print(2 + 2)"');30return Response.json({ output: result.stdout });31}32};33```3435**wrangler.jsonc**:36```jsonc37{38"name": "my-sandbox-worker",39"main": "src/index.ts",40"compatibility_date": "2025-01-01", // Use current date for new projects4142"containers": [{43"class_name": "Sandbox",44"image": "./Dockerfile",45"instance_type": "lite", // lite | standard | heavy46"max_instances": 547}],4849"durable_objects": {50"bindings": [{ "class_name": "Sandbox", "name": "Sandbox" }]51},5253"migrations": [{54"tag": "v1",55"new_sqlite_classes": ["Sandbox"]56}]57}58```5960**Dockerfile**:61```dockerfile62FROM docker.io/cloudflare/sandbox:0.7.063RUN pip3 install --no-cache-dir pandas numpy matplotlib64EXPOSE 8080 3000 # Required for wrangler dev65```6667## Core APIs6869- `getSandbox(namespace, id, options?)` → Get/create sandbox70- `sandbox.exec(command, options?)` → Execute command71- `sandbox.readFile(path)` / `writeFile(path, content)` → File ops72- `sandbox.startProcess(command, options)` → Background process73- `sandbox.exposePort(port, options)` → Get preview URL74- `sandbox.createSession(options)` → Isolated session75- `sandbox.wsConnect(request, port)` → WebSocket proxy76- `sandbox.destroy()` → Terminate container77- `sandbox.mountBucket(bucket, path, options)` → Mount S3 storage7879## Critical Rules8081- ALWAYS call `proxyToSandbox()` first82- Same ID = reuse sandbox83- Use `/workspace` for persistent files84- `normalizeId: true` for preview URLs85- Retry on `CONTAINER_NOT_READY`8687## In This Reference88- [configuration.md](./configuration.md) - Config, CLI, environment setup89- [api.md](./api.md) - Programmatic API, testing patterns90- [patterns.md](./patterns.md) - Common workflows, CI/CD integration91- [gotchas.md](./gotchas.md) - Issues, limits, best practices9293## See Also94- [durable-objects](../durable-objects/) - Sandbox runs on DO infrastructure95- [containers](../containers/) - Container runtime fundamentals96- [workers](../workers/) - Entry point for sandbox requests97