### Full Sandbox Workflow Example Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-python.md Demonstrates a complete workflow including creating a sandbox, getting screen info, taking a screenshot, launching an application, starting streaming, interacting with the UI, and finally cleaning up. ```python from e2b_desktop import Sandbox desktop = Sandbox.create( resolution=(1920, 1080), timeout=600 ) try: # Get screen info width, height = desktop.get_screen_size() print(f'Screen: {width}x{height}') # Take screenshot image = desktop.screenshot() with open('screenshot.png', 'wb') as f: f.write(image) # Launch application desktop.launch('google-chrome') desktop.wait(3000) # Start streaming desktop.stream.start(require_auth=True) url = desktop.stream.get_url(auth_key=desktop.stream.get_auth_key()) print(f'Stream URL: {url}') # Interact desktop.left_click(100, 100) desktop.write('search query') desktop.press('enter') finally: desktop.stream.stop() desktop.kill() ``` -------------------------------- ### Full workflow example Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-python.md Demonstrates a comprehensive workflow using the Sandbox class, including creating a sandbox, getting screen info, taking a screenshot, launching an application, starting a stream, interacting with the UI, and finally cleaning up. ```APIDOC ## Full workflow ```python from e2b_desktop import Sandbox desktop = Sandbox.create( resolution=(1920, 1080), timeout=600 ) try: # Get screen info width, height = desktop.get_screen_size() print(f'Screen: {width}x{height}') # Take screenshot image = desktop.screenshot() with open('screenshot.png', 'wb') as f: f.write(image) # Launch application desktop.launch('google-chrome') desktop.wait(3000) # Start streaming desktop.stream.start(require_auth=True) url = desktop.stream.get_url(auth_key=desktop.stream.get_auth_key()) print(f'Stream URL: {url}') # Interact desktop.left_click(100, 100) desktop.write('search query') desktop.press('enter') finally: desktop.stream.stop() desktop.kill() ``` ``` -------------------------------- ### Start Stream Server with Options (Python) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/configuration.md Configure and start the stream server in Python using `stream.start()`. This example shows how to enable authentication and retrieve the stream URL. ```python desktop.stream.start(require_auth=True) auth_key = desktop.stream.get_auth_key() url = desktop.stream.get_url(auth_key=auth_key) ``` -------------------------------- ### Full Sandbox Workflow Example Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-javascript.md Demonstrates a complete workflow including sandbox creation, screen capture, application launch, streaming setup, and interaction. ```typescript import { Sandbox } from '@e2b/desktop' const sandbox = await Sandbox.create({ resolution: [1920, 1080], timeout: 600000 }) try { // Get screen info const size = await sandbox.getScreenSize() console.log('Screen:', size) // Take screenshot const screenshot = await sandbox.screenshot() // Launch application await sandbox.launch('google-chrome') await sandbox.wait(3000) // Start streaming await sandbox.stream.start({ requireAuth: true }) const url = sandbox.stream.getUrl({ authKey: sandbox.stream.getAuthKey() }) console.log('Stream URL:', url) // Interact await sandbox.leftClick(100, 100) await sandbox.write('search query') await sandbox.press('enter') } finally { await sandbox.stream.stop() await sandbox.kill() } ``` -------------------------------- ### Install E2B Desktop SDK Source: https://github.com/e2b-dev/desktop/blob/main/examples/streaming-apps-python/README.md Install the E2B Desktop SDK using pip. This is a prerequisite for running the streaming application examples. ```bash pip install e2b-desktop ``` -------------------------------- ### Full Streaming Example in Python Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md Provides a Python example for creating a sandbox, launching an application, starting a secured stream with authentication, and retrieving the stream URL. Suitable for managing streams in Python environments. ```python from e2b_desktop import Sandbox desktop = Sandbox.create( resolution=(1920, 1080), timeout=600 ) try: # Get display info width, height = desktop.get_screen_size() print(f'Display: {width}x{height}') # Launch application desktop.launch('google-chrome', 'https://example.com') desktop.wait(3000) # Start secured stream desktop.stream.start( port=6080, require_auth=True ) # Get stream URL with authentication auth_key = desktop.stream.get_auth_key() stream_url = desktop.stream.get_url( auto_connect=True, view_only=False, resize='scale', auth_key=auth_key ) print(f'Stream URL: {stream_url}') print(f'Auth Key: {auth_key}') # Stream is now accessible at stream_url for 10 minutes # Browser clients can connect and interact with the desktop # Stop stream when done desktop.stream.stop() finally: desktop.kill() ``` -------------------------------- ### Run the Application Source: https://github.com/e2b-dev/desktop/blob/main/examples/basic-javascript/README.md Start the application using the npm start script. ```bash npm start ``` -------------------------------- ### Full Streaming Example in JavaScript Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md Demonstrates how to create a sandbox, launch an application, start a secured stream with authentication, and retrieve the stream URL. Use this for full-featured stream management in JavaScript applications. ```typescript import { Sandbox } from '@e2b/desktop' const desktop = await Sandbox.create({ resolution: [1920, 1080], timeout: 600000 }) try { // Get display info const size = await desktop.getScreenSize() console.log(`Display: ${size.width}x${size.height}`) // Launch application await desktop.launch('google-chrome', 'https://example.com') await desktop.wait(3000) // Start secured stream await desktop.stream.start({ port: 6080, requireAuth: true }) // Get stream URL with authentication const authKey = desktop.stream.getAuthKey() const streamUrl = desktop.stream.getUrl({ autoConnect: true, viewOnly: false, resize: 'scale', authKey: authKey }) console.log('Stream URL:', streamUrl) console.log('Auth Key:', authKey) // Stream is now accessible at streamUrl for 10 minutes // Browser clients can connect and interact with the desktop // Stop stream when done await desktop.stream.stop() } finally { await desktop.kill() } ``` -------------------------------- ### Example URL with Authentication Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md This example shows a typical URL structure for connecting to a VNC session, including parameters for auto-connection, view-only mode, resize behavior, and password authentication. ```text https://my-sandbox-host.e2b.dev:6080/vnc.html?autoconnect=true&view_only=false&resize=scale&password=abc123xyz ``` -------------------------------- ### Stream Desktop Screen Source: https://github.com/e2b-dev/desktop/blob/main/packages/python-sdk/README.md Shows how to start, get the URL for, and stop streaming the entire desktop screen. ```APIDOC ## Stream Desktop Screen ### Description Enables streaming of the entire desktop screen, providing its URL and allowing the stream to be stopped. ### Method ```python from e2b_desktop import Sandbox desktop = Sandbox.create() # Start the stream desktop.stream.start() # Get stream URL url = desktop.stream.get_url() print(url) # Stop the stream desktop.stream.stop() ``` ``` -------------------------------- ### Install Build Dependencies Source: https://github.com/e2b-dev/desktop/blob/main/template/README.md Installs the necessary dependencies for building the E2B Desktop template using Poetry. ```bash poetry install ``` -------------------------------- ### Install E2B Desktop SDK Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/MANIFEST.md Install the E2B Desktop SDK using npm for JavaScript or pip for Python. ```bash # JavaScript npm install @e2b/desktop ``` ```bash # Python pip install e2b-desktop ``` -------------------------------- ### Launch applications Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-python.md The launch() method starts an application. It can optionally open a specific URI or file with the application upon launch. Ensure the application is installed and accessible. ```python desktop.launch('google-chrome') desktop.launch('firefox') desktop.launch('google-chrome', 'https://example.com') desktop.launch('thunar') ``` -------------------------------- ### Install Dependencies Source: https://github.com/e2b-dev/desktop/blob/main/examples/basic-javascript/README.md Install the necessary project dependencies using npm. ```bash npm install ``` -------------------------------- ### Window Control Examples Source: https://github.com/e2b-dev/desktop/blob/main/packages/js-sdk/README.md Manage windows within the sandbox, including getting the current active window ID, retrieving all windows for a specific application, and getting the title of a window. ```javascript import { Sandbox } from '@e2b/desktop' const desktop = await Sandbox.create() // Get current (active) window ID const windowId = await desktop.getCurrentWindowId() // Get all windows of the application const windowIds = await desktop.getApplicationWindows('Firefox') // Get window title const title = await desktop.getWindowTitle(windowId) ``` -------------------------------- ### Install Dependencies Source: https://github.com/e2b-dev/desktop/blob/main/examples/basic-python/README.md Installs project dependencies using Poetry. ```bash poetry install ``` -------------------------------- ### Copy Environment File Source: https://github.com/e2b-dev/desktop/blob/main/examples/basic-python/README.md Copies the example environment file to a new file for configuration. ```bash cp .env.example .env ``` -------------------------------- ### Install E2B Desktop SDK Source: https://github.com/e2b-dev/desktop/blob/main/examples/streaming-apps-javascript/README.md Install the E2B Desktop SDK using npm. This is a prerequisite for using the streaming applications feature. ```bash npm install @e2b/desktop ``` -------------------------------- ### Create and Control Sandbox (Python) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/INDEX.md Demonstrates creating a sandbox with specific resolution and timeout, launching an application, starting a stream, and controlling the desktop. Includes error handling for stream and sandbox lifecycle. ```python from e2b_desktop import Sandbox desktop = Sandbox.create( resolution=(1920, 1080), timeout=600 ) try: # Launch app desktop.launch('google-chrome') # Start streaming desktop.stream.start(require_auth=True) url = desktop.stream.get_url( auth_key=desktop.stream.get_auth_key() ) print(f'Stream: {url}') # Control desktop desktop.left_click(100, 200) desktop.write('search text') desktop.press('enter') finally: desktop.stream.stop() desktop.kill() ``` -------------------------------- ### stream.start() Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md Starts the VNC streaming server with various configuration options including ports and authentication. ```APIDOC ## stream.start() ### Description Start the VNC streaming server. ### Method `start( vnc_port: Optional[int] = None, port: Optional[int] = None, require_auth: bool = False, window_id: Optional[str] = None )` ### Parameters #### Optional Parameters - **vnc_port** (`int`) - Optional - `5900` - VNC server port (internal, used by x11vnc) - **port** (`int`) - Optional - `6080` - noVNC web proxy port (external, for client connections) - **require_auth** (`bool`) - Optional - `False` - Enable password authentication for stream access - **window_id** (`str`) - Optional - `None` - Specific window ID to stream; if omitted, streams entire desktop ### Returns `None` ### Raises - `RuntimeError` if stream is already running - `TimeoutException` if VNC server fails to start or port never becomes available ### Example ```python # Start stream without authentication desktop.stream.start() # Start with authentication desktop.stream.start(require_auth=True) # Start on custom port desktop.stream.start(port=6081) # Stream specific application window window_ids = desktop.get_application_windows('Firefox') if window_ids: desktop.stream.start( require_auth=True, window_id=window_ids[0] ) ``` ``` -------------------------------- ### Start Stream Server with Options (JavaScript) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/configuration.md Use this snippet to start the stream server with custom options like authentication, port, and specific window ID. Ensure the `desktop.stream` object is available. ```typescript await desktop.stream.start({ requireAuth: true, port: 6080, windowId: '123456' // optional }) const authKey = desktop.stream.getAuthKey() const url = desktop.stream.getUrl({ authKey }) ``` -------------------------------- ### Start and Get Screen Stream URL (Python) Source: https://github.com/e2b-dev/desktop/blob/main/README.md Initiates screen streaming and retrieves the stream URL. The stream can be configured for view-only access. ```python from e2b_desktop import Sandbox desktop = Sandbox.create() # Start the stream desktop.stream.start() # Get stream URL url = desktop.stream.get_url() print(url) # Get stream URL and disable user interaction url = desktop.stream.get_url(view_only=True) print(url) # Stop the stream desktop.stream.stop() ``` -------------------------------- ### Sandbox.create() - Example Usage Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-javascript.md Demonstrates creating a sandbox instance using the default template and a custom template with specific options like resolution, DPI, and environment variables. ```typescript // Default template const sandbox = await Sandbox.create() // Custom template with options const sandbox = await Sandbox.create('desktop', { resolution: [1920, 1080], dpi: 96, timeout: 600000, envs: { DISPLAY: ':0' } }) ``` -------------------------------- ### Start VNC Streaming Server (Python) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md Starts the VNC streaming server. Configure VNC port, noVNC port, authentication, or stream a specific window. ```python def start( self, vnc_port: Optional[int] = None, port: Optional[int] = None, require_auth: bool = False, window_id: Optional[str] = None ) -> None ``` ```python # Start stream without authentication desktop.stream.start() ``` ```python # Start with authentication desktop.stream.start(require_auth=True) ``` ```python # Start on custom port desktop.stream.start(port=6081) ``` ```python # Stream specific application window window_ids = desktop.get_application_windows('Firefox') if window_ids: desktop.stream.start( require_auth=True, window_id=window_ids[0] ) ``` -------------------------------- ### stream.start() Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-javascript.md Starts streaming the sandbox's display. ```APIDOC ## stream.start() ### Description Start streaming the sandbox's display. Optionally specify a `windowId` to stream a specific application window. ### Method Signature ```typescript stream.start(opts?: { requireAuth?: boolean; windowId?: string }): Promise ``` ### Parameters * `opts` (object, optional): Options for starting the stream. * `requireAuth` (boolean, optional): If true, requires authentication to access the stream URL. * `windowId` (string, optional): The ID of the specific window to stream. ### Returns * `Promise`: A promise that resolves when the stream has started. ``` -------------------------------- ### JavaScript: stream.start() Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md Starts the VNC streaming server with configurable options for port, authentication, and specific window streaming. ```APIDOC ## JavaScript: stream.start() ### Description Start the VNC streaming server. ### Method `start(opts?: VNCServerOptions): Promise` ### Parameters #### Options - **vncPort** (`number`) - Optional - VNC server port (internal, used by x11vnc). Default: `5900` - **port** (`number`) - Optional - noVNC web proxy port (external, for client connections). Default: `6080` - **requireAuth** (`boolean`) - Optional - Enable password authentication for stream access. Default: `false` - **windowId** (`string`) - Optional - Specific window ID to stream; if omitted, streams entire desktop. ### Returns `Promise` ### Throws - `Error` if stream is already running - `TimeoutError` if VNC server fails to start or port never becomes available ### Example ```typescript // Start stream without authentication await desktop.stream.start() // Start with authentication await desktop.stream.start({ requireAuth: true }) // Start on custom port await desktop.stream.start({ port: 6081 }) // Stream specific application window const windowIds = await desktop.getApplicationWindows('Firefox') await desktop.stream.start({ requireAuth: true, windowId: windowIds[0] }) ``` ``` -------------------------------- ### Start VNC Streaming Server (JavaScript) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md Starts the VNC streaming server. Configure VNC port, noVNC proxy port, authentication, or a specific window ID to stream. Throws an error if the stream is already running or a timeout error if the VNC server fails to start. ```typescript await desktop.stream.start() await desktop.stream.start({ requireAuth: true }) await desktop.stream.start({ port: 6081 }) const windowIds = await desktop.getApplicationWindows('Firefox') await desktop.stream.start({ requireAuth: true, windowId: windowIds[0] }) ``` -------------------------------- ### Install E2B SDK and dotenv Source: https://github.com/e2b-dev/desktop/blob/main/template/README.md Installs the E2B SDK and the dotenv library using pip, required for creating custom sandbox templates. ```bash pip install e2b dotenv ``` -------------------------------- ### Create and Control Sandbox (JavaScript) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/INDEX.md Demonstrates creating a sandbox with specific resolution and timeout, launching an application, starting a stream, and controlling the desktop. Includes error handling for stream and sandbox lifecycle. ```javascript import { Sandbox } from '@e2b/desktop' const desktop = await Sandbox.create({ resolution: [1920, 1080], timeout: 600000 }) try { // Launch app await desktop.launch('google-chrome') // Start streaming await desktop.stream.start({ requireAuth: true }) const url = desktop.stream.getUrl({ authKey: desktop.stream.getAuthKey() }) console.log('Stream:', url) // Control desktop await desktop.leftClick(100, 200) await desktop.write('search text') await desktop.press('enter') } finally { await desktop.stream.stop() await desktop.kill() } ``` -------------------------------- ### Create and Launch Sandbox with Python Source: https://github.com/e2b-dev/desktop/blob/main/README.md Create a new desktop sandbox and launch an application like Google Chrome. This example demonstrates basic sandbox creation and application launching in Python. ```python from e2b_desktop import Sandbox # Create a new desktop sandbox desktop = Sandbox.create() # Launch an application desktop.launch('google-chrome') # or vscode, firefox, etc. # Wait 10s for the application to open desktop.wait(10000) # Stream the application's window # Note: There can be only one stream at a time # You need to stop the current stream before streaming another application desktop.stream.start( window_id=desktop.get_current_window_id(), # if not provided the whole desktop will be streamed require_auth=True ) # Get the stream auth key auth_key = desktop.stream.get_auth_key() # Print the stream URL print('Stream URL:', desktop.stream.get_url(auth_key=auth_key)) # Kill the sandbox after the tasks are finished # desktop.kill() ``` -------------------------------- ### Create and Launch a Desktop Sandbox Source: https://github.com/e2b-dev/desktop/blob/main/packages/python-sdk/README.md Create a new desktop sandbox, launch an application like Google Chrome, and stream its window. Ensure to stop any existing stream before starting a new one. ```python from e2b_desktop import Sandbox # Create a new desktop sandbox desktop = Sandbox.create() # Launch an application desktop.launch('google-chrome') # or vscode, firefox, etc. # Wait 10s for the application to open desktop.wait(10000) # Stream the application's window # Note: There can be only one stream at a time # You need to stop the current stream before streaming another application desktop.stream.start( window_id=desktop.get_current_window_id(), # if not provided the whole desktop will be streamed require_auth=True ) # Get the stream auth key auth_key = desktop.stream.get_auth_key() # Print the stream URL print('Stream URL:', desktop.stream.get_url(auth_key=auth_key)) # Kill the sandbox after the tasks are finished # desktop.kill() ``` -------------------------------- ### Create and Launch Sandbox with JavaScript Source: https://github.com/e2b-dev/desktop/blob/main/README.md Create a new desktop sandbox and launch an application like Google Chrome. This example demonstrates basic sandbox creation and application launching in JavaScript. ```javascript import { Sandbox } from '@e2b/desktop' // Start a new desktop sandbox const desktop = await Sandbox.create() // Launch an application await desktop.launch('google-chrome') // or vscode, firefox, etc. // Wait 10s for the application to open await desktop.wait(10000) // Stream the application's window // Note: There can be only one stream at a time // You need to stop the current stream before streaming another application await desktop.stream.start({ windowId: await desktop.getCurrentWindowId(), // if not provided the whole desktop will be streamed requireAuth: true, }) // Get the stream auth key const authKey = desktop.stream.getAuthKey() // Print the stream URL console.log('Stream URL:', desktop.stream.getUrl({ authKey })) // Kill the sandbox after the tasks are finished // await desktop.kill() ``` -------------------------------- ### Create and Launch Sandbox (Python) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/00-START-HERE.txt Initialize a sandbox instance and launch a Google Chrome browser using the Python SDK. The 'e2b_desktop' package must be installed. ```python from e2b_desktop import Sandbox sb = Sandbox.create() sb.launch('google-chrome') ``` -------------------------------- ### Launch an application Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-javascript.md Starts a specified application. Optionally, you can provide a URI or file to be opened by the application upon launch. ```typescript await sandbox.launch('google-chrome') await sandbox.launch('firefox') ``` ```typescript await sandbox.launch('google-chrome', 'https://example.com') ``` ```typescript await sandbox.launch('thunar') ``` -------------------------------- ### Install E2B Desktop SDK for JavaScript Source: https://github.com/e2b-dev/desktop/blob/main/README.md Install the E2B Desktop SDK for JavaScript using npm. This command is used to set up the necessary packages for interacting with the E2B Desktop Sandbox in JavaScript. ```bash npm install @e2b/desktop ``` -------------------------------- ### Create and Stream Desktop Sandbox Applications Source: https://github.com/e2b-dev/desktop/blob/main/examples/streaming-apps-javascript/README.md This example demonstrates how to create a desktop sandbox, launch applications like Google Chrome and VS Code, stream their windows, interact with them, and manage the streams. Ensure you have an E2B API key set as an environment variable. ```javascript import { Sandbox } from '@e2b/desktop' // Start a new desktop sandbox const desktop = await Sandbox.create() console.log('Desktop sandbox created', desktop.sandboxId) // Launch an application console.log('Launching Google Chrome') await desktop.launch('google-chrome') // or vscode, firefox, etc. // Wait 15s for the application to open await desktop.wait(15000) // Stream the application's window // Note: there can be only one stream at a time // You need to stop the current stream before streaming another application console.log('Starting to stream Google Chrome') await desktop.stream.start({ windowId: await desktop.getCurrentWindowId(), // if not provided the whole desktop will be streamed requireAuth: true, }) // Get the stream auth key const authKey = desktop.stream.getAuthKey() // Print the stream URL console.log('Stream URL:', desktop.stream.getUrl({ authKey })) // Do some actions in the application console.log('Writing to Google Chrome') await desktop.write('What is the capital of Germany?') console.log('Pressing Enter') await desktop.press('Enter') // wait 15s for page to load console.log('Waiting 15s') await desktop.wait(15000) // Stop the stream console.log('Stopping the stream') await desktop.stream.stop() // Open another application console.log('Launching VS Code') await desktop.launch('code') // Wait 15s for the application to open await desktop.wait(15000) // Start streaming the new application console.log('Starting to stream VS Code') await desktop.stream.start({ windowId: await desktop.getCurrentWindowId(), // if not provided the whole desktop will be streamed requireAuth: true, }) // Get the stream auth key const authKey2 = desktop.stream.getAuthKey() // Print the stream URL console.log('Stream URL:', desktop.stream.getUrl({ authKey: authKey2 })) // Kill the sandbox after the tasks are finished // await desktop.kill() ``` -------------------------------- ### Install E2B Desktop SDK for Python Source: https://github.com/e2b-dev/desktop/blob/main/README.md Install the E2B Desktop SDK for Python using pip. This command is used to set up the necessary libraries for interacting with the E2B Desktop Sandbox in Python. ```bash pip install e2b-desktop ``` -------------------------------- ### Start and Stop VNC Stream Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-python.md Starts a VNC streaming server, retrieves an authentication key, generates a URL, and then stops the stream. Requires the desktop object to be initialized. ```python desktop.stream.start(require_auth=True) auth_key = desktop.stream.get_auth_key() url = desktop.stream.get_url(auth_key=auth_key) print(url) desktop.stream.stop() ``` -------------------------------- ### Setup Stream Connection (JavaScript) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/INDEX.md Initiate a stream connection with authentication and retrieve the stream URL. See stream-api.md for more details on stream operations. ```javascript // See [stream-api.md](stream-api.md) await sb.stream.start({ requireAuth: true }) const url = sb.stream.getUrl({ authKey: sb.stream.getAuthKey() }) ``` -------------------------------- ### Basic Sandbox Workflow Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/API-INDEX.md Demonstrates the fundamental workflow for creating, launching an application, starting a stream, and cleaning up resources. Includes try-finally blocks for robust stream management and sandbox termination. ```typescript // JavaScript const desktop = await Sandbox.create() try { await desktop.launch('google-chrome') await desktop.stream.start() const url = desktop.stream.getUrl() // Use desktop... } finally { await desktop.stream.stop() await desktop.kill() } ``` ```python # Python desktop = Sandbox.create() try: desktop.launch('google-chrome') desktop.stream.start() url = desktop.stream.get_url() # Use desktop... finally: desktop.stream.stop() desktop.kill() ``` -------------------------------- ### Create and Launch Sandbox (JavaScript) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/00-START-HERE.txt Quickly create a sandbox instance and launch a Google Chrome browser within it using the JavaScript SDK. Ensure the '@e2b/desktop' package is installed. ```javascript import { Sandbox } from '@e2b/desktop' const sb = await Sandbox.create() await sb.launch('google-chrome') ``` -------------------------------- ### Streaming specific application example Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-python.md Shows how to stream a specific application window by first retrieving its window ID. ```APIDOC ## Streaming specific application ```python window_ids = desktop.get_application_windows('Firefox') if window_ids: desktop.stream.start(require_auth=True, window_id=window_ids[0]) ``` ``` -------------------------------- ### Launch Application with Python SDK Source: https://github.com/e2b-dev/desktop/blob/main/packages/python-sdk/README.md Launches a specified application within the sandbox environment. Ensure the application is installed in the sandbox. ```python from e2b_desktop import Sandbox desktop = Sandbox.create() # Launch the application desktop.launch('google-chrome') ``` -------------------------------- ### Stream Desktop Screen Source: https://github.com/e2b-dev/desktop/blob/main/packages/python-sdk/README.md Start streaming the entire desktop screen and retrieve the stream URL. Remember to stop the stream when finished. ```python from e2b_desktop import Sandbox desktop = Sandbox.create() # Start the stream desktop.stream.start() # Get stream URL url = desktop.stream.get_url() print(url) # Stop the stream desktop.stream.stop() ``` -------------------------------- ### VNC Server Streaming Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/INDEX.md Details on how to start, stop, and retrieve the URL for streaming the sandbox desktop using the VNC server. ```APIDOC ## VNCServer Class Methods ### Description Manages the streaming of the sandbox desktop content via VNC. ### Methods - `desktop.stream.start(options)`: Starts the VNC streaming server. - `desktop.stream.stop()`: Stops the VNC streaming server. - `desktop.stream.getUrl(options)`: Retrieves the URL for accessing the VNC stream. - `desktop.stream.getAuthKey()`: Retrieves the authentication key for the VNC stream. ### Parameters #### desktop.stream.start options - **requireAuth** (boolean): Optional. If true, requires authentication to access the stream. #### desktop.stream.getUrl options - **authKey** (string): The authentication key obtained from `getAuthKey()`. ### Request Example (JavaScript) ```javascript await desktop.stream.start({ requireAuth: true }) const url = desktop.stream.getUrl({ authKey: desktop.stream.getAuthKey() }) console.log('Stream:', url) await desktop.stream.stop() ``` ### Request Example (Python) ```python desktop.stream.start(require_auth=True) url = desktop.stream.get_url( auth_key=desktop.stream.get_auth_key() ) print(f'Stream: {url}') desktop.stream.stop() ``` ``` -------------------------------- ### Stream Desktop Screen Source: https://github.com/e2b-dev/desktop/blob/main/packages/js-sdk/README.md Provides examples for streaming the entire desktop screen or specific application windows, with and without authentication. ```APIDOC ## Stream.start() ### Description Starts streaming the entire desktop screen. ### Method `desktop.stream.start()` ### Returns - `Promise`: A promise that resolves when the stream has started. ``` ```APIDOC ## Stream.start({ requireAuth: true }) ### Description Starts streaming the desktop screen with authentication enabled. ### Method `desktop.stream.start({ requireAuth: true })` ### Parameters #### Request Body - **requireAuth** (boolean) - Optional - If true, enables authentication for the stream. ### Returns - `Promise`: A promise that resolves when the stream has started. ``` ```APIDOC ## Stream.getUrl({ authKey? }) ### Description Retrieves the URL for the stream. If `authKey` is provided, it includes the key in the URL. ### Method `desktop.stream.getUrl({ authKey?: string })` ### Parameters #### Query Parameters - **authKey** (string) - Optional - The authentication key for the stream. ### Returns - `string`: The stream URL. ``` ```APIDOC ## Stream.getAuthKey() ### Description Retrieves the authentication key for the stream when `requireAuth` is enabled. ### Method `desktop.stream.getAuthKey()` ### Returns - `Promise`: A promise that resolves with the authentication key. ``` ```APIDOC ## Stream.stop() ### Description Stops the current screen stream. ### Method `desktop.stream.stop()` ### Returns - `Promise`: A promise that resolves when the stream has stopped. ``` ```APIDOC ## Stream.start({ windowId }) ### Description Starts streaming a specific application window. ### Method `desktop.stream.start({ windowId: string })` ### Parameters #### Request Body - **windowId** (string) - Required - The ID of the window to stream. ### Returns - `Promise`: A promise that resolves when the stream has started. ``` -------------------------------- ### Switching Between Application Streams (Python) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md Demonstrates how to stop the current stream and start a new one for a different application. Ensure the previous stream is stopped before initiating a new one to avoid conflicts. ```python # Stream application 1 app1_windows = desktop.get_application_windows('Firefox') desktop.stream.start(window_id=app1_windows[0]) print('Streaming Firefox...') # Later, switch to application 2 desktop.stream.stop() app2_windows = desktop.get_application_windows('VirtualBox') desktop.stream.start(window_id=app2_windows[0]) print('Streaming VirtualBox...') ``` -------------------------------- ### Stream Desktop Screen Source: https://github.com/e2b-dev/desktop/blob/main/packages/js-sdk/README.md Start streaming the entire desktop screen of the sandbox. This snippet shows how to initiate the stream and retrieve its URL. ```javascript import { Sandbox } from '@e2b/desktop' const desktop = await Sandbox.create() // Start the stream await desktop.stream.start() // Get stream URL const url = desktop.stream.getUrl() console.log(url) // Stop the stream await desktop.stream.stop() ``` -------------------------------- ### Create and Launch Sandbox Source: https://github.com/e2b-dev/desktop/blob/main/packages/js-sdk/README.md Demonstrates how to create a new desktop sandbox instance and launch an application within it. ```APIDOC ## Sandbox.create() ### Description Creates and starts a new desktop sandbox instance. ### Method `Sandbox.create()` ### Returns - `Promise`: A promise that resolves with the newly created Sandbox instance. ``` ```APIDOC ## Sandbox.launch(applicationName) ### Description Launches a specified application within the sandbox. ### Method `desktop.launch(applicationName: string)` ### Parameters #### Path Parameters - **applicationName** (string) - Required - The name of the application to launch (e.g., 'google-chrome', 'vscode'). ### Returns - `Promise`: A promise that resolves when the application has been launched. ``` -------------------------------- ### Start Streaming with Authentication Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/API-INDEX.md Initiates a stream with authentication enabled and retrieves the authentication key and URL. Useful for securing stream access. ```typescript // JavaScript await desktop.stream.start({ requireAuth: true }) const authKey = desktop.stream.getAuthKey() const url = desktop.stream.getUrl({ authKey }) ``` ```python # Python desktop.stream.start(require_auth=True) auth_key = desktop.stream.get_auth_key() url = desktop.stream.get_url(auth_key=auth_key) ``` -------------------------------- ### Handle 'Could not start noVNC server' Error in JavaScript Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/errors.md Handle TimeoutError when starting the stream to diagnose issues with the noVNC server binding to a port. This example shows how to retry with a different port if the initial attempt fails. ```typescript try { await desktop.stream.start({ port: 6080 }) } catch (e) { if (e instanceof TimeoutError && e.message.includes('noVNC')) { console.error('Failed to start stream server') // Try with a different port await desktop.stream.start({ port: 6081 }) } } ``` -------------------------------- ### Start and Get Screen Stream URL (JavaScript) Source: https://github.com/e2b-dev/desktop/blob/main/README.md Initiates screen streaming and retrieves the stream URL. The stream can be configured for view-only access. ```javascript import { Sandbox } from '@e2b/desktop' const desktop = await Sandbox.create() // Start the stream await desktop.stream.start() // Get stream URL const url = desktop.stream.getUrl() console.log(url) // Get stream URL and disable user interaction const url = desktop.stream.getUrl({ viewOnly: true }) console.log(url) // Stop the stream await desktop.stream.stop() ``` -------------------------------- ### Create and Launch Sandbox Source: https://github.com/e2b-dev/desktop/blob/main/packages/python-sdk/README.md Demonstrates how to create a new desktop sandbox, launch an application like Google Chrome, and stream its window. ```APIDOC ## Create and Launch Sandbox ### Description Creates a new desktop sandbox, launches a specified application, and optionally streams its window. ### Method ```python from e2b_desktop import Sandbox # Create a new desktop sandbox desktop = Sandbox.create() # Launch an application desktop.launch('google-chrome') # or vscode, firefox, etc. # Wait 10s for the application to open desktop.wait(10000) # Stream the application's window # Note: There can be only one stream at a time # You need to stop the current stream before streaming another application desktop.stream.start( window_id=desktop.get_current_window_id(), # if not provided the whole desktop will be streamed require_auth=True ) # Get the stream auth key auth_key = desktop.stream.get_auth_key() # Print the stream URL print('Stream URL:', desktop.stream.get_url(auth_key=auth_key)) # Kill the sandbox after the tasks are finished # desktop.kill() ``` ``` -------------------------------- ### Setup Stream Connection (Python) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/INDEX.md This Python code sets up a stream connection, optionally requiring authentication, and retrieves the stream URL. Refer to stream-api.md for comprehensive stream API documentation. ```python # See [stream-api.md](stream-api.md) sb.stream.start(require_auth=True) url = sb.stream.get_url(auth_key=sb.stream.get_auth_key()) ``` -------------------------------- ### Get Stream Authentication Key (TypeScript) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md Retrieves the authentication password for secure stream access. Ensure authentication is enabled via `requireAuth` in `start()` before calling. ```typescript public getAuthKey(): string ``` ```typescript await desktop.stream.start({ requireAuth: true }) const authKey = desktop.stream.getAuthKey() console.log(`Auth key: ${authKey}`) // Embed in URL const url = desktop.stream.getUrl({ authKey }) ``` -------------------------------- ### Get window title by ID Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-python.md Fetch the title of a specific window using its ID with get_window_title(). This requires obtaining the window ID first, for example, using get_current_window_id(). ```python window_id = desktop.get_current_window_id() title = desktop.get_window_title(window_id) print(f"Window title: {title}") ``` -------------------------------- ### Get Stream URL After Successful Start in JavaScript Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/errors.md Ensure that `stream.start()` has been successfully called before attempting to retrieve the stream URL using `stream.getUrl()`. This prevents errors when the server is not running. ```typescript await desktop.stream.start() const url = desktop.stream.getUrl() // OK // ❌ This would fail: const url2 = desktop.stream.getUrl() // After stop() await desktop.stream.stop() ``` -------------------------------- ### Switching Between Application Streams (TypeScript) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/stream-api.md Demonstrates how to stop the current stream and start a new one for a different application. Ensure the previous stream is stopped before initiating a new one to avoid conflicts. ```typescript // Stream application 1 const app1Windows = await desktop.getApplicationWindows('Firefox') await desktop.stream.start({ windowId: app1Windows[0] }) console.log('Streaming Firefox...') // Later, switch to application 2 await desktop.stream.stop() const app2Windows = await desktop.getApplicationWindows('VirtualBox') await desktop.stream.start({ windowId: app2Windows[0] }) console.log('Streaming VirtualBox...') ``` -------------------------------- ### Get Stream Authentication Key with require_auth Enabled in Python Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/errors.md Enable `require_auth=True` when starting the stream to successfully retrieve an authentication key using `stream.get_auth_key()`. This is necessary when authentication is required for the stream. ```python # Python desktop.stream.start(require_auth=True) auth_key = desktop.stream.get_auth_key() # OK # ❌ This would fail: desktop2 = Sandbox.create() desktop2.stream.start() # require_auth defaults to False key = desktop2.stream.get_auth_key() ``` -------------------------------- ### Get Stream Authentication Key with requireAuth Enabled in JavaScript Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/errors.md Enable `requireAuth: true` when starting the stream to successfully retrieve an authentication key using `stream.getAuthKey()`. This is necessary when authentication is required for the stream. ```typescript // JavaScript await desktop.stream.start({ requireAuth: true }) const authKey = desktop.stream.getAuthKey() // OK // ❌ This would fail: const desktop2 = await Sandbox.create() await desktop2.stream.start() // requireAuth defaults to false const key = desktop2.stream.getAuthKey() ``` -------------------------------- ### Sandbox Creation and Control Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/INDEX.md Demonstrates how to create a new sandbox instance with specified resolution and timeout, launch applications, and control the desktop environment. ```APIDOC ## Sandbox Class Methods ### Description Provides methods for creating, managing, and interacting with sandboxed desktop environments. ### Methods - `Sandbox.create(options)`: Asynchronously creates a new sandbox instance. - `desktop.launch(appName)`: Launches a specified application within the sandbox. - `desktop.leftClick(x, y)`: Simulates a left mouse click at the given coordinates. - `desktop.write(text)`: Writes the specified text to the active input field. - `desktop.press(key)`: Simulates pressing a keyboard key. - `desktop.kill()`: Terminates the sandbox instance. ### Parameters #### Sandbox.create options - **resolution** (array): Optional. Specifies the resolution of the sandbox, e.g., `[1920, 1080]`. - **timeout** (number): Optional. Sets a timeout for sandbox operations in milliseconds (JS) or seconds (Python). ### Request Example (JavaScript) ```javascript import { Sandbox } from '@e2b/desktop' const desktop = await Sandbox.create({ resolution: [1920, 1080], timeout: 600000 }) await desktop.launch('google-chrome') await desktop.leftClick(100, 200) await desktop.write('search text') await desktop.press('enter') ``` ### Request Example (Python) ```python from e2b_desktop import Sandbox desktop = Sandbox.create( resolution=(1920, 1080), timeout=600 ) desktop.launch('google-chrome') desktop.left_click(100, 200) desktop.write('search text') desktop.press('enter') ``` ``` -------------------------------- ### Full Sandbox Lifecycle with Error Handling (Python) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/utilities-and-patterns.md Demonstrates creating, configuring, launching applications, setting up streaming, and cleaning up a sandbox with comprehensive error handling for timeouts and command failures. Ensure to handle potential errors during stream stopping. ```python from e2b_desktop import Sandbox from e2b import TimeoutException, CommandExitException def run_with_desktop(): desktop = None try: # Create sandbox with custom configuration desktop = Sandbox.create( resolution=(1920, 1080), dpi=96, timeout=600 # 10 minutes ) print(f"✓ Sandbox created: {desktop.sandbox_id}") # Initialize display width, height = desktop.get_screen_size() print(f"✓ Display ready: {width}x{height}") # Perform operations desktop.launch('google-chrome') desktop.wait(2000) # Setup streaming desktop.stream.start(require_auth=True) url = desktop.stream.get_url(auth_key=desktop.stream.get_auth_key()) print(f"✓ Stream available: {url}") # Use the sandbox... except TimeoutException as e: print(f"✗ Operation timed out: {e}") raise except CommandExitException as e: print(f"✗ Command failed: {e}") raise except Exception as e: print(f"✗ Unexpected error: {e}") raise finally: # Cleanup if desktop: try: desktop.stream.stop() except Exception: print("Warning: Failed to stop stream") desktop.kill() print("✓ Sandbox cleaned up") if __name__ == "__main__": run_with_desktop() ``` -------------------------------- ### Create and Launch Sandbox Source: https://github.com/e2b-dev/desktop/blob/main/packages/js-sdk/README.md Create a new desktop sandbox and launch an application like Google Chrome. Includes instructions for streaming the application's window and obtaining the stream authentication key. ```javascript import { Sandbox } from '@e2b/desktop' // Start a new desktop sandbox const desktop = await Sandbox.create() // Launch an application await desktop.launch('google-chrome') // or vscode, firefox, etc. // Wait 10s for the application to open await desktop.wait(10000) // Stream the application's window // Note: there can be only one stream at a time // You need to stop the current stream before streaming another application await desktop.stream.start({ windowId: await desktop.getCurrentWindowId(), // if not provided the whole desktop will be streamed requireAuth: true, }) // Get the stream auth key const authKey = desktop.stream.getAuthKey() // Print the stream URL console.log('Stream URL:', desktop.stream.getUrl({ authKey })) // Kill the sandbox after the tasks are finished // await desktop.kill() ``` -------------------------------- ### Create and Launch Sandbox (Python) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/MANIFEST.md Create a new Sandbox instance and launch the Google Chrome browser within it. ```python desktop = Sandbox.create() desktop.launch('google-chrome') desktop.stream.start() ``` -------------------------------- ### JavaScript: Control Window Operations Source: https://github.com/e2b-dev/desktop/blob/main/README.md Get the active window ID, retrieve all windows for a specific application, and get the title of a given window. ```JavaScript import { Sandbox } from '@e2b/desktop' const desktop = await Sandbox.create() // Get current (active) window ID const windowId = await desktop.getCurrentWindowId() // Get all windows of the application const windowIds = await desktop.getApplicationWindows('Firefox') // Get window title const title = await desktop.getWindowTitle(windowId) ``` -------------------------------- ### Python: Control Window Operations Source: https://github.com/e2b-dev/desktop/blob/main/README.md Get the active window ID, retrieve all windows for a specific application, and get the title of a given window. ```Python from e2b_desktop import Sandbox desktop = Sandbox.create() # Get current (active) window ID window_id = desktop.get_current_window_id() # Get all windows of the application window_ids = desktop.get_application_windows("Firefox") # Get window title title = desktop.get_window_title(window_id) ``` -------------------------------- ### Start Password Protected Screen Stream (JavaScript) Source: https://github.com/e2b-dev/desktop/blob/main/README.md Starts a screen stream that requires authentication. An authentication key is generated and used to retrieve the stream URL. ```javascript import { Sandbox } from '@e2b/desktop' const desktop = await Sandbox.create() // Start the stream await desktop.stream.start({ requireAuth: true, // Require authentication with an auto-generated key }) // Retrieve the authentication key const authKey = await desktop.stream.getAuthKey() // Get stream URL const url = desktop.stream.getUrl({ authKey }) console.log(url) // Stop the stream await desktop.stream.stop() ``` -------------------------------- ### Python: Open File Source: https://github.com/e2b-dev/desktop/blob/main/README.md Create a file with specified content and then open it using the system's default application. ```Python from e2b_desktop import Sandbox desktop = Sandbox.create() # Open file with default application desktop.files.write("/home/user/index.js", "console.log('hello')") # First create the file desktop.open("/home/user/index.js") # Then open it ``` -------------------------------- ### Start Password Protected Screen Stream (Python) Source: https://github.com/e2b-dev/desktop/blob/main/README.md Starts a screen stream that requires authentication. An authentication key is generated and used to retrieve the stream URL. ```python from e2b_desktop import Sandbox desktop = Sandbox.create() # Start the stream desktop.stream.start( require_auth=True # Require authentication with an auto-generated key ) # Retrieve the authentication key auth_key = desktop.stream.get_auth_key() # Get stream URL url = desktop.stream.get_url(auth_key=auth_key) print(url) # Stop the stream desktop.stream.stop() ``` -------------------------------- ### Create and Launch Sandbox (JavaScript) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/MANIFEST.md Create a new Sandbox instance and launch the Google Chrome browser within it. ```javascript const desktop = await Sandbox.create() await desktop.launch('google-chrome') await desktop.stream.start() ``` -------------------------------- ### Create Sandbox Instance (Python) Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/INDEX.md This Python snippet demonstrates how to create a sandbox instance with similar configurations to the JavaScript version. Consult configuration.md for further information. ```python # Python - See [configuration.md](configuration.md) sb = Sandbox.create( template='desktop', resolution=(1024, 768), dpi=96, timeout=300, # lifetime in seconds envs={'DISPLAY': ':0'} ) ``` -------------------------------- ### get_window_title() Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-python.md Get the title of a window by its ID. ```APIDOC ## get_window_title() ### Description Get the title of a window by its ID. ### Method `get_window_title` ### Parameters #### Path Parameters - **window_id** (`str`) - Required - Window ID ### Returns `str` - Window title ### Example ```python window_id = desktop.get_current_window_id() title = desktop.get_window_title(window_id) print(f"Window title: {title}") ``` ``` -------------------------------- ### get_application_windows() Source: https://github.com/e2b-dev/desktop/blob/main/_autodocs/sandbox-class-python.md Get all window IDs for a specific application. ```APIDOC ## get_application_windows() ### Description Get all window IDs for a specific application. ### Method `get_application_windows` ### Parameters #### Path Parameters - **application** (`str`) - Required - Application name or class ### Returns `list[str]` - List of window IDs ### Example ```python firefox_windows = desktop.get_application_windows('Firefox') chrome_windows = desktop.get_application_windows('google-chrome') ``` ```