This topic describes how to use Computer-Use (desktop control) in ApsaraDB RDS for PostgreSQL Supabase sandboxes to build "observe, understand, and operate" automation agents with the E2B Desktop SDK and AI vision models. Five typical examples are included.
Prerequisites
Make sure that you have created an RDS Supabase instance and the instance is in the Running state.
Make sure that the instance resides in one of the following 5 regions: China (Beijing), China (Shanghai), China (Hangzhou), China (Shenzhen), or China (Chengdu).
Make sure that the
AliyunServiceRoleForRDSAISupabaseservice-linked role is authorized.Make sure that the sandbox feature is enabled on the Sandboxes and edge functions page in the RDS console. For more information, see Use sandboxes and edge functions.
Limits
The desktop sandbox is based on Ubuntu Linux. Windows and macOS desktops are not supported.
The default single-sandbox timeout is 300 seconds and can be adjusted through the
timeoutparameter. Recreate the sandbox after timeout.
Configure Computer-Use
Complete the following 6 configuration items in order to prepare the Computer-Use runtime.
Step 1: Create a desktop sandbox template
Create the desktop public template on the RDS Supabase instance details page and record the template ID for later use in your code.
In the left-side navigation pane of the instance details page, choose Components > Sandboxes and edge functions.
Confirm that the Enable sandboxes and edge functions toggle is turned on.
In the Sandbox templates section, click Template marketplace.
In the Template marketplace panel that appears on the right, find the row where the Template name column is
desktopand the Description column isImage used in computer-use scenarios. Click Create in the Actions column of that row.After the template is created, return to the Sandbox templates list. Find the row where Configuration item is
desktopand copy the full value in the Template ID column. The format isdesktop-<random-suffix>. This template ID is used as the value of theSandbox.create(template="<template-id>")parameter in subsequent code. Save it for later use.
Step 2: Bind a domain name
Bind a domain name to the IP address of the Supabase instance.
Get the IP address of the public endpoint of the instance from the instance details page.
Add a domain name mapping entry to the local
/etc/hostsfile. The following example uses<instance-id>.sb.rds.comas the domain name:<instance-ip> <instance-id>.sb.rds.com
For development and testing scenarios, write directly to /etc/hosts. For production environments, use DNS resolution.
Step 3: Download the e2b SDK patch code
Download the e2b SDK patch code from the GitHub repository. This patch adapts to the RDS Supabase routing.
Step 4: (Optional) Configure SSL for HTTPS access
To access the sandbox over HTTPS, first configure an SSL certificate for the Supabase instance. Then, set the following environment variables:
# rootCA.pem is the CA certificate of the SSL certificate generated for the Supabase instance.
export SSL_CERT_FILE="/xxx/rootCA.pem"
export REQUESTS_CA_BUNDLE="/xxx/rootCA.pem"If SSL is not configured, access falls back to HTTP.
Step 5: Configure environment variables
Set the following environment variables for the e2b SDK to connect to the Supabase instance:
# E2B_DOMAIN: The domain name bound in "Step 2: Bind a domain name".
export E2B_DOMAIN=<instance-id>.sb.rds.com
# E2B_API_KEY: The Service Key of your Supabase instance.
export E2B_API_KEY=<your-service-key>How to get the Service Key: On the instance details page, click Get API Key in the upper-right corner. In the dialog box, copy the value of ServiceKey.
In the subsequent Python code, calling patch_e2b() applies the patch to the e2b SDK. The parameter True or False indicates whether to access the sandbox over HTTPS. Passing True uses HTTPS. In this case, you must first enable HTTPS for the RDS Supabase instance and set the HTTPS-related environment variables as described in Use sandboxes and edge functions. Passing False uses HTTP.
Step 6: Install the Desktop SDK
Run the following command to install the Desktop SDK:
pip install e2b-desktopComputer-Use examples
The core workflow of Computer-Use is: receive user instruction, create Ubuntu desktop sandbox, capture screen, send screenshot to AI vision model for analysis, execute the operation returned by the model (click, input, scroll, and so on), and loop until task completion. Each of the following 5 examples provides a complete, self-contained runnable Python script. Copy a script, save it as a .py file per Run the example code, and execute it directly.
Example 1: Desktop streaming (remote viewing)
Start an interactive remote desktop, get a browser viewer URL, and observe the sandbox desktop in real time in a local browser.
from e2b_desktop import Sandbox
from patch_e2b import patch_e2b
patch_e2b(True)
desktop = Sandbox.create(template="<template-id>")
print(f"sandboxId: {desktop.sandbox_id}")
# Start desktop streaming with authentication enabled.
desktop.stream.start(require_auth=True)
auth_key = desktop.stream.get_auth_key()
print('Stream URL:', desktop.stream.get_url(auth_key=auth_key))
input("press ENTER to exit")
desktop.kill()Open the printed Stream URL in a browser to view the sandbox desktop in real time.
Example 2: UI control (keyboard and mouse)
Automatically open a browser and simulate keyboard input to implement basic UI control.
from e2b_desktop import Sandbox
from patch_e2b import patch_e2b
patch_e2b(True)
with Sandbox.create(template="<template-id>") as desktop:
# Open the browser.
desktop.launch('google-chrome')
# Enter a URL.
desktop.write('https://www.alibabacloud.com')
# Press Enter to visit.
desktop.press('enter')
input("press ENTER to exit")Example 3: Custom resolution and screenshots
Create a desktop sandbox with a specified resolution and DPI, and capture a screen image.
from e2b_desktop import Sandbox
from patch_e2b import patch_e2b
patch_e2b(True)
# Create a desktop sandbox with the specified resolution.
sandbox = Sandbox.create(
template="<template-id>",
resolution=(1024, 720),
dpi=96,
timeout=300,
)
# Start streaming and view in a browser.
sandbox.stream.start()
print("View desktop at:", sandbox.stream.get_url())
# Capture the screen.
img_bytes = sandbox.screenshot()
with open("screenshot.png", "wb") as f:
f.write(img_bytes)
print("Screenshot saved.")
sandbox.kill()Example 4: Complete desktop control API
The desktop control methods provided by the E2B Desktop SDK are categorized as follows:
Category | Methods |
Mouse operations |
|
Keyboard operations |
|
Scroll operations |
|
Screenshot |
|
Execute command |
|
Complete invocation example:
from e2b_desktop import Sandbox
from patch_e2b import patch_e2b
patch_e2b(True)
sandbox = Sandbox.create(template="<template-id>")
# —— Mouse operations ——
sandbox.left_click(500, 300) # Left click
sandbox.right_click(500, 300) # Right click
sandbox.double_click(500, 300) # Double click
sandbox.middle_click(500, 300) # Middle click
sandbox.move_mouse(500, 300) # Move mouse
sandbox.drag([100, 200], [400, 500]) # Drag
# —— Keyboard operations ——
sandbox.write("Hello, world!") # Enter text
sandbox.press("Enter") # Press a key
# —— Scroll operations ——
sandbox.scroll("down", 3) # Scroll down 3 units
sandbox.scroll("up", 3) # Scroll up 3 units
# —— Screenshot ——
img_bytes = sandbox.screenshot()
# —— Execute command ——
sandbox.commands.run("ls -la /home")
sandbox.kill()Example 5: AI vision-driven automation loop
Combine AI vision models (such as OpenAI GPT-4o or Anthropic Claude) to implement a closed automation loop of "capture screen, AI analysis, and execute operation," enabling AI to observe the screen like a human and control the desktop to complete tasks.
The specification of the operation object returned by the LLM is as follows:
Operation type | Example |
Click |
|
Enter text |
|
Key press |
|
Scroll |
|
Drag |
|
Task completion | Return |
Automation loop code example:
from e2b_desktop import Sandbox
from patch_e2b import patch_e2b
patch_e2b(True)
sandbox = Sandbox.create(
template="<template-id>",
resolution=(1024, 720),
dpi=96,
timeout=300,
)
def get_next_action_from_llm(screenshot):
"""
Send the screenshot to an AI vision model and get the next action.
This is a placeholder function. Replace it with your own LLM call.
The model should return an operation object in the format defined in the
table at the beginning of this example, or None to indicate task completion.
"""
pass
# Automation loop: capture screen, AI analysis, execute operation.
while True:
screenshot = sandbox.screenshot()
action = get_next_action_from_llm(screenshot)
if not action:
break
if action.type == "click":
sandbox.left_click(action.x, action.y)
elif action.type == "type":
sandbox.write(action.text)
elif action.type == "keypress":
sandbox.press(action.keys)
elif action.type == "scroll":
direction = "up" if action.scroll_y < 0 else "down"
sandbox.scroll(direction, abs(action.scroll_y))
elif action.type == "drag":
sandbox.drag(
[action.start_x, action.start_y],
[action.end_x, action.end_y]
)
sandbox.kill()The placeholder function get_next_action_from_llm must be integrated with your own LLM service (OpenAI Vision, Anthropic Claude Vision, and so on). This topic does not cover LLM integration details. For details, see the official documentation of the corresponding model.
Run the example code
After completing the preceding 6 configuration steps, run the Python examples in this topic as follows.
Step 1: Create the script file
Save the example code as a .py file. The file must be in the same directory as the patch_e2b source code downloaded in Step 3 of "Configure Computer-Use". Otherwise, the error ModuleNotFoundError: No module named 'patch_e2b' occurs. Example directory structure:
e2b-sdk-patch/
├── patch_e2b/ # patch source code downloaded in Step 3 of "Configure Computer-Use"
└── demo.py # your example scriptTo place the script in a different directory, add the patch_e2b source path to PYTHONPATH:
export PYTHONPATH="/path/to/e2b-sdk-patch:$PYTHONPATH"/path/to/e2b-sdk-patch is a placeholder. Replace it with the actual absolute path of the local patch_e2b source (for example, /Users/yourname/code/e2b-sdk-patch). :$PYTHONPATH preserves the existing paths so they are not overwritten. After the configuration, run the script in the same terminal window for the setting to take effect.
Step 2: Replace the placeholder in the code
Replace the <template-id> in the example code with the template ID recorded in Step 1 of "Configure Computer-Use" (format: desktop-<suffix>).
Step 3: Confirm that the environment variables are effective
In the terminal where you run the script, run the following commands to verify that the environment variables configured in Step 5 of "Configure Computer-Use" are loaded:
echo $E2B_DOMAIN # Should print <instance-id>.sb.rds.com
echo $E2B_API_KEY # Should print the Supabase ServiceKeyStep 4: Run the script
python3 demo.pyCommon startup issues
Q1: What should I do if the error ModuleNotFoundError: No module named 'patch_e2b' occurs?
A: The script is not in the patch_e2b source directory. Move the script to the patch source directory, or configure the PYTHONPATH environment variable to point to the patch source path.
Q2: What should I do if the error ModuleNotFoundError: No module named 'e2b_desktop' occurs?
A: The Desktop SDK is not installed. Run pip install e2b-desktop.
Q3: What should I do if the connection times out or DNS resolution fails?
A: The /etc/hosts file may not contain the domain mapping, or the E2B_DOMAIN environment variable may not be set. Check Step 2 and Step 5 of "Configure Computer-Use".
Q4: What should I do if 401 authentication fails?
A: The E2B_API_KEY is incorrect. On the instance details page, click Get API Key in the upper-right corner to get a new ServiceKey and update the environment variable.