Skip to content

Commit 10f0cd8

Browse files
committed
Improve template example documentation
- Use Sandbox.create() consistently in both JS and Python - Rename 'sbx' variable to 'sandbox' for clarity - Use timeout parameter instead of explicit kill() calls - Personalize section headers per template - Add descriptive prose for each section
1 parent c1df0cc commit 10f0cd8

5 files changed

Lines changed: 158 additions & 62 deletions

File tree

docs/template/examples/claude-code.mdx

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ title: "Claude Code"
33
description: "Claude Code Agent available in a sandbox"
44
---
55

6+
Run Claude Code in a sandbox to execute AI-generated code safely.
7+
8+
## Install Claude Code
9+
10+
Set up Node.js with Claude Code and the tools it needs: curl for fetching, git for repos, and ripgrep for fast code search.
611

712
<CodeGroup>
813

@@ -13,7 +18,6 @@ import { Template } from 'e2b'
1318
export const template = Template()
1419
.fromNodeImage('24')
1520
.aptInstall(['curl', 'git', 'ripgrep'])
16-
// Claude Code will be available globally as "claude"
1721
.npmInstall('@anthropic-ai/claude-code@latest', { g: true })
1822

1923
```
@@ -26,13 +30,15 @@ template = (
2630
Template()
2731
.from_node_image("24")
2832
.apt_install(["curl", "git", "ripgrep"])
29-
# Claude Code will be available globally as "claude"
3033
.npm_install("@anthropic-ai/claude-code@latest", g=True)
3134
)
3235
```
3336

3437
</CodeGroup>
3538

39+
## Build and publish
40+
41+
Build the template. Claude Code itself is lightweight, so minimal resources work fine.
3642

3743
<CodeGroup>
3844

