6.9 配置示例

本文档提供 OpenClaw 网关的常用配置示例,帮助您快速了解和应用各种配置场景。

基本配置示例

最小化配置

最简单的网关配置示例:

{
  "gateway": {
    "mode": "local",
    "port": 18789,
    "bind": "loopback"
  }
}

完整本地配置

{
  "gateway": {
    "mode": "local",
    "port": 18789,
    "bind": "loopback",
    "controlUi": {
      "enabled": true,
      "basePath": "/openclaw"
    },
    "auth": {
      "enabled": true
    },
    "reload": {
      "enabled": true
    }
  }
}

多代理配置示例

配置多个 AI 代理:

{
  "agents": {
    "defaults": {
      "models": [
        "anthropic:claude-3-5-sonnet",
        "openai:gpt-4"
      ],
      "systemPrompt": "你是一个有帮助的 AI 助手。"
    },
    "custom": {
      "coder": {
        "models": ["anthropic:claude-3-5-sonnet"],
        "systemPrompt": "你是一个专业的编程助手。",
        "tools": ["exec", "read", "write", "grep"]
      },
      "researcher": {
        "models": ["openai:gpt-4"],
        "systemPrompt": "你是一个研究助手。",
        "tools": ["web_search", "read"]
      }
    }
  }
}

模型提供商配置

配置多个提供商

{
  "models": {
    "providers": {
      "anthropic": {
        "apiKey": "${ANTHROPIC_API_KEY}",
        "baseUrl": "https://api.anthropic.com"
      },
      "openai": {
        "apiKey": "${OPENAI_API_KEY}",
        "baseUrl": "https://api.openai.com/v1"
      },
      "local": {
        "type": "ollama",
        "baseUrl": "http://localhost:11434"
      }
    }
  }
}

沙箱配置示例

启用沙箱保护

{
  "sandbox": {
    "enabled": true,
    "policy": {
      "allowedCommands": [
        "git",
        "npm",
        "node",
        "python3"
      ],
      "blockedPaths": [
        "/etc/passwd",
        "/etc/shadow",
        "~/.ssh"
      ],
      "allowNetwork": true,
      "maxProcesses": 10
    }
  }
}

渠道特定配置

WhatsApp 配置

{
  "channels": {
    "whatsapp": {
      "enabled": true,
      "phoneNumber": "+1234567890",
      "apiKey": "${WHATSAPP_API_KEY}",
      "webhookSecret": "${WHATSAPP_WEBHOOK_SECRET}"
    }
  }
}

Telegram 配置

{
  "channels": {
    "telegram": {
      "enabled": true,
      "botToken": "${TELEGRAM_BOT_TOKEN}",
      "allowedUsers": [
        "user1",
        "user2"
      ]
    }
  }
}

Discord 配置

{
  "channels": {
    "discord": {
      "enabled": true,
      "botToken": "${DISCORD_BOT_TOKEN}",
      "guildId": "your-guild-id",
      "channelId": "your-channel-id"
    }
  }
}

Tailscale 集成配置

{
  "gateway": {
    "tailscale": {
      "enabled": true,
      "authKey": "${TAILSCALE_AUTH_KEY}",
      "hostname": "openclaw-gateway"
    },
    "bind": "tailnet"
  }
}

远程访问配置

{
  "gateway": {
    "mode": "remote",
    "remote": {
      "enabled": true,
      "host": "0.0.0.0",
      "port": 18789,
      "tls": {
        "enabled": true,
        "certFile": "/path/to/cert.pem",
        "keyFile": "/path/to/key.pem"
      }
    },
    "auth": {
      "enabled": true,
      "tokens": [
        {
          "name": "client1",
          "token": "${CLIENT1_TOKEN}"
        }
      ]
    }
  }
}

会话管理配置

{
  "sessions": {
    "maxAge": 86400000,
    "cleanupInterval": 3600000,
    "storage": {
      "type": "file",
      "path": "~/.openclaw/sessions"
    }
  }
}

日志配置

{
  "logging": {
    "level": "info",
    "format": "json",
    "outputs": [
      {
        "type": "console"
      },
      {
        "type": "file",
        "path": "~/.openclaw/logs/gateway.log",
        "maxSize": "10MB",
        "maxFiles": 5
      }
    ]
  }
}

插件配置

{
  "plugins": {
    "enabled": true,
    "path": "~/.openclaw/plugins",
    "autoLoad": true,
    "plugins": [
      {
        "name": "custom-tools",
        "enabled": true,
        "config": {
          "apiKey": "${CUSTOM_PLUGIN_KEY}"
        }
      }
    ]
  }
}

模块化配置(使用 $include)

主配置文件

// ~/.openclaw/openclaw.json
{
  "$include": [
    "./config/gateway.json",
    "./config/agents.json",
    "./config/models.json"
  ]
}

网关配置文件

// ~/.openclaw/config/gateway.json
{
  "gateway": {
    "mode": "local",
    "port": 18789,
    "bind": "loopback"
  }
}

代理配置文件

// ~/.openclaw/config/agents.json
{
  "agents": {
    "defaults": {
      "models": ["anthropic:claude-3-5-sonnet"]
    }
  }
}

环境变量使用

配置文件中可以使用环境变量:

{
  "models": {
    "providers": {
      "anthropic": {
        "apiKey": "${ANTHROPIC_API_KEY}"
      }
    }
  }
}

对应的 .env 文件:

# ~/.openclaw/.env
ANTHROPIC_API_KEY=sk-ant-xxxxx
OPENAI_API_KEY=sk-xxxxx
TELEGRAM_BOT_TOKEN=123456:ABC-DEF

热重载配置

{
  "gateway": {
    "reload": {
      "enabled": true,
      "watchFiles": [
        "~/.openclaw/openclaw.json",
        "~/.openclaw/config/*.json"
      ],
      "debounceMs": 1000
    }
  }
}
配置验证
OpenClaw 使用严格的 JSON Schema 验证配置文件。启动前会自动验证配置的正确性,如有错误会显示详细的验证信息。
安全提示
  • 不要在配置文件中直接存储敏感信息,使用环境变量
  • 将 .env 文件添加到 .gitignore
  • 定期轮换 API 密钥和令牌
  • 限制配置文件的文件权限(chmod 600)