Bước 1: Tạo project sandbox đầu tiên Step 1: Create Your First Sandbox Project ជំហាន 1: បង្កើត project sandbox ដំបូង
- Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
- A Cloudflare Workers project with Sandbox SDK integration, ready to run isolated code.
- Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
- Sandboxes enable safe execution of untrusted code, including AI-generated code in containers isolated from everything else.
Run the following command to scaffold a new project from the official minimal template:
Run the following command to scaffold a new project from the official minimal template:
Run the following command to scaffold a new project from the official minimal template:
npm create cloudflare@latest -- my-sandbox --template=cloudflare/sandbox-sdk/examples/minimal When prompted, choose the following options:
When prompted, choose the following options:
When prompted, choose the following options:
├ Do you want to use git for version control?
│ yes
├ Do you want to add an AGENTS.md file to help AI coding tools understand Cloudflare APIs?
│ yes
├ Do you want to deploy your application?
│ no Once scaffolding is complete, you will see the following output in your terminal:
Once scaffolding is complete, you will see the following output in your terminal:
Once scaffolding is complete, you will see the following output in your terminal:
🎉 SUCCESS Application created successfully!
💻 Continue Developing
Change directories: cd my-sandbox
Deploy: npm run deploy
📖 Explore Documentation
https://developers.cloudflare.com/workers
🐛 Report an Issue
https://github.com/cloudflare/workers-sdk/issues/new/choose
💬 Join our Community
https://discord.cloudflare.com
──────────────────────────────────────────────────────────── Next, navigate into your new project and install dependencies:
Next, navigate into your new project and install dependencies:
Next, navigate into your new project and install dependencies:
cd my-sandbox
npm install Bước 2: Xem cấu trúc project Step 2: Explore the Project Structure ជំហាន 2: មើលរចនាសម្ព័ន្ធ project
- Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
- A clear understanding of each file's purpose and how the pieces fit together.
- Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
- Knowing the structure lets you confidently customize and extend your sandbox applications.
my-sandbox/
├── src/
│ └── index.ts # Worker with sandbox integration
├── wrangler.jsonc # Cloudflare Workers + Container config
├── Dockerfile # Container definition
└── package.json # Dependencies Open wrangler.jsonc and notice the key configuration:
Open wrangler.jsonc and notice the key configuration:
Open wrangler.jsonc and notice the key configuration:
{
"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"
}
]
} The containers block tells Cloudflare to build your Dockerfile and make it available to the Sandbox Durable Object. The Durable Object manages lifecycle and state for each sandbox instance.
The containers block tells Cloudflare to build your Dockerfile and make it available to the Sandbox Durable Object. The Durable Object manages lifecycle and state for each sandbox instance.
The containers block tells Cloudflare to build your Dockerfile and make it available to the Sandbox Durable Object. The Durable Object manages lifecycle and state for each sandbox instance.
Now open src/index.ts:
Now open src/index.ts:
Now open src/index.ts:
import { getSandbox } from "@cloudflare/sandbox";
export { Sandbox } from "@cloudflare/sandbox";
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
// Get or create a sandbox instance
const sandbox = getSandbox(env.Sandbox, "my-sandbox");
if (url.pathname === "/run") {
const result = await sandbox.exec('echo "2 + 2 = $((2 + 2))"');
return Response.json({
output: result.stdout,
error: result.stderr,
exitCode: result.exitCode,
success: result.success,
});
}
return new Response("Try /run or /file");
},
}; Key concepts:
Key concepts:
Key concepts:
- getSandbox(binding, id) — Gets or creates a sandbox instance. The id lets you maintain multiple isolated environments
- Sandbox is lazy-initialized on first use — no upfront cost until you actually need it
- exec() runs a shell command and returns structured output
Bước 3: Chạy sandbox trên local Step 3: Run Your Sandbox Locally ជំហាន 3: រត់ sandbox លើ local
- Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
- A running local development environment with Docker powering your isolated container.
- Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
- Local development lets you iterate quickly and test sandbox code before deploying to production.
npm run dev ⛅️ wrangler 4.x.x
───────────────────
Your Worker has access to the following bindings:
env.Sandbox (Sandbox) — Durable Object…[wrangler:inf] Ready on http://localhost:8787 Bước 4: Chạy lệnh đầu tiên Step 4: Execute Your First Command ជំហាន 4: រត់ពាក្យបញ្ជាដំបូង
- Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
- A successful command execution inside your isolated sandbox, with structured output back to your Worker.
- Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
- This validates your entire setup and demonstrates the core sandbox capability end-to-end.
In a new terminal, run:
In a new terminal, run:
In a new terminal, run:
curl http://localhost:8787/run {
"output": "2 + 2 = 4\n",
"error": "",
"exitCode": 0,
"success": true
} - Docker not running: Open Docker Desktop and wait for it to fully start before running npm run dev
- npm not found: Install Node.js v18+ from nodejs.org
- Port 8787 busy: Use npm run dev -- --port 8788 and update your curl commands
- Container build fails: Ensure Docker has enough available memory and disk space in its settings
- Template not found: Update npm with npm install -g npm@latest and try again
✅ Your sandbox is live! You’ve created an isolated execution environment and run your first command inside it. Next, you’ll master the core APIs for commands and file operations.
✅ Your sandbox is live! You’ve created an isolated execution environment and run your first command inside it. Next, you’ll master the core APIs for commands and file operations.
✅ Your sandbox is live! You’ve created an isolated execution environment and run your first command inside it. Next, you’ll master the core APIs for commands and file operations.