# E2B compatibility mode
<subtitle>Use existing E2B SDK to seamlessly connect to UCloud Sandbox service. </subtitle>

UCloud Sandbox is fully compatible with the E2B SDK. You can use your existing E2B code base to directly access the UCloud Sandbox service by simply configuring environment variables.

---

## Environment configuration

### Set domain name and API Key

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

```bash
# Set UCloud Sandbox domain name
export E2B_DOMAIN=cn-wlcb.sandbox.ucloudai.com

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

# Turn off e2b API key verification
export E2B_VALIDATE_API_KEY="false"
```

> You can get your API Key at [Key Management](https://astraflow.ucloud.cn/modelverse/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 functions | `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 example

### 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 to 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 a sandbox and set a 5 minute timeout
sbx = Sandbox.create(timeout=300)

# Execute long-term tasks
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 a sandbox using a custom template
sbx = Sandbox.create(template="my-custom-template")

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

sbx.kill()
```

---

## Notes

> **API Key difference**: E2B compatibility mode uses the `E2B_API_KEY` environment variable, while the native UCloud SDK uses `UCLOUD_SANDBOX_API_KEY`. Please make sure to use the correct environment variables.

> **Recommendation**: If you are a new user, it is recommended to use [UCloud Sandbox native SDK](quickstart.md) directly to get better functional support and technical support experience.

---