sandbox-sdk

โดย Cloudflare

สร้างแอปพลิเคชันในสภาพแวดล้อมแซนด์บ็อกซ์สำหรับการรันโค้ดอย่างปลอดภัย โหลดเมื่อสร้างการรันโค้ด AI, ตัวแปลโค้ด, ระบบ CI/CD, สภาพแวดล้อมการพัฒนาที่โต้ตอบได้ หรือการรันโค้ดที่ไม่น่าเชื่อถือ ครอบคลุมวงจรชีวิตของ Sandbox SDK, คำสั่ง, ไฟล์, ตัวแปลโค้ด และ URL แสดงตัวอย่าง

npx skills add https://github.com/cloudflare/skills --skill sandbox-sdk

Cloudflare Sandbox SDK

Build secure, isolated code execution environments on Cloudflare Workers.

FIRST: Verify Installation

npm install @cloudflare/sandbox
docker info  # Must succeed - Docker required for local dev

Retrieval Sources

Your knowledge of the Sandbox SDK may be outdated. Prefer retrieval over pre-training for any Sandbox SDK task.

ResourceURL
Docshttps://developers.cloudflare.com/sandbox/
API Referencehttps://developers.cloudflare.com/sandbox/api/
Exampleshttps://github.com/cloudflare/sandbox-sdk/tree/main/examples
Get Startedhttps://developers.cloudflare.com/sandbox/get-started/

When implementing features, fetch the relevant doc page or example first.

Required Configuration

wrangler.jsonc (exact - do not modify structure):

{
  "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" }]
}

Worker entry - must re-export Sandbox class:

import { getSandbox } from '@cloudflare/sandbox';
export { Sandbox } from '@cloudflare/sandbox';  // Required export

Quick Reference

TaskMethod
Get sandboxgetSandbox(env.Sandbox, 'user-123')
Run commandawait sandbox.exec('python script.py')
Run code (interpreter)await sandbox.runCode(code, { language: 'python' })
Write fileawait sandbox.writeFile('/workspace/app.py', content)
Read fileawait sandbox.readFile('/workspace/app.py')
Create directoryawait sandbox.mkdir('/workspace/src', { recursive: true })
List filesawait sandbox.listFiles('/workspace')
Expose portawait sandbox.exposePort(8080)
Destroyawait sandbox.destroy()

Core Patterns

Execute Commands

const sandbox = getSandbox(env.Sandbox, 'user-123');
const result = await sandbox.exec('python --version');
// result: { stdout, stderr, exitCode, success }

Code Interpreter (Recommended for AI)

Use runCode() for executing LLM-generated code with rich outputs:

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"

Languages: python, javascript, typescript

State persists within context. Create explicit contexts for production.

File Operations

await 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');

When to Use What

NeedUseWhy
Shell commands, scriptsexec()Direct control, streaming
LLM-generated coderunCode()Rich outputs, state persistence
Build/test pipelinesexec()Exit codes, stderr capture
Data analysisrunCode()Charts, tables, pandas

Extending the Dockerfile

Base image (docker.io/cloudflare/sandbox:0.7.0) includes Python 3.11, Node.js 20, and common tools.

Add dependencies by extending the Dockerfile:

FROM 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 exposure

Keep images lean - affects cold start time.

Preview URLs (Port Exposure)

Expose HTTP services running in sandboxes:

const { url } = await sandbox.exposePort(8080);
// Returns preview URL for the service

Production requirement: Preview URLs need a custom domain with wildcard DNS (*.yourdomain.com). The .workers.dev domain does not support preview URL subdomains.

See: https://developers.cloudflare.com/sandbox/guides/expose-services/

OpenAI Agents SDK Integration

The SDK provides helpers for OpenAI Agents at @cloudflare/sandbox/openai:

import { Shell, Editor } from '@cloudflare/sandbox/openai';

See examples/openai-agents for complete integration pattern.

Sandbox Lifecycle

  • getSandbox() returns immediately - container starts lazily on first operation
  • Containers sleep after 10 minutes of inactivity (configurable via sleepAfter)
  • Use destroy() to immediately free resources
  • Same sandboxId always returns same sandbox instance

Anti-Patterns

  • Don't use internal clients (CommandClient, FileClient) - use sandbox.* methods
  • Don't skip the Sandbox export - Worker won't deploy without export { Sandbox }
  • Don't hardcode sandbox IDs for multi-user - use user/session identifiers
  • Don't forget cleanup - call destroy() for temporary sandboxes

Detailed References

Skills เพิ่มเติมจาก Cloudflare

