Skip to content

Commit 084eb8a

Browse files
Document per-command env and cwd options for exec methods
Add documentation for new per-command environment variables and working directory options in exec() and execStream() methods. These options allow temporary configuration that does not persist in session state. Includes examples showing: - Setting per-command environment variables - Specifying working directory per command - Demonstrating that values do not persist across commands Synced from cloudflare/sandbox-sdk PR #204 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 832c0ad commit 084eb8a

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { Details, TypeScriptExample } from "~/components";
99

1010
Execute commands and manage background processes in the sandbox's isolated container environment.
1111

12+
:::note[Per-command vs Session-level options]
13+
Commands support per-command `env` and `cwd` options that apply only to that specific command execution. These do not persist in the session state. For persistent environment variables and working directories, use [session-level configuration](/sandbox/api/sessions/) with `createSession()`.
14+
:::
15+
1216
## Methods
1317

1418
### `exec()`
@@ -25,6 +29,8 @@ const result = await sandbox.exec(command: string, options?: ExecOptions): Promi
2529
- `stream` - Enable streaming callbacks (default: `false`)
2630
- `onOutput` - Callback for real-time output: `(stream: 'stdout' | 'stderr', data: string) => void`
2731
- `timeout` - Maximum execution time in milliseconds
32+
- `env` - Environment variables for this command only (does not persist)
33+
- `cwd` - Working directory for this command only (does not persist)
2834

2935
**Returns**: `Promise<ExecuteResponse>` with `success`, `stdout`, `stderr`, `exitCode`
3036

@@ -43,6 +49,22 @@ await sandbox.exec('npm install', {
4349
stream: true,
4450
onOutput: (stream, data) => console.log(`[${stream}] ${data}`)
4551
});
52+
53+
// With per-command environment variables and directory
54+
await sandbox.exec('npm run build', {
55+
env: { NODE_ENV: 'production', API_URL: 'https://api.example.com' },
56+
cwd: '/workspace/my-project'
57+
});
58+
59+
// Environment variables are scoped to this command only
60+
const result = await sandbox.exec('echo $TEMP_VAR', {
61+
env: { TEMP_VAR: 'temporary-value' }
62+
});
63+
console.log(result.stdout); // "temporary-value"
64+
65+
// TEMP_VAR is not available in subsequent commands
66+
const verify = await sandbox.exec('echo $TEMP_VAR');
67+
console.log(verify.stdout); // Empty - variable does not persist
4668
```
4769
</TypeScriptExample>
4870

@@ -56,7 +78,10 @@ const stream = await sandbox.execStream(command: string, options?: ExecOptions):
5678

5779
**Parameters**:
5880
- `command` - The command to execute
59-
- `options` - Same as `exec()`
81+
- `options` (optional):
82+
- `timeout` - Maximum execution time in milliseconds
83+
- `env` - Environment variables for this command only (does not persist)
84+
- `cwd` - Working directory for this command only (does not persist)
6085

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

@@ -79,6 +104,19 @@ for await (const event of parseSSEStream<ExecEvent>(stream)) {
79104
break;
80105
}
81106
}
107+
108+
// With per-command options
109+
const stream = await sandbox.execStream('python train.py', {
110+
env: { MODEL_PATH: '/workspace/models', BATCH_SIZE: '32' },
111+
cwd: '/workspace/ml-project',
112+
timeout: 300000 // 5 minutes
113+
});
114+
115+
for await (const event of parseSSEStream<ExecEvent>(stream)) {
116+
if (event.type === 'stdout') {
117+
console.log('Training:', event.data);
118+
}
119+
}
82120
```
83121
</TypeScriptExample>
84122

0 commit comments

Comments
 (0)