Skip to main content
sbx.commands is the Commands instance available on every Sandbox. It provides methods to run commands, stream output, and manage processes.

sbx.commands.run()

Run a command in the sandbox. Returns a CommandResult when run in the foreground (default), or a CommandHandle when background: true.
string
required
Shell command to execute inside the sandbox.
RunOpts
Optional run configuration.

RunOpts

boolean
default:"false"
When true, returns a CommandHandle immediately. When false (default), blocks and returns CommandResult.
Record<string, string>
Environment variables for the command.
string
default:"'user'"
Unix user to run the command as.
string
Working directory.
number
default:"60"
Command execution timeout in seconds.
number
Per-request HTTP timeout in milliseconds.
(line: string) => void
Callback invoked for each stdout line after the command completes (foreground only). For real-time output, use runStream().
(line: string) => void
Callback invoked for each stderr line after the command completes.
Returns Promise<CommandResult> (foreground) or Promise<CommandHandle> (background)

sbx.commands.runStream()

Run a command with real-time SSE streaming. Callbacks are invoked as each chunk of output arrives via Server-Sent Events.
string
required
Shell command to execute.
RunStreamOpts
Optional streaming configuration.

RunStreamOpts

Record<string, string>
Environment variables for the command.
string
default:"'user'"
Unix user to run as.
string
Working directory.
number
default:"60"
Command execution timeout in seconds.
(line: string) => void
Called in real-time for each stdout chunk as it arrives.
(line: string) => void
Called in real-time for each stderr chunk as it arrives.
Returns Promise<CommandResult> with the accumulated stdout and stderr.

sbx.commands.list()

List all running processes in the sandbox.
number
Per-request HTTP timeout in milliseconds.
Returns Promise<ProcessInfo[]>

sbx.commands.kill()

Send SIGKILL to a process by PID.
number
required
Process ID to kill.
number
Per-request HTTP timeout in milliseconds.
Returns Promise<boolean>true if killed, false if already dead.

sbx.commands.sendStdin()

Write data to the stdin of a running process.
number
required
Process ID of the running command.
string
required
Data to write to stdin. Include \n for newlines.
number
Per-request HTTP timeout in milliseconds.
Returns Promise<void>

sbx.commands.connect()

Create a CommandHandle for an already-running process by PID without making an API call.
number
required
Process ID of the running command.
Returns CommandHandle (synchronous, no API call)

CommandHandle

Returned by commands.run({ background: true }) and commands.connect().

handle.wait()

Wait for the background command to complete.
(line: string) => void
Called for each stdout line.
(line: string) => void
Called for each stderr line.
Returns Promise<CommandResult>. Throws CommandExitError if exit code is non-zero.

handle.kill()

Kill the process. Returns Promise<boolean>

handle.disconnect()

Disconnect from the handle (currently a no-op; reserved for future WebSocket support). Returns void

handle.pid

Type number — the process ID.

Data models

CommandResult

ProcessInfo

CommandWaitOpts


Examples

Run a Node.js script

Run with environment variables

Background process with stdin

Stream long-running output