sandbox-sdk

द्वारा Cloudflare

सैंडबॉक्स्ड एप्लिकेशन बनाएं सुरक्षित कोड निष्पादन के लिए। AI कोड निष्पादन, कोड इंटरप्रेटर, CI/CD सिस्टम, इंटरैक्टिव डेव एनवायरनमेंट या अविश्वसनीय कोड निष्पादित करते समय लोड करें। इसमें सैंडबॉक्स 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

Cloudflare की और Skills

agents-sdk
Cloudflare
Cloudflare Workers पर Agents SDK का उपयोग करके AI एजेंट बनाएं। स्टेटफुल एजेंट, ड्यूरेबल वर्कफ़्लो, रीयल-टाइम WebSocket ऐप, शेड्यूल किए गए कार्य, MCP सर्वर या चैट एप्लिकेशन बनाते समय लोड करें। इसमें Agent क्लास, स्टेट प्रबंधन, कॉल करने योग्य RPC, Workflows एकीकरण और React हुक शामिल हैं।
official
building-ai-agent-on-cloudflare
Cloudflare
Cloudflare पर Agents SDK का उपयोग करके AI एजेंट बनाता है, जिसमें स्थिति प्रबंधन, रीयल-टाइम WebSockets, निर्धारित कार्य, उपकरण एकीकरण और चैट क्षमताएं शामिल हैं। Workers पर तैनात करने के लिए उत्पादन-तैयार एजेंट कोड उत्पन्न करता है। उपयोग करें जब: उपयोगकर्ता "एजेंट बनाएं", "AI एजेंट", "चैट एजेंट", "स्थिति-युक्त एजेंट" चाहता है, "Agents SDK" का उल्लेख करता है, "रीयल-टाइम AI", "WebSocket AI" की आवश्यकता है,
developmentofficial
building-mcp-server-on-cloudflare
Cloudflare
Cloudflare Workers पर रिमोट MCP (मॉडल कॉन्टेक्स्ट प्रोटोकॉल) सर्वर बनाता है, जिसमें टूल्स, OAuth प्रमाणीकरण और प्रोडक्शन डिप्लॉयमेंट शामिल हैं। सर्वर कोड जनरेट करता है, प्रमाणीकरण प्रदाताओं को कॉन्फ़िगर करता है और Workers पर डिप्लॉय करता है। उपयोग तब करें जब: उपयोगकर्ता "MCP सर्वर बनाएं", "MCP टूल्स बनाएं", "रिमोट MCP", "MCP डिप्लॉय करें", "MCP में OAuth जोड़ें" या Cloudflare पर मॉडल कॉन्टेक्स्ट प्रोटो
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
क्रोम DevTools MCP का उपयोग करके वेब प्रदर्शन का विश्लेषण करता है। कोर वेब वाइटल्स (FCP, LCP, TBT, CLS, स्पीड इंडेक्स) को मापता है, रेंडर-ब्लॉकिंग संसाधनों, नेटवर्क निर्भरता श्रृंखलाओं, लेआउट शिफ्ट्स, कैशिंग समस्याओं और पहुंच संबंधी अंतरालों की पहचान करता है। जब पेज लोड प्रदर्शन, Lighthouse स्कोर या साइट स्पीड का ऑडिट, प्रोफाइल, डीबग या ऑप्टिमाइज़ करने के लिए क
official
workers-best-practices
Cloudflare
Cloudflare Workers कोड की समीक्षा करता है और उसे उत्पादन सर्वोत्तम प्रथाओं के अनुसार लिखता है। नए Workers लिखते समय, Worker कोड की समीक्षा करते समय, wrangler.jsonc कॉन्फ़िगर करते समय, या सामान्य Workers एंटी-पैटर्न (स्ट्रीमिंग, फ्लोटिंग प्रॉमिसेस, ग्लोबल स्टेट, सीक्रेट्स, बाइंडिंग्स, ऑब्जर्वेबिलिटी) की जाँच करते समय लोड करें। पूर्व-प्रशिक्षित ज्ञान की तुलना में Cloudflare दस्तावेज़ों से पुनर्प्राप्ति को प्राथमिकता देता है।
official
wrangler
Cloudflare
Cloudflare Workers CLI जो Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines और Secrets Store को डिप्लॉय, डेवलप और प्रबंधित करने के लिए है। wrangler कमांड चलाने से पहले लोड करें ताकि सही सिंटैक्स और सर्वोत्तम प्रथाओं को सुनिश्चित किया जा सके।
official