For the complete documentation index, see llms.txt. This page is also available as Markdown.

Sandbox (interface)

Overview

Methods available for interacting with a compute sandbox.


runCommand(command, options?)

Execute shell commands in the sandbox with full control over execution environment.

Parameters:

  • command (string, required): The shell command to execute as a single string

  • options (RunCommandOptions, optional): Execution options

    • cwd (string, optional): Working directory for command execution

    • env (Record<string, string>, optional): Environment variables to set

    • timeout (number, optional): Command timeout in milliseconds

    • background (boolean, optional): Run command in background without waiting for completion

    • onStdout ((data: string) => void, optional): Callback invoked with stdout chunks as the command runs

    • onStderr ((data: string) => void, optional): Callback invoked with stderr chunks as the command runs

Returns: Promise<CommandResult> - Command execution result with output streams, exit code, and duration

CommandResult interface:

  • stdout (string): Standard output from the command

  • stderr (string): Standard error output from the command

  • exitCode (number): Exit code (0 for success, non-zero for errors)

  • durationMs (number): Command execution duration in milliseconds

Examples:

Notes:

  • Commands are executed as a single string, not as separate command + arguments arrays

  • Use shell operators (&&, ||, |, etc.) within the command string for complex operations

  • Non-zero exit codes indicate command failure but do not throw errors - check exitCode in the result

  • Background commands return immediately with exitCode: 0 without waiting for completion

  • The command runs in a shell context, so all shell features (pipes, redirects, etc.) are available

  • Passing onStdout / onStderr enables incremental streaming of command output. The returned CommandResult still contains the complete stdout and stderr once the command finishes

  • Streaming callbacks cannot be combined with background: true β€” doing so throws

  • Available on all sandbox instances regardless of provider

Streaming output with onStdout / onStderr

By default runCommand buffers all output and only resolves once the command finishes. Pass onStdout and/or onStderr to receive output incrementally as the command runs β€” useful for long-running commands (installs, builds, dev servers) where you want to surface progress instead of waiting for completion.

Both are optional callbacks with the signature (data: string) => void. Each fires once per output chunk delivered as the command streams, in order, while the command executes.

Accumulate streamed chunks yourself, e.g. to push progress to a UI:

Behavior:

  • The callbacks are additive, not a replacement. The returned CommandResult still contains the full stdout and stderr once the command finishes, regardless of whether you passed callbacks.

  • Chunk boundaries are not guaranteed. A chunk may contain a partial line, multiple lines, or a mix β€” do not assume one callback invocation equals one line. Buffer and split on \n yourself if you need line-by-line handling.

  • Incremental delivery depends on the sandbox environment. Streaming is handled by a built-in in-sandbox daemon, not by each provider β€” the command always runs and the full output is always returned in the result. But if the environment can't stream live chunks, the callbacks fall back to firing once when the command completes, carrying the full output, rather than incrementally.

  • Cannot be combined with background: true. Passing streaming callbacks together with background: true throws, since a backgrounded command returns immediately and has no output to stream.


getInfo()

Get information about the sandbox including status, provider, and metadata.

Parameters: None

Returns: Promise<SandboxInfo> - Sandbox information including status and configuration

SandboxInfo interface:

  • id (string): Unique identifier for the sandbox

  • provider (string): Provider hosting the sandbox (e.g., 'e2b', 'modal', 'docker')

  • status (string): Current sandbox status ('running' | 'stopped' | 'error')

  • createdAt (Date): Timestamp when the sandbox was created

  • timeout (number): Execution timeout in milliseconds

  • metadata (Record<string, any>, optional): Additional provider-specific metadata

Examples:

Notes:

  • Returns information about the sandbox's current state and configuration

  • May return locally cached information depending on provider implementation

  • The metadata field contains any custom metadata set during sandbox creation

  • Available on all sandbox instances regardless of provider


getUrl(options)

Get a publicly accessible URL for accessing services running on a specific port in the sandbox.

Parameters:

  • options (object, required): URL configuration options

    • port (number, required): Port number where the service is running in the sandbox

    • protocol (string, optional): Protocol to use ('http' | 'https'). Defaults to 'https'

