OpenClaw (formerly Moltbot)

repository·main·Indexed 27 days ago

https://github.com/cloudflare/moltworker

A personal AI assistant designed to run within Cloudflare Sandbox containers. OpenClaw provides a managed, always-on deployment featuring multi-channel support for Telegram, Discord, and Slack, a web-based Control UI, and integration with Cloudflare AI Gateway, Cloudflare Access, and R2 for persistent storage. It includes a Chrome DevTools Protocol (CDP) shim for headless browser automation and scraping via the cloudflare-browser skill.

Tokens
6.5K
Snippets
11
Records
36
Agent score
95%

What's inside cloudflare-moltworker

  1. Set up Cloudflare Access for the Admin UI

    main

    The Admin UI (/_admin/) must be protected using Cloudflare Access.

    1. Enable Access: In the Workers & Pages dashboard, select your worker, go to Settings > Domains & Routes, and click Enable Cloudflare Access on the workers.dev row.
    2. Configure Permissions: In the Cloudflare Zero Trust dashboard, add your email or identity provider to the allow list for the worker application.
    3. Set Secrets: You must provide the Application Audience (AUD) tag and your Team Domain as secrets so the worker can validate JWTs.

    Required secrets:

    • CF_ACCESS_TEAM_DOMAIN: Your Cloudflare Access team domain (e.g., myteam.cloudflareaccess.com).
    • CF_ACCESS_AUD: The Application Audience (AUD) tag from your Access application settings.
    # Your Cloudflare Access team domain
    npx wrangler secret put CF_ACCESS_TEAM_DOMAIN
    
    # The Application Audience (AUD) tag
    npx wrangler secret put CF_ACCESS_AUD
  2. Configure Cloudflare AI Gateway for Anthropic Access

    main

    Instead of using a direct Anthropic API key, you can route requests through Cloudflare AI Gateway for unified billing and analytics. Set the following secrets using Wrangler:

    • CLOUDFLARE_AI_GATEWAY_API_KEY
    • CF_AI_GATEWAY_ACCOUNT_ID
    • CF_AI_GATEWAY_GATEWAY_ID
    npx wrangler secret put CLOUDFLARE_AI_GATEWAY_API_KEY
    npx wrangler secret put CF_AI_GATEWAY_ACCOUNT_ID
    npx wrangler secret put CF_AI_GATEWAY_GATEWAY_ID
  3. Configure Cloudflare AI Gateway

    main

    You can route AI requests through Cloudflare AI Gateway for caching, rate limiting, and analytics. When configured, AI Gateway takes precedence over direct ANTHROPIC_API_KEY or OPENAI_API_KEY.

    Setup

    1. Create an AI Gateway in the Cloudflare Dashboard.
    2. Set the following secrets:
    # Your AI provider's API key (e.g., Anthropic key)
    npx wrangler secret put CLOUDFLARE_AI_GATEWAY_API_KEY
    
    # Your Cloudflare account ID
    npx wrangler secret put CF_AI_GATEWAY_ACCOUNT_ID
    
    # Your AI Gateway ID
    npx wrangler secret put CF_AI_GATEWAY_GATEWAY_ID
    1. Redeploy:
    npm run deploy

    Choosing a Model

    Set CF_AI_GATEWAY_MODEL using the format provider/model-id to override the default (Claude Sonnet 4.5).

    ProviderExample CF_AI_GATEWAY_MODEL value
    Workers AIworkers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast
    OpenAIopenai/gpt-4o
    Anthropicanthropic/claude-sonnet-4-5
    Groqgroq/llama-3.3-70b

    Note: CLOUDFLARE_AI_GATEWAY_API_KEY must match the provider you are using. For Workers AI with Unified Billing, set this to your AI Gateway authentication token (cf-aig-authorization).

  4. Quick Start: Deploy OpenClaw to Cloudflare Sandbox

    main

    To deploy OpenClaw as a personal AI assistant in a Cloudflare Sandbox container, follow these steps. Note that a Workers Paid plan is required to use Cloudflare Sandboxes.

    1. Install dependencies: npm install
    2. Set Anthropic API Key: Provide your direct Anthropic key using npx wrangler secret put ANTHROPIC_API_KEY.
    3. Generate and set a Gateway Token: This token is required to access the Control UI. Use a random hex string.
    4. Deploy: Run npm run deploy.
    5. Access the UI: Open your worker URL with the token as a query parameter.

    Important: You must also set up Cloudflare Access and pair your device via the /_admin/ endpoint before the UI becomes functional.

    # Install dependencies
    npm install
    
    # Set your API key (direct Anthropic access)
    npx wrangler secret put ANTHROPIC_API_KEY
    
    # Generate and set a gateway token (required for remote access)
    export MOLTBOT_GATEWAY_TOKEN=$(openssl rand -hex 32)
    echo "Your gateway token: $MOLTBOT_GATEWAY_TOKEN"
    echo "$MOLTBOT_GATEWAY_TOKEN" | npx wrangler secret put MOLTBOT_GATEWAY_TOKEN
    
    # Deploy
    npm run deploy
  5. Setup Browser Automation (CDP)

    main

    OpenClaw includes a Chrome DevTools Protocol (CDP) shim for headless browser automation (scraping, screenshots, etc.).

    Configuration

    1. Set a shared secret for authentication:
      npx wrangler secret put CDP_SECRET
    2. Set your worker's public URL:
      npx wrangler secret put WORKER_URL
    3. Redeploy:
      npm run deploy

    Endpoints

    All endpoints require authentication via the ?secret=<CDP_SECRET> query parameter.

    EndpointDescription
    GET /cdp/json/versionBrowser version information
    GET /cdp/json/listList available browser targets
    GET /cdp/json/newCreate a new browser target
    WS /cdp/devtools/browser/{id}WebSocket connection for CDP commands
  6. Configure Cloudflare Browser Rendering

    main

    To use Cloudflare Browser Rendering, you must set the CDP_SECRET environment variable and configure a browser profile in your openclaw.json file. The cdpUrl must point to your worker endpoint including the secret as a query parameter.

    Prerequisites:

    • An environment variable named CDP_SECRET.
    • A configured browser.profiles section in openclaw.json.
    {
      "browser": {
        "profiles": {
          "cloudflare": {
            "cdpUrl": "https://your-worker.workers.dev/cdp?secret=..."
          }
        }
      }
    }
  7. Connect to the CDP WebSocket

    main

    The worker automatically creates a page target upon WebSocket connection. To interact with the page, you must listen for the Target.targetCreated event to retrieve the targetId.

    const WebSocket = require('ws');
    const CDP_SECRET = process.env.CDP_SECRET;
    const WS_URL = `wss://your-worker.workers.dev/cdp?secret=${encodeURIComponent(CDP_SECRET)}`;
    
    const ws = new WebSocket(WS_URL);
    let targetId = null;
    
    ws.on('message', (data) => {
      const msg = JSON.parse(data.toString());
      if (msg.method === 'Target.targetCreated' && msg.params?.targetInfo?.type === 'page') {
        targetId = msg.params.targetInfo.targetId;
      }
    });
  8. Local Development Configuration

    main

    For local development, create a .dev.vars file to bypass authentication and enable debugging features.

    Key variables:

    • DEV_MODE=true: Skips Cloudflare Access authentication and bypasses device pairing (enables allowInsecureAuth).
    • DEBUG_ROUTES=true: Enables /debug/* routes.
    DEV_MODE=true
    DEBUG_ROUTES=true
  9. Configure Chat Channels (Telegram, Discord, Slack)

    main

    To enable chat platform integration, set the corresponding bot tokens as secrets and redeploy your worker.

    Telegram

    npx wrangler secret put TELEGRAM_BOT_TOKEN
    npm run deploy

    Discord

    npx wrangler secret put DISCORD_BOT_TOKEN
    npm run deploy

    Slack

    npx wrangler secret put SLACK_BOT_TOKEN
    npx wrangler secret put SLACK_APP_TOKEN
    npm run deploy
  10. Enable Persistent Storage with R2

    main

    By default, data (configs, paired devices, history) is lost when the container restarts. To enable persistence, configure R2 storage using a backup/restore approach.

    1. Create R2 API Token: In the Cloudflare Dashboard, go to R2 > Overview > Manage R2 API Tokens. Create a token with Object Read & Write permissions for the moltbot-data bucket.
    2. Set Secrets: Use Wrangler to set the following credentials:
    • R2_ACCESS_KEY_ID
    • R2_SECRET_ACCESS_KEY
    • CF_ACCOUNT_ID

    Operation: A cron job syncs the configuration to R2 every 5 minutes. You can also trigger a manual backup via the /_admin/ UI.

    # R2 Access Key ID
    npx wrangler secret put R2_ACCESS_KEY_ID
    
    # R2 Secret Access Key
    npx wrangler secret put R2_SECRET_ACCESS_KEY
    
    # Your Cloudflare Account ID
    npx wrangler secret put CF_ACCOUNT_ID
  11. Quick Start: Capture Screenshots and Videos

    main

    You can use the provided scripts to perform basic browser automation tasks via the command line.

    Capture a single screenshot: Run the screenshot.js script with the target URL and output filename.

    Capture a multi-page video: Run the video.js script with a comma-separated list of URLs and an output filename.

    # Screenshot
    node /path/to/skills/cloudflare-browser/scripts/screenshot.js https://example.com output.png
    
    # Multi-page Video
    node /path/to/skills/cloudflare-browser/scripts/video.js "https://site1.com,https://site2.com" output.mp4
  12. Manage Container Lifecycle and Costs

    main

    To reduce costs, you can configure the Cloudflare Sandbox container to sleep after a period of inactivity. Without this, the container runs 24/7 and incurs full provisioned capacity costs for memory and disk.

    Set the SANDBOX_SLEEP_AFTER secret to a duration (e.g., 10m, 30m, 1h).

    Note: When the container sleeps, the next request will trigger a cold start, which may take 1-2 minutes. If R2 is configured, your data will persist across these restarts.