Start the OpenClaw gateway in an E2B sandbox, connect your browser, and auto-approve device pairing.
The OpenClaw gateway serves a web UI for chatting with agents. When exposed over a network it requires token auth and device pairing — your browser must be approved as a trusted device before it can connect.This guide starts the gateway inside an E2B sandbox, prints the URL, and programmatically approves your browser.
The gateway requires each browser to be approved as a paired device. Run this after opening the URL — it polls for pending requests and approves the first one.
Copy
Ask AI
// Make sure you've opened the gateway URL in your browser before running this.// 4. 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))}
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.
Copy
Ask AI
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))}