This launches your OpenClaw gateway site (web UI for chatting with agents).
import { Sandbox } from 'e2b'const TOKEN = process.env.OPENCLAW_APP_TOKEN || 'my-gateway-token'const PORT = 18789// 1. Create sandboxconst sandbox = await Sandbox.create('openclaw', { envs: { OPENAI_API_KEY: process.env.OPENAI_API_KEY }, timeoutMs: 3600_000,})// 2. Set the default modelawait sandbox.commands.run('openclaw config set agents.defaults.model.primary openai/gpt-5.2')// 3. Set insecure control UI flags and start the gateway with token authawait sandbox.commands.run( `bash -lc 'openclaw config set gateway.controlUi.allowInsecureAuth true && ` + `openclaw config set gateway.controlUi.dangerouslyDisableDeviceAuth true && ` + `openclaw gateway --allow-unconfigured --bind lan --auth token --token ${TOKEN} --port ${PORT}'`, { background: true })// 4. Wait for the gateway to start listeningfor (let i = 0; i < 45; i++) { const probe = await sandbox.commands.run( `bash -lc 'ss -ltn | grep -q ":${PORT} " && echo ready || echo waiting'` ) if (probe.stdout.trim() === 'ready') break await new Promise((r) => setTimeout(r, 1000))}const url = `https://${sandbox.getHost(PORT)}/?token=${TOKEN}`console.log(`Gateway: ${url}`)
import os, timefrom e2b import SandboxTOKEN = os.environ.get("OPENCLAW_APP_TOKEN", "my-gateway-token")PORT = 18789# 1. Create sandboxsandbox = Sandbox.create("openclaw", envs={ "OPENAI_API_KEY": os.environ["OPENAI_API_KEY"],}, timeout=3600)# 2. Set the default modelsandbox.commands.run("openclaw config set agents.defaults.model.primary openai/gpt-5.2")# 3. Set insecure control UI flags and start the gateway with token authsandbox.commands.run( f"bash -lc 'openclaw config set gateway.controlUi.allowInsecureAuth true && " f"openclaw config set gateway.controlUi.dangerouslyDisableDeviceAuth true && " f"openclaw gateway --allow-unconfigured --bind lan --auth token --token {TOKEN} --port {PORT}'", background=True,)# 4. Wait for the gateway to start listeningfor _ in range(45): probe = sandbox.commands.run( f'bash -lc \'ss -ltn | grep -q ":{PORT} " && echo ready || echo waiting\'' ) if probe.stdout.strip() == "ready": break time.sleep(1)url = f"https://{sandbox.get_host(PORT)}/?token={TOKEN}"print(f"Gateway: {url}")
Visit the printed Gateway URL in your browser.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.
// 5. Poll for the browser's pending device request and approve itfor (let i = 0; i < 30; i++) { try { const res = await sandbox.commands.run( `openclaw devices list --json --url ws://127.0.0.1:${PORT} --token ${TOKEN}` ) const data = JSON.parse(res.stdout) if (data.pending?.length) { const rid = data.pending[0].requestId await sandbox.commands.run( `openclaw devices approve ${rid} --token ${TOKEN} --url ws://127.0.0.1:${PORT}` ) console.log(`Device approved: ${rid}`) break } } catch {} await new Promise((r) => setTimeout(r, 2000))}
import json# 5. Poll for the browser's pending device request and approve itfor _ in range(30): try: res = sandbox.commands.run( f"openclaw devices list --json --url ws://127.0.0.1:{PORT} --token {TOKEN}" ) data = json.loads(res.stdout) if data.get("pending"): rid = data["pending"][0]["requestId"] sandbox.commands.run( f"openclaw devices approve {rid} --token {TOKEN} --url ws://127.0.0.1:{PORT}" ) print(f"Device approved: {rid}") break except Exception: pass time.sleep(2)
Once approved, the browser connects and the gateway UI loads.
Use this when the gateway is already running and you want a clean restart (for example, after changing model or env settings).
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.
const TOKEN = process.env.OPENCLAW_APP_TOKEN || 'my-gateway-token'const PORT = 18789// 1) Kill existing gateway processes if presentawait sandbox.commands.run( `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'`)await new Promise((r) => setTimeout(r, 1000))// 2) Start gateway againawait sandbox.commands.run( `openclaw gateway --allow-unconfigured --bind lan --auth token --token ${TOKEN} --port ${PORT}`, { background: true })// 3) Wait for listening socketfor (let i = 0; i < 45; i++) { const probe = await sandbox.commands.run( `bash -lc 'ss -ltn | grep -q ":${PORT} " && echo ready || echo waiting'` ) if (probe.stdout.trim() === 'ready') break await new Promise((r) => setTimeout(r, 1000))}
import os, timeTOKEN = os.environ.get("OPENCLAW_APP_TOKEN", "my-gateway-token")PORT = 18789# 1) Kill existing gateway processes if presentsandbox.commands.run( """bash -lc 'for p in "[o]penclaw gateway" "[o]penclaw-gateway"; dofor pid in $(pgrep -f "$p" || true); do kill "$pid" >/dev/null 2>&1 || truedonedone'""")time.sleep(1)# 2) Start gateway againsandbox.commands.run( f"openclaw gateway --allow-unconfigured --bind lan --auth token --token {TOKEN} --port {PORT}", background=True,)# 3) Wait for listening socketfor _ in range(45): probe = sandbox.commands.run( f'bash -lc \'ss -ltn | grep -q ":{PORT} " && echo ready || echo waiting\'' ) if probe.stdout.strip() == "ready": break time.sleep(1)