diff --git a/src/content/docs/sandbox/api/commands.mdx b/src/content/docs/sandbox/api/commands.mdx index 324dcbe5df5a311..ddb0966aa888662 100644 --- a/src/content/docs/sandbox/api/commands.mdx +++ b/src/content/docs/sandbox/api/commands.mdx @@ -25,6 +25,8 @@ const result = await sandbox.exec(command: string, options?: ExecOptions): Promi - `stream` - Enable streaming callbacks (default: `false`) - `onOutput` - Callback for real-time output: `(stream: 'stdout' | 'stderr', data: string) => void` - `timeout` - Maximum execution time in milliseconds + - `env` - Environment variables for this command (temporarily override session/sandbox-level variables) + - `cwd` - Working directory for this command execution **Returns**: `Promise` with `success`, `stdout`, `stderr`, `exitCode` @@ -38,6 +40,12 @@ if (result.success) { console.error('Build failed:', result.stderr); } +// With custom environment and working directory +await sandbox.exec('node app.js', { + env: { NODE_ENV: 'production', PORT: '3000' }, + cwd: '/workspace/my-app' +}); + // With streaming await sandbox.exec('npm install', { stream: true, @@ -56,7 +64,9 @@ const stream = await sandbox.execStream(command: string, options?: ExecOptions): **Parameters**: - `command` - The command to execute -- `options` - Same as `exec()` +- `options` (optional): + - `env` - Environment variables for this command (temporarily override session/sandbox-level variables) + - `cwd` - Working directory for this command execution **Returns**: `Promise` emitting `ExecEvent` objects (`start`, `stdout`, `stderr`, `complete`, `error`) @@ -93,8 +103,9 @@ const process = await sandbox.startProcess(command: string, options?: ProcessOpt **Parameters**: - `command` - The command to start as a background process - `options` (optional): - - `cwd` - Working directory - - `env` - Environment variables + - `env` - Environment variables for this process (temporarily override session/sandbox-level variables) + - `cwd` - Working directory for this process + - `processId` - Custom process ID (auto-generated if not provided) **Returns**: `Promise` with `id`, `pid`, `command`, `status` diff --git a/src/content/docs/sandbox/configuration/environment-variables.mdx b/src/content/docs/sandbox/configuration/environment-variables.mdx index a92441e21390c41..6d6af5c6bab3ea5 100644 --- a/src/content/docs/sandbox/configuration/environment-variables.mdx +++ b/src/content/docs/sandbox/configuration/environment-variables.mdx @@ -42,6 +42,13 @@ await sandbox.exec("node app.js", { }, }); +// Also works with execStream() +await sandbox.execStream("python analyze.py", { + env: { + OPENAI_API_KEY: env.OPENAI_API_KEY, + }, +}); + // Also works with startProcess() await sandbox.startProcess("python server.py", { env: {