# E2B 兼容模式
<subtitle>使用现有的 E2B SDK 无缝接入 UCloud Sandbox 服务。</subtitle>

UCloud Sandbox 完全兼容 E2B SDK，您只需简单配置环境变量即可使用现有的 E2B 代码库直接接入 UCloud Sandbox 服务。

---

## 环境配置

### 设置域名和 API Key

在使用 E2B SDK 之前，您需要配置以下环境变量：

```bash
# 设置 UCloud Sandbox 域名
export E2B_DOMAIN=cn-wlcb.sandbox.ucloudai.com

# 设置您的 Modelverse API Key
export E2B_API_KEY="<Your Modelverse API Key>"

# 关闭 e2b API key 校验
export E2B_VALIDATE_API_KEY="false"
```

> 您可以在 [密钥管理](https://astraflow.ucloud.cn/modelverse/api-keys) 获取您的 API Key。

---

## 支持的 SDK 包

UCloud Sandbox 兼容以下 E2B SDK 包：

| SDK 包 | 用途 | 安装命令 |
|--------|------|----------|
| `e2b_code_interpreter` | 代码解释执行 | `pip install e2b-code-interpreter` |
| `e2b` | 基础沙箱功能 | `pip install e2b` |
| `e2b_desktop` | 桌面环境沙箱 | `pip install e2b-desktop` |

---

## 快速开始

### 基础示例

```python
from e2b_code_interpreter import Sandbox
# 或者 from e2b import Sandbox
# 或者 from e2b_desktop import Sandbox

# 创建沙箱
sbx = Sandbox.create()

# 执行命令
execution = sbx.commands.run('ls -l')
print(execution)

# 销毁沙箱
sbx.kill()
```

---

## 完整示例

### 代码执行

```python
from e2b_code_interpreter import Sandbox

# 创建沙箱
sbx = Sandbox.create()

# 执行 shell 命令
result = sbx.commands.run('echo "Hello from UCloud Sandbox!"')
print(f"stdout: {result.stdout}")
print(f"exit_code: {result.exit_code}")

# 写入文件
sbx.files.write("test.py", """
print("Hello, World!")
for i in range(5):
    print(f"Count: {i}")
""")

# 执行 Python 脚本
result = sbx.commands.run('python test.py')
print(result.stdout)

# 读取文件
content = sbx.files.read("test.py")
print(f"File content: {content}")

# 销毁沙箱
sbx.kill()
```

### 超时控制

```python
from e2b_code_interpreter import Sandbox

# 创建沙箱，设置 5 分钟超时
sbx = Sandbox.create(timeout=300)

# 执行长时间任务
result = sbx.commands.run('sleep 10 && echo "Done!"')
print(result.stdout)

# 手动销毁
sbx.kill()
```

### 自定义模板

```python
from e2b_code_interpreter import Sandbox

# 使用自定义模板创建沙箱
sbx = Sandbox.create(template="my-custom-template")

# 验证环境
result = sbx.commands.run('echo $MY_ENV_VAR')
print(result.stdout)

sbx.kill()
```

---

## 注意事项

> **API Key 区别**：E2B 兼容模式使用 `E2B_API_KEY` 环境变量，而原生 UCloud SDK 使用 `UCLOUD_SANDBOX_API_KEY`。请确保使用正确的环境变量。

> **推荐**：如果您是新用户，建议直接使用 [UCloud Sandbox 原生 SDK](quickstart.md)，以获得更好的功能支持和技术支持体验。

---