agents-sdk
Cloudflare
สร้าง AI agents บน Cloudflare Workers โดยใช้ Agents SDK โหลดเมื่อสร้าง stateful agents, durable workflows, แอป WebSocket แบบเรียลไทม์, งานตามกำหนดเวลา, MCP servers หรือแอปแชท ครอบคลุม Agent class, การจัดการสถานะ, RPC ที่เรียกได้, การรวม Workflows และ React hooks
official
building-ai-agent-on-cloudflare
Cloudflare
| สร้างเอเจนต์ AI บน Cloudflare โดยใช้ Agents SDK พร้อมการจัดการสถานะ WebSocket แบบเรียลไทม์ งานตามกำหนดเวลา การรวมเครื่องมือ และความสามารถในการแชท สร้างโค้ดเอเจนต์ที่พร้อมใช้งานจริงซึ่งปรับใช้กับ Workers ใช้เมื่อ: ผู้ใช้ต้องการ "สร้างเอเจนต์", "เอเจนต์ AI", "เอเจนต์แชท", "เอเจนต์ ที่มีสถานะ", พูดถึง "Agents SDK", ต้องการ "AI แบบเรียลไทม์", "WebSocket AI", หรือถามเกี่ยวกับ "การจัดการสถานะ" ของเอเจนต์, "งานตามกำหนดเวลา" หรือ "การเรียกใช้เครื่องมือ
developmentofficial
building-mcp-server-on-cloudflare
Cloudflare
| สร้างเซิร์ฟเวอร์ MCP (Model Context Protocol) ระยะไกลบน Cloudflare Workers พร้อมเครื่องมือ การตรวจสอบสิทธิ์ OAuth และการปรับใช้ในระบบผลิต สร้างโค้ดเซิร์ฟเวอร์ กำหนดค่าผู้ให้บริการตรวจสอบสิทธิ์ และปรับใช้กับ Workers ใช้เมื่อ: ผู้ใช้ต้องการ "สร้างเซิร์ฟเวอร์ MCP", "สร้างเครื่องมือ MCP", "MCP ระยะไกล", "ปรับใช้ MCP", เพิ่ม "OAuth ให้กับ MCP" หรือกล่าวถึง Model Context Protocol บน Cloudflare นอกจากนี้ยังทำงานเมื่อมี "การตรวจสอบสิทธิ์ MCP" หรือ "การปรับใช้ MCP
developmentofficial
cloudflare
Cloudflare
ทักษะที่ครอบคลุมแพลตฟอร์ม Cloudflare ครอบคลุม Workers, Pages, พื้นที่จัดเก็บ (KV, D1, R2), AI (Workers AI, Vectorize, Agents SDK), เครือข่าย (Tunnel, Spectrum), ความปลอดภัย (WAF, DDoS) และโครงสร้างพื้นฐานเป็นโค้ด (Terraform, Pulumi) ใช้สำหรับงานพัฒนา Cloudflare ใดๆ
official
durable-objects
Cloudflare
สร้างและตรวจสอบ Cloudflare Durable Objects ใช้เมื่อสร้างการประสานงานที่มีสถานะ (ห้องแชท เกมผู้เล่นหลายคน ระบบจอง) การใช้งานเมธอด RPC พื้นที่จัดเก็บ SQLite สัญญาณเตือน WebSockets หรือตรวจสอบโค้ด DO เพื่อแนวทางปฏิบัติที่ดีที่สุด ครอบคลุมการรวม Workers การกำหนดค่า wrangler และการทดสอบด้วย Vitest
official
web-perf
Cloudflare
วิเคราะห์ประสิทธิภาพเว็บด้วย Chrome DevTools MCP วัด Core Web Vitals (FCP, LCP, TBT, CLS, Speed Index) ระบุทรัพยากรที่บล็อกการเรนเดอร์ ห่วงโซ่การพึ่งพาเครือข่าย การเลื่อนเลย์เอาต์ ปัญหาแคช และช่องว่างด้านการเข้าถึง ใช้เมื่อถูกขอให้ตรวจสอบ โปรไฟล์ แก้ไขข้อบกพร่อง หรือปรับปรุงประสิทธิภาพการโหลดหน้าเว็บ คะแนน Lighthouse หรือความเร็วไซต์
official
workers-best-practices
Cloudflare
ตรวจสอบและเขียนโค้ด Cloudflare Workers ตามแนวทางปฏิบัติที่ดีที่สุดสำหรับโปรดักชัน ใช้เมื่อเขียน Workers ใหม่ ตรวจสอบโค้ด Worker กำหนดค่า wrangler.jsonc หรือตรวจสอบรูปแบบที่ไม่เหมาะสมของ Workers ทั่วไป (การสตรีม, floating promises, global state, secrets, bindings, observability) เน้นการดึงข้อมูลจากเอกสารของ Cloudflare มากกว่าความรู้ที่ฝึกไว้ล่วงหน้า
official
wrangler
Cloudflare
Cloudflare Workers CLI สำหรับการปรับใช้ พัฒนา และจัดการ Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines และ Secrets Store โหลดก่อนรันคำสั่ง wrangler เพื่อให้แน่ใจว่าไวยากรณ์และแนวทางปฏิบัติที่ดีที่สุดถูกต้อง
official