Skip to content

Commit b5ae1e0

Browse files
committed
Simplify getting started page
1 parent cf1ede0 commit b5ae1e0

File tree

1 file changed

+124
-138
lines changed

1 file changed

+124
-138
lines changed

src/content/docs/sandbox/get-started.mdx

Lines changed: 124 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -7,220 +7,206 @@ sidebar:
77

88
import { Render, PackageManagers, Steps, WranglerConfig } from "~/components";
99

10-
This guide will walk you through creating your first secure code execution environment using Sandbox SDK. You'll learn how to:
10+
Build your first application with Sandbox SDK - a secure code execution environment running on Cloudflare's global network. In this guide, you'll create a Worker that can execute Python code and work with files in isolated containers.
1111

12-
- Create a Worker that can execute code in isolated sandboxes
13-
- Run commands and capture output
14-
- Work with files in the sandbox
15-
- Deploy your application globally
16-
17-
By the end of this guide, you'll have a working sandbox that can safely execute Python code.
12+
:::note[What you're building]
13+
A simple API that can safely execute Python code and perform file operations, all running in isolated sandbox environments on Cloudflare's edge network.
14+
:::
1815

1916
## Prerequisites
2017

2118
<Render file="prereqs" product="workers" />
2219

2320
### Ensure Docker is running locally
2421

25-
Sandbox SDK uses [Docker](https://www.docker.com/) to build and push container images alongside your Worker. Docker must be running locally when you run `wrangler deploy`. The easiest way to install Docker is to follow the [Docker Desktop installation guide](https://docs.docker.com/desktop/).
22+
Sandbox SDK uses [Docker](https://www.docker.com/) to build container images alongside your Worker. Docker must be running when you deploy or run locally.
23+
24+
Install Docker by following the [Docker Desktop installation guide](https://docs.docker.com/desktop/).
2625

27-
You can verify Docker is running by executing:
26+
Verify Docker is running:
2827

2928
```sh
3029
docker info
3130
```
3231

33-
If Docker is running, the command will succeed. If not, it will hang or return "Cannot connect to the Docker daemon".
32+
If Docker is not running, this command will hang or return "Cannot connect to the Docker daemon".
3433

35-
## 1. Create your first sandbox project
34+
## 1. Create a new project
3635

37-
Create a new project using the Sandbox SDK template:
36+
Create a new Sandbox SDK project:
3837

3938
<PackageManagers
4039
type="create"
4140
pkg="cloudflare@latest"
42-
args={"my-sandbox --template=cloudflare/sandbox-sdk/examples/basic"}
41+
args={"my-sandbox --template=cloudflare/sandbox-sdk/examples/minimal"}
4342
/>
4443

45-
This creates a new `my-sandbox` directory with:
44+
This creates a `my-sandbox` directory with everything you need:
4645

47-
- A Worker at `src/index.ts` configured to handle sandbox requests
48-
- A `wrangler.jsonc` configuration file with container and Durable Objects setup
49-
- A `Dockerfile` that defines the sandbox container environment
50-
51-
Change into your new project directory:
46+
- `src/index.ts` - Worker with sandbox integration
47+
- `wrangler.jsonc` - Configuration for Workers and Containers
48+
- `Dockerfile` - Container environment definition
5249

5350
```sh
5451
cd my-sandbox
5552
```
5653

57-
## 2. Understanding the configuration
58-
59-
Your `wrangler.jsonc` file contains the configuration for both your Worker and the sandbox container:
60-
61-
<WranglerConfig>
62-
63-
```jsonc
64-
{
65-
"containers": [
66-
{
67-
"class_name": "Sandbox",
68-
"image": "./Dockerfile",
69-
"name": "sandbox",
70-
"max_instances": 1
71-
}
72-
],
73-
"durable_objects": {
74-
"bindings": [
75-
{
76-
"class_name": "Sandbox",
77-
"name": "Sandbox"
78-
}
79-
]
80-
},
81-
"migrations": [
82-
{
83-
"new_sqlite_classes": ["Sandbox"],
84-
"tag": "v1"
85-
}
86-
]
87-
}
88-
```
89-
90-
</WranglerConfig>
91-
92-
Key points:
93-
94-
- `containers` - Defines the container image and configuration
95-
- `durable_objects` - Each sandbox runs in its own Durable Object for isolation
96-
- `migrations` - Required for Durable Objects using SQLite storage
54+
## 2. Explore the template
9755

98-
## 3. Create your first sandbox Worker
99-
100-
Replace the contents of `src/index.ts` with this simple example:
56+
The template provides a minimal Worker that demonstrates core sandbox capabilities:
10157

10258
```typescript
103-
import { getSandbox, type Sandbox } from '@cloudflare/sandbox';
59+
import { getSandbox, proxyToSandbox, type Sandbox } from '@cloudflare/sandbox';
10460

10561
export { Sandbox } from '@cloudflare/sandbox';
10662

10763
type Env = {
108-
Sandbox: DurableObjectNamespace<Sandbox>;
64+
Sandbox: DurableObjectNamespace<Sandbox>;
10965
};
11066

11167
export default {
112-
async fetch(request: Request, env: Env): Promise<Response> {
113-
const url = new URL(request.url);
114-
115-
// Get a sandbox instance with a unique ID
116-
const sandbox = getSandbox(env.Sandbox, 'my-first-sandbox');
117-
118-
// Execute Python code in the sandbox
119-
if (url.pathname === '/run') {
120-
const result = await sandbox.exec('python', [
121-
'-c',
122-
'print("Hello from Sandbox SDK!")'
123-
]);
124-
125-
return Response.json({
126-
stdout: result.stdout,
127-
stderr: result.stderr,
128-
exitCode: result.exitCode,
129-
success: result.success
130-
});
131-
}
132-
133-
// Write and read a file
134-
if (url.pathname === '/file') {
135-
await sandbox.writeFile('/tmp/message.txt', 'Hello, World!');
136-
const content = await sandbox.readFile('/tmp/message.txt');
137-
138-
return Response.json({
139-
content: content,
140-
message: 'File written and read successfully'
141-
});
142-
}
143-
144-
return new Response('Try /run or /file endpoints', { status: 200 });
145-
},
68+
async fetch(request: Request, env: Env): Promise<Response> {
69+
// Required for preview URLs (if you expose ports later)
70+
const proxyResponse = await proxyToSandbox(request, env);
71+
if (proxyResponse) return proxyResponse;
72+
73+
const url = new URL(request.url);
74+
75+
// Get or create a sandbox instance
76+
const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
77+
78+
// Execute Python code
79+
if (url.pathname === '/run') {
80+
const result = await sandbox.exec('python -c "print(2 + 2)"');
81+
return Response.json({
82+
output: result.stdout,
83+
success: result.success
84+
});
85+
}
86+
87+
// Work with files
88+
if (url.pathname === '/file') {
89+
await sandbox.writeFile('/workspace/hello.txt', 'Hello, Sandbox!');
90+
const file = await sandbox.readFile('/workspace/hello.txt');
91+
return Response.json({
92+
content: file.content
93+
});
94+
}
95+
96+
return new Response('Try /run or /file');
97+
},
14698
};
14799
```
148100

149-
This Worker demonstrates two key Sandbox SDK features:
101+
**Key concepts**:
150102

151-
1. **Command Execution** - Run Python code and capture output
152-
2. **File Operations** - Write and read files in the sandbox
103+
- `getSandbox()` - Gets or creates a sandbox instance by ID
104+
- `proxyToSandbox()` - Required at the top for preview URLs to work
105+
- `sandbox.exec()` - Execute commands and capture output
106+
- `sandbox.writeFile()` / `readFile()` - File operations
153107

154-
## 4. Test locally
108+
## 3. Test locally
155109

156-
Before deploying, test your sandbox locally:
110+
Start the development server:
157111

158112
```sh
159113
npm run dev
160114
```
161115

162116
:::note
163-
The first time you run `wrangler dev`, it will build the Docker container image. This may take a few minutes. Subsequent runs will be much faster due to Docker's layer caching.
117+
First run builds the Docker container (2-3 minutes). Subsequent runs are much faster due to caching.
164118
:::
165119

166-
Once the dev server is running, open your browser and visit:
120+
Test the endpoints:
167121

168-
- `http://localhost:8787/run` - Execute Python code
169-
- `http://localhost:8787/file` - Test file operations
122+
```sh
123+
# Execute Python code
124+
curl http://localhost:8787/run
170125

171-
You should see JSON responses with the output from each operation.
126+
# File operations
127+
curl http://localhost:8787/file
128+
```
172129

173-
## 5. Deploy your sandbox
130+
You should see JSON responses with the command output and file contents.
174131

175-
Deploy your Worker and sandbox container to Cloudflare's global network:
132+
## 4. Deploy to production
133+
134+
Deploy your Worker and container:
176135

177136
```sh
178137
npx wrangler deploy
179138
```
180139

181-
When you run `wrangler deploy`:
182-
183-
1. Wrangler builds your container image using Docker
184-
2. The image is pushed to Cloudflare's Container Registry
185-
3. Your Worker is deployed with the sandbox configuration
140+
This will:
141+
1. Build your container image using Docker
142+
2. Push it to Cloudflare's Container Registry
143+
3. Deploy your Worker globally
186144

187-
:::note
188-
After your first deployment, wait 2-3 minutes for the container to be fully provisioned before making requests. During this time, the Worker is deployed but sandbox operations may error.
145+
:::caution[Wait for provisioning]
146+
After first deployment, wait 2-3 minutes before making requests. The Worker deploys immediately, but the container needs time to provision.
189147
:::
190148

191-
### Check deployment status
192-
193-
Verify your sandbox is ready:
149+
Check deployment status:
194150

195151
```sh
196152
npx wrangler containers list
197153
```
198154

199-
This shows all containers in your account and their deployment status.
200-
201-
## 6. Test your deployed sandbox
155+
## 5. Test your deployment
202156

203-
Visit your deployed Worker URL (shown in the deploy output):
157+
Visit your Worker URL (shown in deploy output):
204158

205-
```
206-
https://my-sandbox.<YOUR_SUBDOMAIN>.workers.dev/run
159+
```sh
160+
# Replace with your actual URL
161+
curl https://my-sandbox.YOUR_SUBDOMAIN.workers.dev/run
207162
```
208163

209-
You should see the same JSON response as when testing locally, but now running on Cloudflare's global network.
164+
Your sandbox is now running globally on Cloudflare's network.
165+
166+
## Understanding the configuration
167+
168+
Your `wrangler.jsonc` connects three pieces together:
169+
170+
<WranglerConfig>
171+
172+
```jsonc
173+
{
174+
"containers": [
175+
{
176+
"class_name": "Sandbox",
177+
"image": "./Dockerfile"
178+
}
179+
],
180+
"durable_objects": {
181+
"bindings": [
182+
{
183+
"class_name": "Sandbox",
184+
"name": "Sandbox"
185+
}
186+
]
187+
},
188+
"migrations": [
189+
{
190+
"new_sqlite_classes": ["Sandbox"],
191+
"tag": "v1"
192+
}
193+
]
194+
}
195+
```
210196

211-
## Summary
197+
</WranglerConfig>
212198

213-
In this guide, you:
199+
- **containers** - Your Dockerfile defines the execution environment
200+
- **durable_objects** - Makes the `Sandbox` binding available in your Worker
201+
- **migrations** - Initializes Durable Object storage (required once)
214202

215-
- Created a Worker with Sandbox SDK integration
216-
- Executed Python code in an isolated sandbox
217-
- Worked with files in the sandbox environment
218-
- Deployed your sandbox globally
203+
For detailed configuration options including environment variables, secrets, and custom images, see the [Wrangler configuration reference](/sandbox/configuration/wrangler/).
219204

220205
## Next steps
221206

222-
- Learn about [command execution](/sandbox/guides/execute-commands/) with streaming output
223-
- Explore [file operations](/sandbox/guides/manage-files/) and working with projects
224-
- Set up [preview URLs](/sandbox/guides/expose-services/) to expose services running in your sandbox
225-
- Review the [API reference](/sandbox/api/) for all available methods
226-
- Check out [examples](/sandbox/examples/) including AI code execution and data analysis
207+
Now that you have a working sandbox, explore more capabilities:
208+
209+
- [Execute commands](/sandbox/guides/execute-commands/) - Run shell commands and stream output
210+
- [Manage files](/sandbox/guides/manage-files/) - Work with files and directories
211+
- [Expose services](/sandbox/guides/expose-services/) - Get public URLs for services running in your sandbox
212+
- [API reference](/sandbox/api/) - Complete API documentation

0 commit comments

Comments
 (0)