@@ -41,7 +47,7 @@ template = (
4147
import { Template, defaultBuildLogger } from 'e2b'
4248
import { template as claudeCodeTemplate } from './template'
4349

44-
Template.build(claudeCodeTemplate, 'claude-code', {
50+
await Template.build(claudeCodeTemplate, 'claude-code', {
4551
cpuCount: 1,
4652
memoryMB: 1024,
4753
onBuildLogs: defaultBuildLogger(),
@@ -62,59 +68,45 @@ Template.build(claudeCodeTemplate, 'claude-code',
6268

6369
</CodeGroup>
6470

71+
## Run AI prompts
72+
73+
Create a sandbox with your Anthropic API key and pipe prompts to Claude. It can write files, run commands, and build whatever you describe.
74+
6575
<CodeGroup>
6676

6777
```typescript JavaScript & TypeScript
68-
// sandbox.ts
6978
import { Sandbox } from 'e2b'
7079

71-
const sbx = await Sandbox.create('claude-code', {
80+
const sandbox = await Sandbox.create('claude-code', {
81+
timeoutMs: 60_000,
7282
envs: {
7383
ANTHROPIC_API_KEY: '<your api key>',
7484
},
7585
})
7686

77-
console.log('Sandbox created', sbx.sandboxId)
78-
79-
// Print help for Claude Code
80-
// const result = await sbx.commands.run('claude --help')
81-
// console.log(result.stdout)
82-
83-
// Run a prompt with Claude Code
84-
const result = await sbx.commands.run(
87+
const result = await sandbox.commands.run(
8588
`echo 'Create a hello world index.html' | claude -p --dangerously-skip-permissions`,
8689
{ timeoutMs: 0 }
8790
)
88-
8991
console.log(result.stdout)
90-
91-
sbx.kill()
9292
```
9393

9494
```python Python
95-
# sandbox.py
9695
from e2b import Sandbox
9796

98-
sbx = Sandbox(
97+
sandbox = Sandbox.create(
9998
'claude-code',
99+
timeout=60,
100100
envs={
101101
'ANTHROPIC_API_KEY': '<your api key>',
102102
},
103103
)
104-
print("Sandbox created", sbx.sandbox_id)
105104

106-
# Print help for Claude Code
107-
# result = sbx.commands.run('claude --help')
108-
# print(result.stdout)
109-
110-
# Run a prompt with Claude Code
111-
result = sbx.commands.run(
105+
result = sandbox.commands.run(
112106
"echo 'Create a hello world index.html' | claude -p --dangerously-skip-permissions",
113107
timeout=0,
114108
)
115109
print(result.stdout)
116-
117-
sbx.kill()
118110
```
119111

120112
</CodeGroup>

docs/template/examples/desktop.mdx

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ title: "Desktop"
33
description: "Sandbox with Ubuntu Desktop and VNC access"
44
---
55

6+
Ubuntu Desktop with XFCE, accessible via noVNC on port 6080.
7+
8+
## Set up the desktop environment
9+
10+
Install XFCE with all the essentials: file manager, text editor, LibreOffice, and more. noVNC provides browser-based access without a native VNC client.
611

712
<CodeGroup>
813

@@ -12,7 +17,6 @@ import { Template, waitForPort } from 'e2b'
1217

1318
export const template = Template()
1419
.fromUbuntuImage('22.04')
15-
// Desktop environment and system utilities
1620
.runCmd([
1721
'yes | unminimize',
1822
'apt-get update',
@@ -48,13 +52,11 @@ export const template = Template()
4852
'apt-get clean',
4953
'rm -rf /var/lib/apt/lists/*',
5054
])
51-
// Streaming server setup
5255
.runCmd([
5356
'git clone --branch e2b-desktop https://github.com/e2b-dev/noVNC.git /opt/noVNC',
5457
'ln -s /opt/noVNC/vnc.html /opt/noVNC/index.html',
5558
'git clone --branch v0.12.0 https://github.com/novnc/websockify /opt/noVNC/utils/websockify',
5659
])
57-
// Set default terminal
5860
.runCmd(
5961
'ln -sf /usr/bin/xfce4-terminal.wrapper /etc/alternatives/x-terminal-emulator'
6062
)
@@ -71,8 +73,6 @@ from e2b import Template, wait_for_port
7173
template = (
7274
Template()
7375
.from_image("ubuntu:22.04")
74-
# Initial system setup and packages
75-
# We are not using .apt_install() here because some packages have interactive prompts (keyboard layout setup, etc.)
7676
.run_cmd(
7777
[
7878
"yes | unminimize",
@@ -109,22 +109,18 @@ template = (
109109
"rm -rf /var/lib/apt/lists/*",
110110
]
111111
)
112-
# Setup NoVNC and websockify
113112
.run_cmd(
114113
[
115114
"git clone --branch e2b-desktop https://github.com/e2b-dev/noVNC.git /opt/noVNC",
116115
"ln -s /opt/noVNC/vnc.html /opt/noVNC/index.html",
117116
"git clone --branch v0.12.0 https://github.com/novnc/websockify /opt/noVNC/utils/websockify",
118117
]
119118
)
120-
# Set default terminal
121119
.run_cmd(
122120
"ln -sf /usr/bin/xfce4-terminal.wrapper /etc/alternatives/x-terminal-emulator"
123121
)
124-
# Copy the start command
125122
.copy("start_command.sh", "/start_command.sh")
126123
.run_cmd("chmod +x /start_command.sh")
127-
# Set start command to launch the desktop environment
128124
.set_start_cmd("/start_command.sh", wait_for_port(6080))
129125
)
130126
```
@@ -135,27 +131,25 @@ template = (
135131
```bash start_command.sh
136132
#!/bin/bash
137133

138-
# Set display
139134
export DISPLAY=${DISPLAY:-:0}
140135

141-
# Start Xvfb
142136
Xvfb $DISPLAY -ac -screen 0 1024x768x24 -nolisten tcp &
143137
sleep 2
144138

145-
# Start XFCE session
146139
startxfce4 &
147140
sleep 5
148141

149-
# Start VNC server
150142
x11vnc -bg -display $DISPLAY -forever -wait 50 -shared -rfbport 5900 -nopw \
151143
-noxdamage -noxfixes -nowf -noscr -ping 1 -repeat -speeds lan &
152144
sleep 2
153145

154-
# Start noVNC server
155146
cd /opt/noVNC/utils && ./novnc_proxy --vnc localhost:5900 --listen 6080 --web /opt/noVNC --heartbeat 30 &
156147
sleep 2
157148
```
158149

150+
## Build and publish
151+
152+
Build the template. The desktop environment needs more resources than typical sandboxes.
159153

160154
<CodeGroup>
161155

@@ -184,3 +178,29 @@ Template.build(desktopTemplate, 'desktop',
184178
```
185179

186180
</CodeGroup>
181+
182+
## Connect to the desktop
183+
184+
Create a sandbox and open the URL in your browser. You'll see a full Ubuntu desktop running in your sandbox.
185+
186+
<CodeGroup>
187+
188+
```typescript JavaScript & TypeScript
189+
import { Sandbox } from 'e2b'
190+
191+
const sandbox = await Sandbox.create('desktop', { timeoutMs: 60_000 })
192+
193+
const url = sandbox.getHost(6080)
194+
console.log('Desktop available at:', url)
195+
```
196+
197+
```python Python
198+
from e2b import Sandbox
199+
200+
sandbox = Sandbox.create('desktop', timeout=60)
201+
202+
url = sandbox.get_host(6080)
203+
print('Desktop available at:', url)
204+
```
205+
206+
</CodeGroup>

docs/template/examples/expo.mdx

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ title: "Expo App"
33
description: "Expo web app running in the sandbox using Node.js"
44
---
55

6-
Basic Expo app.
6+
Expo app with the dev server running on port 8081.
77

8-
<Note>
9-
The development server runs on port 8081 as soon as the sandbox is ready.
10-
</Note>
8+
## Create the Expo project
9+
10+
Scaffold a fresh Expo app. The dev server starts automatically when the sandbox boots.
1111

1212
<CodeGroup>
1313
```typescript JavaScript & TypeScript
1414
// template.ts
15-
import { defaultBuildLogger, waitForURL } from "e2b";
15+
import { Template, waitForURL } from 'e2b'
1616

1717
export const template = Template()
1818
.fromNodeImage()
19-
.setWorkdir("/home/user/expo-app")
20-
.runCmd("npx create-expo-app@latest . --yes")
21-
.runCmd("mv /home/user/expo-app/* /home/user/ && rm -rf /home/user/expo-app")
22-
.setWorkdir("/home/user")
23-
.setStartCmd("npx expo start", waitForURL("http://localhost:8081"));
19+
.setWorkdir('/home/user/expo-app')
20+
.runCmd('npx create-expo-app@latest . --yes')
21+
.runCmd('mv /home/user/expo-app/* /home/user/ && rm -rf /home/user/expo-app')
22+
.setWorkdir('/home/user')
23+
.setStartCmd('npx expo start', waitForURL('http://localhost:8081'))
2424
```
2525

2626
```python Python
@@ -39,13 +39,17 @@ template = (
3939
```
4040
</CodeGroup>
4141

42+
## Build and publish
43+
44+
Build the template with enough memory for the Expo bundler.
45+
4246
<CodeGroup>
4347
```typescript JavaScript & TypeScript
4448
// build.ts
4549
import { Template, defaultBuildLogger } from 'e2b'
4650
import { template as expoTemplate } from './template'
4751

48-
Template.build(expoTemplate, 'expo-app', {
52+
await Template.build(expoTemplate, 'expo-app', {
4953
cpuCount: 4,
5054
memoryMB: 8192,
5155
onBuildLogs: defaultBuildLogger(),
@@ -64,3 +68,27 @@ Template.build(expoTemplate, 'expo-app',
6468
)
6569
```
6670
</CodeGroup>
71+
72+
## Access the dev server
73+
74+
Create a sandbox and get the URL. Open it in your browser to see the Expo web preview on port 8081.
75+
76+
<CodeGroup>
77+
```typescript JavaScript & TypeScript
78+
import { Sandbox } from 'e2b'
79+
80+
const sandbox = await Sandbox.create('expo-app', { timeoutMs: 60_000 })
81+
82+
const url = sandbox.getHost(8081)
83+
console.log('Expo app running at:', url)
84+
```
85+
86+
```python Python
87+
from e2b import Sandbox
88+
89+
sandbox = Sandbox.create('expo-app', timeout=60)
90+
91+
url = sandbox.get_host(8081)
92+
print('Expo app running at:', url)
93+
```
94+
</CodeGroup>

docs/template/examples/nextjs-bun.mdx

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ title: "Next.js App (Bun)"
33
description: "Next.js web app running in the sandbox using Bun"
44
---
55

6-
Basic Next.js app with Tailwind and shadcn UI using Bun.
6+
Next.js app with Tailwind and shadcn UI using Bun. The dev server starts automatically on port 3000.
77

8-
<Note>
9-
The development server runs on port 3000 as soon as the sandbox is ready.
10-
</Note>
8+
## Set up Next.js with Bun
9+
10+
Use Bun for faster installs and hot reloads. This template includes TypeScript, Tailwind, and all shadcn components.
1111

1212
<CodeGroup>
1313
```typescript JavaScript & TypeScript
@@ -47,13 +47,17 @@ template = (
4747
```
4848
</CodeGroup>
4949

50+
## Build and publish
51+
52+
Build the template. Bun makes the initial setup noticeably faster than npm.
53+
5054
<CodeGroup>
5155
```typescript JavaScript & TypeScript
5256
// build.ts
5357
import { Template, defaultBuildLogger } from 'e2b'
5458
import { template as nextJSTemplate } from './template'
5559

56-
Template.build(nextJSTemplate, 'nextjs-app-bun', {
60+
await Template.build(nextJSTemplate, 'nextjs-app-bun', {
5761
cpuCount: 4,
5862
memoryMB: 4096,
5963
onBuildLogs: defaultBuildLogger(),
@@ -72,3 +76,27 @@ Template.build(nextjsTemplate, 'nextjs-app-bun',
7276
)
7377
```
7478
</CodeGroup>
79+
80+
## Access your app
81+
82+
Create a sandbox and grab the URL. The dev server with Turbopack is already running on port 3000.
83+
84+
<CodeGroup>
85+
```typescript JavaScript & TypeScript
86+
import { Sandbox } from 'e2b'
87+
88+
const sandbox = await Sandbox.create('nextjs-app-bun', { timeoutMs: 60_000 })
89+
90+
const url = sandbox.getHost(3000)
91+
console.log('Next.js app running at:', url)
92+
```
93+
94+
```python Python
95+
from e2b import Sandbox
96+
97+
sandbox = Sandbox.create('nextjs-app-bun', timeout=60)
98+
99+
url = sandbox.get_host(3000)
100+
print('Next.js app running at:', url)
101+
```
102+
</CodeGroup>

0 commit comments

Comments
 (0)