|
| 1 | +--- |
| 2 | +title: "Deploy OpenClaw" |
| 3 | +description: "Start the OpenClaw gateway in an E2B sandbox and connect your browser." |
| 4 | +icon: "globe" |
| 5 | +--- |
| 6 | + |
| 7 | +## Quick start |
| 8 | + |
| 9 | +This launches your OpenClaw [gateway](https://docs.openclaw.ai/gateway) site (web UI for chatting with agents). |
| 10 | + |
| 11 | +<CodeGroup> |
| 12 | +```typescript JavaScript & TypeScript |
| 13 | +import { Sandbox } from 'e2b' |
| 14 | + |
| 15 | +const TOKEN = process.env.OPENCLAW_APP_TOKEN || 'my-gateway-token' |
| 16 | +const PORT = 18789 |
| 17 | + |
| 18 | +// 1. Create sandbox |
| 19 | +const sandbox = await Sandbox.create('openclaw', { |
| 20 | + envs: { OPENAI_API_KEY: process.env.OPENAI_API_KEY }, |
| 21 | + timeoutMs: 3600_000, |
| 22 | +}) |
| 23 | + |
| 24 | +// 2. Set the default model |
| 25 | +await sandbox.commands.run('openclaw config set agents.defaults.model.primary openai/gpt-5.2') |
| 26 | + |
| 27 | +// 3. Set insecure control UI flags and start the gateway with token auth |
| 28 | +await sandbox.commands.run( |
| 29 | + `bash -lc 'openclaw config set gateway.controlUi.allowInsecureAuth true && ` + |
| 30 | + `openclaw config set gateway.controlUi.dangerouslyDisableDeviceAuth true && ` + |
| 31 | + `openclaw gateway --allow-unconfigured --bind lan --auth token --token ${TOKEN} --port ${PORT}'`, |
| 32 | + { background: true } |
| 33 | +) |
| 34 | + |
| 35 | +// 4. Wait for the gateway to start listening |
| 36 | +for (let i = 0; i < 45; i++) { |
| 37 | + const probe = await sandbox.commands.run( |
| 38 | + `bash -lc 'ss -ltn | grep -q ":${PORT} " && echo ready || echo waiting'` |
| 39 | + ) |
| 40 | + if (probe.stdout.trim() === 'ready') break |
| 41 | + await new Promise((r) => setTimeout(r, 1000)) |
| 42 | +} |
| 43 | + |
| 44 | +const url = `https://${sandbox.getHost(PORT)}/?token=${TOKEN}` |
| 45 | +console.log(`Gateway: ${url}`) |
| 46 | +``` |
| 47 | +```python Python |
| 48 | +import os, time |
| 49 | +from e2b import Sandbox |
| 50 | + |
| 51 | +TOKEN = os.environ.get("OPENCLAW_APP_TOKEN", "my-gateway-token") |
| 52 | +PORT = 18789 |
| 53 | + |
| 54 | +# 1. Create sandbox |
| 55 | +sandbox = Sandbox.create("openclaw", envs={ |
| 56 | + "OPENAI_API_KEY": os.environ["OPENAI_API_KEY"], |
| 57 | +}, timeout=3600) |
| 58 | + |
| 59 | +# 2. Set the default model |
| 60 | +sandbox.commands.run("openclaw config set agents.defaults.model.primary openai/gpt-5.2") |
| 61 | + |
| 62 | +# 3. Set insecure control UI flags and start the gateway with token auth |
| 63 | +sandbox.commands.run( |
| 64 | + f"bash -lc 'openclaw config set gateway.controlUi.allowInsecureAuth true && " |
| 65 | + f"openclaw config set gateway.controlUi.dangerouslyDisableDeviceAuth true && " |
| 66 | + f"openclaw gateway --allow-unconfigured --bind lan --auth token --token {TOKEN} --port {PORT}'", |
| 67 | + background=True, |
| 68 | +) |
| 69 | + |
| 70 | +# 4. Wait for the gateway to start listening |
| 71 | +for _ in range(45): |
| 72 | + probe = sandbox.commands.run( |
| 73 | + f'bash -lc \'ss -ltn | grep -q ":{PORT} " && echo ready || echo waiting\'' |
| 74 | + ) |
| 75 | + if probe.stdout.strip() == "ready": |
| 76 | + break |
| 77 | + time.sleep(1) |
| 78 | + |
| 79 | +url = f"https://{sandbox.get_host(PORT)}/?token={TOKEN}" |
| 80 | +print(f"Gateway: {url}") |
| 81 | +``` |
| 82 | +</CodeGroup> |
| 83 | + |
| 84 | +Visit the printed `Gateway` URL in your browser. |
| 85 | + |
| 86 | +If you run in secure mode (set `gateway.controlUi.dangerouslyDisableDeviceAuth false`), run this after opening the URL to poll pending pairing requests and approve the first one. |
| 87 | + |
| 88 | +<CodeGroup> |
| 89 | +```typescript JavaScript & TypeScript |
| 90 | +// 5. Poll for the browser's pending device request and approve it |
| 91 | +for (let i = 0; i < 30; i++) { |
| 92 | + try { |
| 93 | + const res = await sandbox.commands.run( |
| 94 | + `openclaw devices list --json --url ws://127.0.0.1:${PORT} --token ${TOKEN}` |
| 95 | + ) |
| 96 | + const data = JSON.parse(res.stdout) |
| 97 | + if (data.pending?.length) { |
| 98 | + const rid = data.pending[0].requestId |
| 99 | + await sandbox.commands.run( |
| 100 | + `openclaw devices approve ${rid} --token ${TOKEN} --url ws://127.0.0.1:${PORT}` |
| 101 | + ) |
| 102 | + console.log(`Device approved: ${rid}`) |
| 103 | + break |
| 104 | + } |
| 105 | + } catch {} |
| 106 | + await new Promise((r) => setTimeout(r, 2000)) |
| 107 | +} |
| 108 | +``` |
| 109 | +```python Python |
| 110 | +import json |
| 111 | + |
| 112 | +# 5. Poll for the browser's pending device request and approve it |
| 113 | +for _ in range(30): |
| 114 | + try: |
| 115 | + res = sandbox.commands.run( |
| 116 | + f"openclaw devices list --json --url ws://127.0.0.1:{PORT} --token {TOKEN}" |
| 117 | + ) |
| 118 | + data = json.loads(res.stdout) |
| 119 | + if data.get("pending"): |
| 120 | + rid = data["pending"][0]["requestId"] |
| 121 | + sandbox.commands.run( |
| 122 | + f"openclaw devices approve {rid} --token {TOKEN} --url ws://127.0.0.1:{PORT}" |
| 123 | + ) |
| 124 | + print(f"Device approved: {rid}") |
| 125 | + break |
| 126 | + except Exception: |
| 127 | + pass |
| 128 | + time.sleep(2) |
| 129 | +``` |
| 130 | +</CodeGroup> |
| 131 | + |
| 132 | +Once approved, the browser connects and the gateway UI loads. |
| 133 | + |
| 134 | +## How it works |
| 135 | + |
| 136 | +| Step | What happens | |
| 137 | +|------|-------------| |
| 138 | +| `--bind lan` | Gateway listens on `0.0.0.0` so E2B can proxy it | |
| 139 | +| `--auth token` | Requires `?token=` on the URL for HTTP and WebSocket auth | |
| 140 | +| Browser opens URL | Gateway serves the UI, browser opens a WebSocket | |
| 141 | +| `code=1008 pairing required` | Gateway closes the WebSocket until the device is approved (secure mode only) | |
| 142 | +| `devices approve` | Approves the browser's device fingerprint (secure mode only) | |
| 143 | +| Browser reconnects | WebSocket connects successfully, UI is live | |
| 144 | + |
| 145 | +## Gateway flags reference |
| 146 | + |
| 147 | +| Flag | Purpose | |
| 148 | +|------|---------| |
| 149 | +| `--allow-unconfigured` | Start without a full config file | |
| 150 | +| `--bind lan` | Bind to `0.0.0.0` (required for E2B port proxying) | |
| 151 | +| `--auth token` | Enable token-based authentication | |
| 152 | +| `--token <value>` | The auth token (passed as `?token=` in the URL) | |
| 153 | +| `--port <number>` | Gateway listen port (default: `18789`) | |
| 154 | + |
| 155 | +## How to restart the gateway |
| 156 | + |
| 157 | +Use this when the gateway is already running and you want a clean restart (for example, after changing model or env settings). |
| 158 | + |
| 159 | +<Info> |
| 160 | +We can't use the `openclaw gateway restart` command here. Some SDK environments cannot target a specific Unix user in `commands.run`. The commands below use the default command user context. |
| 161 | +</Info> |
| 162 | + |
| 163 | +<CodeGroup> |
| 164 | +```typescript JavaScript & TypeScript |
| 165 | +const TOKEN = process.env.OPENCLAW_APP_TOKEN || 'my-gateway-token' |
| 166 | +const PORT = 18789 |
| 167 | + |
| 168 | +// 1) Kill existing gateway processes if present |
| 169 | +await sandbox.commands.run( |
| 170 | + `bash -lc 'for p in "[o]penclaw gateway" "[o]penclaw-gateway"; do for pid in $(pgrep -f "$p" || true); do kill "$pid" >/dev/null 2>&1 || true; done; done'` |
| 171 | +) |
| 172 | +await new Promise((r) => setTimeout(r, 1000)) |
| 173 | + |
| 174 | +// 2) Start gateway again |
| 175 | +await sandbox.commands.run( |
| 176 | + `openclaw gateway --allow-unconfigured --bind lan --auth token --token ${TOKEN} --port ${PORT}`, |
| 177 | + { background: true } |
| 178 | +) |
| 179 | + |
| 180 | +// 3) Wait for listening socket |
| 181 | +for (let i = 0; i < 45; i++) { |
| 182 | + const probe = await sandbox.commands.run( |
| 183 | + `bash -lc 'ss -ltn | grep -q ":${PORT} " && echo ready || echo waiting'` |
| 184 | + ) |
| 185 | + if (probe.stdout.trim() === 'ready') break |
| 186 | + await new Promise((r) => setTimeout(r, 1000)) |
| 187 | +} |
| 188 | +``` |
| 189 | +```python Python |
| 190 | +import os, time |
| 191 | + |
| 192 | +TOKEN = os.environ.get("OPENCLAW_APP_TOKEN", "my-gateway-token") |
| 193 | +PORT = 18789 |
| 194 | + |
| 195 | +# 1) Kill existing gateway processes if present |
| 196 | +sandbox.commands.run( |
| 197 | + """bash -lc 'for p in "[o]penclaw gateway" "[o]penclaw-gateway"; do |
| 198 | +for pid in $(pgrep -f "$p" || true); do |
| 199 | + kill "$pid" >/dev/null 2>&1 || true |
| 200 | +done |
| 201 | +done'""" |
| 202 | +) |
| 203 | +time.sleep(1) |
| 204 | + |
| 205 | +# 2) Start gateway again |
| 206 | +sandbox.commands.run( |
| 207 | + f"openclaw gateway --allow-unconfigured --bind lan --auth token --token {TOKEN} --port {PORT}", |
| 208 | + background=True, |
| 209 | +) |
| 210 | + |
| 211 | +# 3) Wait for listening socket |
| 212 | +for _ in range(45): |
| 213 | + probe = sandbox.commands.run( |
| 214 | + f'bash -lc \'ss -ltn | grep -q ":{PORT} " && echo ready || echo waiting\'' |
| 215 | + ) |
| 216 | + if probe.stdout.strip() == "ready": |
| 217 | + break |
| 218 | + time.sleep(1) |
| 219 | +``` |
| 220 | +</CodeGroup> |
| 221 | + |
| 222 | +## Turn insecure flags off (recommended after testing) |
| 223 | + |
| 224 | +Use this to restore secure device authentication after initial testing. |
| 225 | + |
| 226 | +<CodeGroup> |
| 227 | +```typescript JavaScript & TypeScript |
| 228 | +const TOKEN = process.env.OPENCLAW_APP_TOKEN || 'my-gateway-token' |
| 229 | +const PORT = 18789 |
| 230 | + |
| 231 | +await sandbox.commands.run( |
| 232 | + `bash -lc 'openclaw config set gateway.controlUi.allowInsecureAuth false && ` + |
| 233 | + `openclaw config set gateway.controlUi.dangerouslyDisableDeviceAuth false'` |
| 234 | +) |
| 235 | + |
| 236 | +await sandbox.commands.run( |
| 237 | + `bash -lc 'for p in "[o]penclaw gateway" "[o]penclaw-gateway"; do for pid in $(pgrep -f "$p" || true); do kill "$pid" >/dev/null 2>&1 || true; done; done'` |
| 238 | +) |
| 239 | + |
| 240 | +await sandbox.commands.run( |
| 241 | + `openclaw gateway --allow-unconfigured --bind lan --auth token --token ${TOKEN} --port ${PORT}`, |
| 242 | + { background: true } |
| 243 | +) |
| 244 | +``` |
| 245 | +```python Python |
| 246 | +import os |
| 247 | + |
| 248 | +TOKEN = os.environ.get("OPENCLAW_APP_TOKEN", "my-gateway-token") |
| 249 | +PORT = 18789 |
| 250 | + |
| 251 | +sandbox.commands.run( |
| 252 | + "bash -lc 'openclaw config set gateway.controlUi.allowInsecureAuth false && " |
| 253 | + "openclaw config set gateway.controlUi.dangerouslyDisableDeviceAuth false'" |
| 254 | +) |
| 255 | + |
| 256 | +sandbox.commands.run( |
| 257 | + """bash -lc 'for p in "[o]penclaw gateway" "[o]penclaw-gateway"; do |
| 258 | +for pid in $(pgrep -f "$p" || true); do |
| 259 | + kill "$pid" >/dev/null 2>&1 || true |
| 260 | +done |
| 261 | +done'""" |
| 262 | +) |
| 263 | + |
| 264 | +sandbox.commands.run( |
| 265 | + f"openclaw gateway --allow-unconfigured --bind lan --auth token --token {TOKEN} --port {PORT}", |
| 266 | + background=True, |
| 267 | +) |
| 268 | +``` |
| 269 | +</CodeGroup> |
| 270 | + |
| 271 | +## Related |
| 272 | + |
| 273 | +<CardGroup cols={1}> |
| 274 | + <Card title="OpenClaw Telegram" icon="message-circle" href="/docs/agents/openclaw/openclaw-telegram"> |
| 275 | + Connect OpenClaw to Telegram and approve pairing |
| 276 | + </Card> |
| 277 | +</CardGroup> |
0 commit comments