# E2B Compatibility Mode
<subtitle>Seamlessly integrate UCloud Sandbox service using existing E2B SDK.</subtitle>

UCloud Sandbox is fully compatible with E2B SDK. You only need to simply configure environment variables to use your existing E2B codebase to directly access UCloud Sandbox service.

---

## Environment Configuration

### Set Domain and API Key

Before using the E2B SDK, you need to configure the following environment variables:

```bash
# Set UCloud Sandbox domain
export E2B_DOMAIN=sandbox.ucloudai.com

# Set your Modelverse API Key
export E2B_API_KEY="<Your Modelverse API Key>"
```

>
> You can obtain your API Key from the [Console API Keys page](https://console.ucloud.cn/modelverse/experience/api-keys).

---

## Supported SDK Packages

UCloud Sandbox is compatible with the following E2B SDK packages:

| SDK Package | Purpose | Installation Command |
|--------|------|----------|
| `e2b_code_interpreter` | Code interpretation and execution | `pip install e2b-code-interpreter` |
| `e2b` | Basic sandbox functionality | `pip install e2b` |
| `e2b_desktop` | Desktop environment sandbox | `pip install e2b-desktop` |

---

## Quick Start

### Basic Example

```python
from e2b_code_interpreter import Sandbox
# or from e2b import Sandbox
# or from e2b_desktop import Sandbox

# Create sandbox
sbx = Sandbox.create()

# Execute command
execution = sbx.commands.run('ls -l')
print(execution)

# Destroy sandbox
sbx.kill()
```

---

## Complete Examples

### Code Execution

```python
from e2b_code_interpreter import Sandbox

# Create sandbox
sbx = Sandbox.create()

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

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

# Execute Python script
result = sbx.commands.run('python test.py')
print(result.stdout)

# Read file
content = sbx.files.read("test.py")
print(f"File content: {content}")

# Destroy sandbox
sbx.kill()
```

### Timeout Control

```python
from e2b_code_interpreter import Sandbox

# Create sandbox with 5-minute timeout
sbx = Sandbox.create(timeout=300)

# Execute long-running task
result = sbx.commands.run('sleep 10 && echo "Done!"')
print(result.stdout)

# Manual destruction
sbx.kill()
```

### Custom Template

```python
from e2b_code_interpreter import Sandbox

# Create sandbox using custom template
sbx = Sandbox.create(template="my-custom-template")

# Verify environment
result = sbx.commands.run('echo $MY_ENV_VAR')
print(result.stdout)

sbx.kill()
```

---

## API Comparison Table

The main methods of E2B SDK are all supported in UCloud Sandbox:

| E2B Method | Description | Support Status |
|----------|------|----------|
| `Sandbox.create()` | Create sandbox | ✅ |
| `sbx.commands.run()` | Execute command | ✅ |
| `sbx.files.write()` | Write file | ✅ |
| `sbx.files.read()` | Read file | ✅ |
| `sbx.files.list()` | List directory | ✅ |
| `sbx.kill()` | Destroy sandbox | ✅ |
| `sbx.set_timeout()` | Set timeout | ✅ |

---

## Migration Guide

If you already have code using E2B SDK, migrating to UCloud Sandbox is very simple:

1. **No code changes needed**: Keep existing E2B SDK imports and usage unchanged
2. **Configure environment variables**: Set `E2B_DOMAIN` and `E2B_API_KEY`
3. **Run code**: Your code will automatically use UCloud Sandbox service

```bash
# After setting environment variables, directly run existing code
export E2B_DOMAIN=sandbox.ucloudai.com
export E2B_API_KEY="your-api-key"

python your_e2b_script.py
```

---

## Notes

>
> **API Key Difference**: E2B compatibility mode uses the `E2B_API_KEY` environment variable, while the native UCloud SDK uses `AGENTBOX_API_KEY`. Please ensure you use the correct environment variable.

>
> **Recommendation**: If you are a new user, we recommend directly using the [UCloud Sandbox Native SDK](/docs/agent-sandbox/product/00-quick-start) for better feature support and technical support experience.

---

## Next Steps

- [SDK Quick Start](/docs/agent-sandbox/product/00-quick-start) - Use native UCloud Sandbox SDK
- [Sandbox Lifecycle](/docs/agent-sandbox/sdk/sandbox/01-lifecycle) - Understand sandbox management
- [Command Execution](/docs/agent-sandbox/sdk/commands/01-overview) - Detailed command execution guide
