|
| 1 | +--- |
| 2 | +title: Getting started |
| 3 | +pcx_content_type: get-started |
| 4 | +sidebar: |
| 5 | + order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Render, PackageManagers, Steps, WranglerConfig } from "~/components"; |
| 9 | + |
| 10 | +This guide will walk you through creating your first secure code execution environment using Sandbox SDK. You'll learn how to: |
| 11 | + |
| 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. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +<Render file="prereqs" product="workers" /> |
| 22 | + |
| 23 | +### Ensure Docker is running locally |
| 24 | + |
| 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/). |
| 26 | + |
| 27 | +You can verify Docker is running by executing: |
| 28 | + |
| 29 | +```sh |
| 30 | +docker info |
| 31 | +``` |
| 32 | + |
| 33 | +If Docker is running, the command will succeed. If not, it will hang or return "Cannot connect to the Docker daemon". |
| 34 | + |
| 35 | +## 1. Create your first sandbox project |
| 36 | + |
| 37 | +Create a new project using the Sandbox SDK template: |
| 38 | + |
| 39 | +<PackageManagers |
| 40 | + type="create" |
| 41 | + pkg="cloudflare@latest" |
| 42 | + args={"my-sandbox --template=cloudflare/sandbox-sdk/examples/basic"} |
| 43 | +/> |
| 44 | + |
| 45 | +This creates a new `my-sandbox` directory with: |
| 46 | + |
| 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: |
| 52 | + |
| 53 | +```sh |
| 54 | +cd my-sandbox |
| 55 | +``` |
| 56 | + |
| 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 |
| 97 | + |
| 98 | +## 3. Create your first sandbox Worker |
| 99 | + |
| 100 | +Replace the contents of `src/index.ts` with this simple example: |
| 101 | + |
| 102 | +```typescript |
| 103 | +import { getSandbox, type Sandbox } from '@cloudflare/sandbox'; |
| 104 | + |
| 105 | +export { Sandbox } from '@cloudflare/sandbox'; |
| 106 | + |
| 107 | +type Env = { |
| 108 | + Sandbox: DurableObjectNamespace<Sandbox>; |
| 109 | +}; |
| 110 | + |
| 111 | +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 | + }, |
| 146 | +}; |
| 147 | +``` |
| 148 | + |
| 149 | +This Worker demonstrates two key Sandbox SDK features: |
| 150 | + |
| 151 | +1. **Command Execution** - Run Python code and capture output |
| 152 | +2. **File Operations** - Write and read files in the sandbox |
| 153 | + |
| 154 | +## 4. Test locally |
| 155 | + |
| 156 | +Before deploying, test your sandbox locally: |
| 157 | + |
| 158 | +```sh |
| 159 | +npm run dev |
| 160 | +``` |
| 161 | + |
| 162 | +:::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. |
| 164 | +::: |
| 165 | + |
| 166 | +Once the dev server is running, open your browser and visit: |
| 167 | + |
| 168 | +- `http://localhost:8787/run` - Execute Python code |
| 169 | +- `http://localhost:8787/file` - Test file operations |
| 170 | + |
| 171 | +You should see JSON responses with the output from each operation. |
| 172 | + |
| 173 | +## 5. Deploy your sandbox |
| 174 | + |
| 175 | +Deploy your Worker and sandbox container to Cloudflare's global network: |
| 176 | + |
| 177 | +```sh |
| 178 | +npx wrangler deploy |
| 179 | +``` |
| 180 | + |
| 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 |
| 186 | + |
| 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. |
| 189 | +::: |
| 190 | + |
| 191 | +### Check deployment status |
| 192 | + |
| 193 | +Verify your sandbox is ready: |
| 194 | + |
| 195 | +```sh |
| 196 | +npx wrangler containers list |
| 197 | +``` |
| 198 | + |
| 199 | +This shows all containers in your account and their deployment status. |
| 200 | + |
| 201 | +## 6. Test your deployed sandbox |
| 202 | + |
| 203 | +Visit your deployed Worker URL (shown in the deploy output): |
| 204 | + |
| 205 | +``` |
| 206 | +https://my-sandbox.<YOUR_SUBDOMAIN>.workers.dev/run |
| 207 | +``` |
| 208 | + |
| 209 | +You should see the same JSON response as when testing locally, but now running on Cloudflare's global network. |
| 210 | + |
| 211 | +## Summary |
| 212 | + |
| 213 | +In this guide, you: |
| 214 | + |
| 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 |
| 219 | + |
| 220 | +## Next steps |
| 221 | + |
| 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 |
0 commit comments