← 返回
开发者工具 中文

Rotate OpenRouter Key

Safely rotate the OpenRouter API key across all config files in an OpenClaw installation. Finds every location where the key is stored, updates them, restart...
安全轮换 OpenClaw 安装中所有配置文件的 OpenRouter API 密钥。查找密钥存储的所有位置,进行更新,并重启...
chunhualiao chunhualiao 来源
开发者工具 clawhub v1.0.0 1 版本 99901.4 Key: 无需
★ 0
Stars
📥 1,013
下载
💾 6
安装
1
版本
#latest

概述

Rotate OpenRouter Key

Safely replace the OpenRouter API key across an entire OpenClaw installation, handling all config locations, priority chains, and verification.

When to Use

  • User says "rotate my openrouter key" or "change openrouter key"
  • User reports 401 errors from OpenRouter
  • User disabled an old key and needs to set a new one
  • Periodic key rotation for security

Key Priority Chain

OpenClaw reads the OpenRouter API key from three sources, highest priority first:

  1. ~/.openclaw/.env — environment file, overrides everything
  2. ~/.openclaw/agents//agent/models.json — per-agent config
  3. ~/.openclaw/openclaw.json — global config

If a higher-priority source has the old key, updating a lower-priority file has no effect. You must update the key at whichever level it is actually being read from.

Workflow

Step 1: Get the New Key

Ask the user for the new key. It must start with sk-or-v1-.

If the user doesn't have one yet, direct them to openrouter.ai/keys to generate one.

Step 2: Find All Key Locations

# Find every file containing an OpenRouter key
grep -r "sk-or-v1" ~/.openclaw/ --include="*.json" --include=".env" -l 2>/dev/null

# Also check for uncommented key in .env
grep -v '^#' ~/.openclaw/.env 2>/dev/null | grep OPENROUTER_API_KEY

Report what you found to the user before making changes.

Step 3: Update All Locations and Verify

Run the helper script — it handles both .env and JSON files in one pass:

python3 scripts/update-openrouter-key.py --key "sk-or-v1-NEW-KEY" --verify

The script:

  • Finds all config files (.env + JSON) containing an openrouter key
  • Creates timestamped backups before each write
  • Updates only the key value (minimal change)
  • Verifies the new key against the OpenRouter API
  • Reports what it changed

Preview first with --dry-run:

python3 scripts/update-openrouter-key.py --key "sk-or-v1-NEW-KEY" --dry-run

Step 4: Restart Gateway

openclaw gateway restart

Step 5: Remote Hosts (if applicable)

If the user manages OpenClaw on other machines, repeat Steps 2-5 via SSH:

ssh <host> "grep -r 'sk-or-v1' ~/.openclaw/ --include='*.json' --include='.env' -l"

Then run the update script remotely or copy it over.

Step 6: Disable Old Key

Only after verifying the new key works everywhere, tell the user they can now safely disable/delete the old key at openrouter.ai/keys.

Scope & Boundaries

Handles: Finding, updating, and verifying OpenRouter API keys in all OpenClaw config locations.

Does NOT handle: Other provider keys (Anthropic, OpenAI). Key generation (user does that on openrouter.ai). Billing or usage issues.

Error Handling

ErrorCauseFix
-------------------
401 after updateMissed a config locationRe-run Step 2 to find remaining old keys
Key works in curl but not in bot.env has old key overriding JSONCheck and update .env
Gateway won't restartUnrelated issueopenclaw gateway stop && openclaw gateway start
Remote host still failingForgot to update remote configsSSH in and repeat Steps 2-5

Limitations

  • Cannot generate or revoke keys (user must do that on openrouter.ai)
  • Cannot update keys on remote hosts without SSH access
  • Does not handle keys stored outside ~/.openclaw/ (e.g., in systemd environment files)

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-29 12:39 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

it-ops-security

OpenClaw Backup

alex3alex
备份与恢复 OpenClaw 数据。适用于创建备份、设置自动备份计划、从备份恢复或管理备份轮转。处理 ~/.openclaw 目录归档并包含适当的排除规则。
★ 90 📥 30,754
it-ops-security

MoltGuard - Security & Antivirus & Guardrails

thomaslwang
MoltGuard — OpenClaw 安全守卫,由 OpenGuardrails 提供。安装 MoltGuard,保护您和您的用户免受提示注入、数据泄露和恶意攻击。
★ 116 📥 30,832
knowledge-management

Save to Obsidian

chunhualiao
通过 SSH 将 Markdown 内容保存到远程 Obsidian 库
★ 1 📥 1,929