Returns: Promise<string> - Publicly accessible URL for the specified port

Examples:

Notes:

  • Returns a publicly accessible URL that routes to the specified port in your sandbox

  • Whether a network call is needed depends on the provider β€” some construct the URL synchronously from the sandbox host (e.g. E2B), while others resolve it via an API call (e.g. Modal tunnels, Daytona preview links). Always await the result

  • The service must be running on the specified port for the URL to be accessible


sandbox.filesystem

ComputeSDK provides filesystem operations for managing files and directories within sandboxes. All filesystem operations are accessed through the sandbox.filesystem object.

filesystem.readFile(path)

Read the contents of a file from the sandbox filesystem.

Parameters:

  • path (string, required): Absolute path to the file to read within the sandbox

Returns: Promise<string> - File contents as UTF-8 encoded string

Examples:

Notes:

  • Always returns UTF-8 encoded strings

  • Throws an error if the file does not exist

  • Requires absolute paths (paths should start with /)

  • No encoding options available - always returns UTF-8


filesystem.writeFile(path, content)

Write content to a file in the sandbox filesystem, creating the file if it doesn't exist.

Parameters:

  • path (string, required): Absolute path where the file should be written

  • content (string, required): Content to write to the file as UTF-8 text

Returns: Promise<void> - Resolves when the file is successfully written

Examples:

Notes:

  • Always writes UTF-8 encoded text

  • Creates the file if it doesn't exist

  • Overwrites existing files completely (previous content is lost)

  • Requires absolute paths (paths should start with /)

  • No encoding options available - always UTF-8


filesystem.mkdir(path)

Create a directory in the sandbox filesystem, automatically creating parent directories as needed.

Parameters:

  • path (string, required): Absolute path of the directory to create

Returns: Promise<void> - Resolves when the directory is successfully created

Examples:

Notes:

  • Automatically creates parent directories as needed

  • Does not throw an error if the directory already exists

  • Requires absolute paths (paths should start with /)

  • Throws errors only on actual failures (permissions, invalid paths, disk space issues)


filesystem.readdir(path)

List the contents of a directory in the sandbox filesystem.

Parameters:

  • path (string, required): Absolute path to the directory to list

Returns: Promise<FileEntry[]> - Array of entries in the directory

FileEntry interface:

  • name (string): Name of the file or directory

  • type ('file' | 'directory'): Type of the entry

  • size (number, optional): Size in bytes (for files)

  • modified (Date, optional): Last modification timestamp

Examples:

Notes:

  • Returns an array of FileEntry objects with file/directory metadata

  • Requires absolute paths (paths should start with /)

  • Only lists direct children - does not recursively list subdirectories

  • Throws an error if the directory does not exist

  • The size and modified fields may not be available on all providers

  • Empty directories return an empty array (not an error)


filesystem.exists(path)

Check if a file or directory exists at the specified path in the sandbox filesystem.

Parameters:

  • path (string, required): Absolute path to the file or directory to check

Returns: Promise<boolean> - Returns true if the path exists (file or directory), false otherwise

Examples:

Notes:

  • Returns a boolean value - never throws errors (unlike readFile or readdir)

  • Works for both files and directories - no distinction in the return value

  • Requires absolute paths (paths should start with /)

  • Returns false for non-existent paths, not an error

  • Cannot distinguish between a file and directory from the return value alone

  • Useful for defensive programming to prevent errors before operations


filesystem.remove(path)

Remove a file or directory from the sandbox filesystem. For directories, this recursively removes all contents.

Parameters:

  • path (string, required): Absolute path to the file or directory to remove

Returns: Promise<void> - Resolves when the file or directory is successfully removed

Examples:

Notes:

  • Works for both files and directories - no distinction needed

  • Recursive deletion for directories - removes all contents and subdirectories (like rm -rf)

  • Requires absolute paths (paths should start with /)

  • Throws an error if the path does not exist

  • Deletion is permanent - no recycle bin, trash, or undo capability

  • Use with caution - destructive operation that cannot be reversed

  • For directories, all nested files and subdirectories are removed automatically

  • No confirmation prompt - removal happens immediately

Last updated

Was this helpful?