> ## Documentation Index
> Fetch the complete documentation index at: https://docs.e2b.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenClaw

> Run OpenClaw in a secure E2B sandbox with full filesystem, terminal, and git access.

[OpenClaw](https://openclaw.ai) is an open-source personal AI assistant that supports multiple LLM providers and messaging channels. E2B provides a pre-built `openclaw` template with OpenClaw already installed.

## CLI

Create a sandbox with the [E2B CLI](https://docs.e2b.dev/cli.md).

```bash theme={"theme":"css-variables"}
e2b sbx create openclaw
```

Once inside the sandbox, start OpenClaw.

```bash theme={"theme":"css-variables"}
openclaw agent --local --message "Hello"
```

## Run headless

Use `openclaw agent --message` for non-interactive mode. Pass `--local` to use API keys from environment variables and `--thinking` to control reasoning depth.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('openclaw', {
  envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
})

const result = await sandbox.commands.run(
  `openclaw agent --local --thinking high --message "Create a hello world HTTP server in Go"`
)

console.log(result.stdout)
await sandbox.kill()
```

```python Python theme={"theme":"css-variables"}
import os
from e2b import Sandbox

sandbox = Sandbox.create("openclaw", envs={
    "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
})

result = sandbox.commands.run(
    'openclaw agent --local --thinking high --message "Create a hello world HTTP server in Go"',
)

print(result.stdout)
sandbox.kill()
```

### Example: work on a cloned repository

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('openclaw', {
  envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
  timeoutMs: 600_000,
})

await sandbox.commands.run(
  'git clone --depth 1 https://x-access-token:$GITHUB_TOKEN@github.com/<owner>/<repo>.git /home/user/repo',
  { envs: { GITHUB_TOKEN: process.env.GITHUB_TOKEN } }
)

const result = await sandbox.commands.run(
  `cd /home/user/repo && openclaw agent --local --thinking high --message "Add error handling to all API endpoints"`,
  { onStdout: (data) => process.stdout.write(data) }
)

const diff = await sandbox.commands.run('cd /home/user/repo && git diff')
console.log(diff.stdout)

await sandbox.kill()
```

```python Python theme={"theme":"css-variables"}
import os
from e2b import Sandbox

sandbox = Sandbox.create("openclaw", envs={
    "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
}, timeout=600)

sandbox.commands.run(
    "git clone --depth 1 https://x-access-token:$GITHUB_TOKEN@github.com/<owner>/<repo>.git /home/user/repo",
    envs={"GITHUB_TOKEN": os.environ["GITHUB_TOKEN"]},
)

result = sandbox.commands.run(
    'cd /home/user/repo && openclaw agent --local --thinking high --message "Add error handling to all API endpoints"',
    on_stdout=lambda data: print(data, end=""),
)

diff = sandbox.commands.run("cd /home/user/repo && git diff")
print(diff.stdout)

sandbox.kill()
```

## Structured output

Use `--json` to get machine-readable responses with structured payload and metadata.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('openclaw', {
  envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
})

const result = await sandbox.commands.run(
  `openclaw agent --local --json --message "List all files in the current directory and describe each"`
)

const response = JSON.parse(result.stdout)
console.log(response)

await sandbox.kill()
```

```python Python theme={"theme":"css-variables"}
import os
import json
from e2b import Sandbox

sandbox = Sandbox.create("openclaw", envs={
    "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
})

result = sandbox.commands.run(
    'openclaw agent --local --json --message "List all files in the current directory and describe each"',
)

response = json.loads(result.stdout)
print(response)

sandbox.kill()
```

## Customize SOUL.md

[SOUL.md](https://docs.openclaw.ai/reference/templates/SOUL) defines the agent's personality, boundaries, and operating principles. Write it into the workspace before running the agent.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('openclaw', {
  envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
})

await sandbox.files.write('/home/user/.openclaw/workspaces/default/SOUL.md', `
# Soul

## Core Truths
- Be genuinely helpful, not performatively helpful
- Be resourceful before asking — check the workspace, search the web, read docs

## Boundaries
- Never share user data outside this session
- Be cautious with external actions (git push, API calls, file deletion)

## Vibe
Concise when needed, thorough when it matters.
`)

const result = await sandbox.commands.run(
  `openclaw agent --local --thinking high --message "Introduce yourself"`,
  { onStdout: (data) => process.stdout.write(data) }
)

await sandbox.kill()
```

```python Python theme={"theme":"css-variables"}
import os
from e2b import Sandbox

sandbox = Sandbox.create("openclaw", envs={
    "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
})

sandbox.files.write("/home/user/.openclaw/workspaces/default/SOUL.md", """
# Soul

## Core Truths
- Be genuinely helpful, not performatively helpful
- Be resourceful before asking — check the workspace, search the web, read docs

## Boundaries
- Never share user data outside this session
- Be cautious with external actions (git push, API calls, file deletion)

## Vibe
Concise when needed, thorough when it matters.
""")

result = sandbox.commands.run(
    'openclaw agent --local --thinking high --message "Introduce yourself"',
    on_stdout=lambda data: print(data, end=""),
)

sandbox.kill()
```

## Customize HEARTBEAT.md

[HEARTBEAT.md](https://docs.openclaw.ai/gateway/heartbeat) is a periodic checklist the agent reviews on a recurring schedule (roughly every 30 minutes). Use it for background task reminders.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('openclaw', {
  envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
})

await sandbox.files.write('/home/user/.openclaw/workspaces/default/HEARTBEAT.md', `
# Heartbeat Checklist

- Check for new issues in the repository
- Run the test suite if any files changed
- Review open pull requests for stale reviews
`)

const result = await sandbox.commands.run(
  `openclaw agent --local --message "Start monitoring the project"`,
  { onStdout: (data) => process.stdout.write(data) }
)

await sandbox.kill()
```

```python Python theme={"theme":"css-variables"}
import os
from e2b import Sandbox

sandbox = Sandbox.create("openclaw", envs={
    "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
})

sandbox.files.write("/home/user/.openclaw/workspaces/default/HEARTBEAT.md", """
# Heartbeat Checklist

- Check for new issues in the repository
- Run the test suite if any files changed
- Review open pull requests for stale reviews
""")

result = sandbox.commands.run(
    'openclaw agent --local --message "Start monitoring the project"',
    on_stdout=lambda data: print(data, end=""),
)

sandbox.kill()
```

## Add skills

OpenClaw supports a [skills ecosystem](https://docs.openclaw.ai/tools/skills) via [ClawHub](https://clawhub.com). Skills are markdown files (`SKILL.md`) that extend the agent with additional tools. Install them with the `clawhub` CLI or write them directly into the workspace.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('openclaw', {
  envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
})

// Install a skill from ClawHub
await sandbox.commands.run('clawhub install <skill-slug>')

const result = await sandbox.commands.run(
  `openclaw agent --local --message "Use the installed skill to complete the task"`,
  { onStdout: (data) => process.stdout.write(data) }
)

await sandbox.kill()
```

```python Python theme={"theme":"css-variables"}
import os
from e2b import Sandbox

sandbox = Sandbox.create("openclaw", envs={
    "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
})

# Install a skill from ClawHub
sandbox.commands.run("clawhub install <skill-slug>")

result = sandbox.commands.run(
    'openclaw agent --local --message "Use the installed skill to complete the task"',
    on_stdout=lambda data: print(data, end=""),
)

sandbox.kill()
```

Browse available skills at [clawhub.com](https://clawhub.com).

You can also write a custom skill as a [SKILL.md](https://docs.openclaw.ai/tools/skills#format-agentskills-+-pi-compatible) file directly into the workspace.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('openclaw', {
  envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
})

// Write a custom skill
await sandbox.files.write('/home/user/.openclaw/workspaces/default/SKILL.md', `---
name: code-reviewer
description: Review code changes for bugs, security issues, and style violations.
user-invocable: true
---

# Code Reviewer

When asked to review code, follow these steps:

1. Read the changed files
2. Check for common bugs and security issues
3. Verify error handling is present
4. Output a structured review with severity levels
`)

const result = await sandbox.commands.run(
  `openclaw agent --local --message "Review the latest changes in this project"`,
  { onStdout: (data) => process.stdout.write(data) }
)

await sandbox.kill()
```

```python Python theme={"theme":"css-variables"}
import os
from e2b import Sandbox

sandbox = Sandbox.create("openclaw", envs={
    "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
})

# Write a custom skill
sandbox.files.write("/home/user/.openclaw/workspaces/default/SKILL.md", """---
name: code-reviewer
description: Review code changes for bugs, security issues, and style violations.
user-invocable: true
---

# Code Reviewer

When asked to review code, follow these steps:

1. Read the changed files
2. Check for common bugs and security issues
3. Verify error handling is present
4. Output a structured review with severity levels
""")

result = sandbox.commands.run(
    'openclaw agent --local --message "Review the latest changes in this project"',
    on_stdout=lambda data: print(data, end=""),
)

sandbox.kill()
```

## Launch the gateway

OpenClaw has a built-in [web UI and chat interface](https://openclaw.ai) served by its gateway. Start it inside a sandbox and connect from your browser.

> [!WARNING]
>
> This sandbox is created with a 10-minute timeout and auto-pause enabled. After 10 minutes of inactivity it pauses and can be resumed later. See [Sandbox Persistence](https://docs.e2b.dev/sandbox/persistence.md) and [Sandbox Lifecycle](https://docs.e2b.dev/sandbox.md) for more details.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('openclaw', {
  envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
  lifecycle: {
    onTimeout: 'pause',
  },
  timeoutMs: 10 * 60 * 1000,
})

// Start the gateway
sandbox.commands.run('openclaw gateway --port 18789 --verbose')

const host = sandbox.getHost(18789)
const url = `https://${host}`
console.log(`OpenClaw Gateway: ${url}`)
console.log(`Sandbox ID: ${sandbox.sandboxId}`)
```

```python Python theme={"theme":"css-variables"}
import os
from e2b import Sandbox

sandbox = Sandbox.beta_create("openclaw", envs={
    "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
}, auto_pause=True, timeout=10 * 60)

# Start the gateway
sandbox.commands.run("openclaw gateway --port 18789 --verbose")

host = sandbox.get_host(18789)
url = f"https://{host}"
print(f"OpenClaw Gateway: {url}")
print(f"Sandbox ID: {sandbox.sandbox_id}")
```

## Build a custom template

If you need to customize the environment (e.g. pre-install dependencies, add config files), build your own template on top of the pre-built `openclaw` template.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
// template.ts
import { Template, waitForPort } from 'e2b'

export const template = Template()
  .fromTemplate('openclaw')
  // Optional - start the OpenClaw gateway on sandbox start
  .setStartCmd(
    'openclaw gateway --port 18789 --verbose',
    waitForPort(18789)
  )
```

```python Python theme={"theme":"css-variables"}
# template.py
from e2b import Template, wait_for_port

template = (
    Template()
    .from_template("openclaw")
    # Optional - start the OpenClaw gateway on sandbox start
    .set_start_cmd(
        "openclaw gateway --port 18789 --verbose",
        wait_for_port(18789)
    )
)
```

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
// build.ts
import { Template, defaultBuildLogger } from 'e2b'
import { template as openClawTemplate } from './template'

await Template.build(openClawTemplate, 'my-openclaw', {
  cpuCount: 2,
  memoryMB: 2048,
  onBuildLogs: defaultBuildLogger(),
})
```

```python Python theme={"theme":"css-variables"}
# build.py
from e2b import Template, default_build_logger
from template import template as openclaw_template

Template.build(openclaw_template, "my-openclaw",
    cpu_count=2,
    memory_mb=2048,
    on_build_logs=default_build_logger(),
)
```

Run the build script to create the template.

```bash JavaScript & TypeScript theme={"theme":"css-variables"}
npx tsx build.ts
```

```bash Python theme={"theme":"css-variables"}
python build.py
```

## Related guides

> [card] [Sandbox persistence](https://docs.e2b.dev/sandbox/persistence.md)
>
> Auto-pause, resume, and manage sandbox lifecycle

> [card] [SSH access](https://docs.e2b.dev/sandbox/ssh-access.md)
>
> Connect to the sandbox via SSH for interactive sessions
