Skip to content

Commit 6b9482e

Browse files
Add per-command environment and working directory options
Document new env and cwd parameters for exec(), execStream(), and startProcess() methods. These options allow temporary per-command overrides of session/sandbox-level settings without persisting changes. - Updated commands.mdx with env and cwd parameters for exec() and execStream() - Added example showing usage of env and cwd options - Updated startProcess() documentation with complete options list - Updated environment-variables.mdx to include execStream() example Related PR: cloudflare/sandbox-sdk#204 Generated with Claude Code Co-Authored-By: Claude <[email protected]>
1 parent b60fa60 commit 6b9482e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/content/docs/sandbox/api/commands.mdx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const result = await sandbox.exec(command: string, options?: ExecOptions): Promi
2525
- `stream` - Enable streaming callbacks (default: `false`)
2626
- `onOutput` - Callback for real-time output: `(stream: 'stdout' | 'stderr', data: string) => void`
2727
- `timeout` - Maximum execution time in milliseconds
28+
- `env` - Environment variables for this command (temporarily override session/sandbox-level variables)
29+
- `cwd` - Working directory for this command execution
2830

2931
**Returns**: `Promise<ExecuteResponse>` with `success`, `stdout`, `stderr`, `exitCode`
3032

@@ -38,6 +40,12 @@ if (result.success) {
3840
console.error('Build failed:', result.stderr);
3941
}
4042
43+
// With custom environment and working directory
44+
await sandbox.exec('node app.js', {
45+
env: { NODE_ENV: 'production', PORT: '3000' },
46+
cwd: '/workspace/my-app'
47+
});
48+
4149
// With streaming
4250
await sandbox.exec('npm install', {
4351
stream: true,
@@ -56,7 +64,10 @@ const stream = await sandbox.execStream(command: string, options?: ExecOptions):
5664

5765
**Parameters**:
5866
- `command` - The command to execute
59-
- `options` - Same as `exec()`
67+
- `options` (optional):
68+
- `timeout` - Maximum execution time in milliseconds
69+
- `env` - Environment variables for this command (temporarily override session/sandbox-level variables)
70+
- `cwd` - Working directory for this command execution
6071

6172
**Returns**: `Promise<ReadableStream>` emitting `ExecEvent` objects (`start`, `stdout`, `stderr`, `complete`, `error`)
6273

@@ -93,8 +104,12 @@ const process = await sandbox.startProcess(command: string, options?: ProcessOpt
93104
**Parameters**:
94105
- `command` - The command to start as a background process
95106
- `options` (optional):
96-
- `cwd` - Working directory
97-
- `env` - Environment variables
107+
- `env` - Environment variables for this process (temporarily override session/sandbox-level variables)
108+
- `cwd` - Working directory for this process
109+
- `timeout` - Maximum execution time in milliseconds
110+
- `processId` - Custom process ID (auto-generated if not provided)
111+
- `encoding` - Output encoding (default: `'utf8'`)
112+
- `autoCleanup` - Automatically clean up process resources when done
98113

99114
**Returns**: `Promise<ProcessInfo>` with `id`, `pid`, `command`, `status`
100115

src/content/docs/sandbox/configuration/environment-variables.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ await sandbox.exec("node app.js", {
4242
},
4343
});
4444

45+
// Also works with execStream()
46+
await sandbox.execStream("python analyze.py", {
47+
env: {
48+
OPENAI_API_KEY: env.OPENAI_API_KEY,
49+
},
50+
});
51+
4552
// Also works with startProcess()
4653
await sandbox.startProcess("python server.py", {
4754
env: {

0 commit comments

Comments
 (0)