Sandbox (interface)
Overview
Methods available for interacting with a compute sandbox.
runCommand(command, options?)
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 stringoptions(RunCommandOptions, optional): Execution optionscwd(string, optional): Working directory for command executionenv(Record<string, string>, optional): Environment variables to settimeout(number, optional): Command timeout in millisecondsbackground(boolean, optional): Run command in background without waiting for completiononStdout((data: string) => void, optional): Callback invoked with stdout chunks as the command runsonStderr((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 commandstderr(string): Standard error output from the commandexitCode(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 operationsNon-zero exit codes indicate command failure but do not throw errors - check
exitCodein the resultBackground commands return immediately with
exitCode: 0without waiting for completionThe command runs in a shell context, so all shell features (pipes, redirects, etc.) are available
Passing
onStdout/onStderrenables incremental streaming of command output. The returnedCommandResultstill contains the completestdoutandstderronce the command finishesStreaming callbacks cannot be combined with
background: trueβ doing so throwsAvailable on all sandbox instances regardless of provider
Streaming output with onStdout / onStderr
onStdout / onStderrBy 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
CommandResultstill contains the fullstdoutandstderronce 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
\nyourself 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 withbackground: truethrows, since a backgrounded command returns immediately and has no output to stream.
getInfo()
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 sandboxprovider(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 createdtimeout(number): Execution timeout in millisecondsmetadata(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
metadatafield contains any custom metadata set during sandbox creationAvailable on all sandbox instances regardless of provider
getUrl(options)
getUrl(options)Get a publicly accessible URL for accessing services running on a specific port in the sandbox.
Parameters:
options(object, required): URL configuration optionsport(number, required): Port number where the service is running in the sandboxprotocol(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
awaitthe resultThe service must be running on the specified port for the URL to be accessible
sandbox.filesystem
sandbox.filesystemComputeSDK provides filesystem operations for managing files and directories within sandboxes. All filesystem operations are accessed through the sandbox.filesystem object.
filesystem.readFile(path)
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)
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 writtencontent(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)
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)
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 directorytype('file' | 'directory'): Type of the entrysize(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
sizeandmodifiedfields may not be available on all providersEmpty directories return an empty array (not an error)
filesystem.exists(path)
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
readFileorreaddir)Works for both files and directories - no distinction in the return value
Requires absolute paths (paths should start with
/)Returns
falsefor non-existent paths, not an errorCannot distinguish between a file and directory from the return value alone
Useful for defensive programming to prevent errors before operations
filesystem.remove(path)
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?