简介
一键安全轮换 OpenRouter API 密钥,自动定位多层级配置、创建备份、验证有效性并重启网关,避免遗漏导致的 401 故障。
原始描述
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...
通过你的 Agent 添加
点「快速安装」复制一段指令,粘给你正在用的 Agent(Claude Code、Codex、OpenClaw 等),它会自己完成安装。
或者手动安装
包里是 SKILL.md 加作者附带的文件,解压后目录名就是技能名。
技能申请的权限由作者决定,装之前请确认它能读什么、能执行什么。
中文使用说明 来自收录源
核心用法
该Skill专为OpenClaw用户设计,用于安全轮换OpenRouter API密钥。其核心工作流程包括六个步骤:首先获取用户的新密钥(必须以sk-or-v1-开头),然后通过grep命令扫描~/.openclaw/目录下的所有配置文件,识别密钥存储位置;接着使用Python脚本update-openrouter-key.py执行原子化更新,该脚本会自动创建时间戳备份、执行最小化变更,并向OpenRouter官方API发送验证请求;更新完成后需重启OpenClaw网关,若涉及远程主机还需通过SSH重复操作;最后指导用户在确认新密钥生效后禁用旧密钥。
Skill特别处理了OpenClaw的三层密钥优先级机制:~/.openclaw/.env(最高优先级,覆盖所有配置)、~/.openclaw/agents/<name>/agent/models.json(单代理配置)、~/.openclaw/openclaw.json(全局配置),确保不会遗漏任何生效位置的密钥。
显著优点
全面覆盖:自动扫描并处理所有可能的密钥存储位置,避免手动遗漏导致的配置不一致问题。
安全备份:每次写入前自动创建.bak.{timestamp}备份,支持故障回滚,降低操作风险。
验证机制:更新后主动向OpenRouter API发送验证请求,确保新密钥可用性,避免"更新但未生效"的尴尬场景。
零外部依赖:仅使用Python标准库(json、os、re、shutil、urllib等),无供应链攻击风险,部署简单可靠。
预览模式:支持--dry-run参数,用户可在实际变更前预览所有修改,提升操作透明度。
潜在缺点与局限性
来源可信度:由个人开发者(chunhualiao)维护,非知名公司或顶级开源基金会背书,属于T3信任级别,需用户自行审查代码。
功能边界明确:仅支持OpenRouter密钥,不处理Anthropic、OpenAI等其他供应商密钥;无法直接生成或吊销密钥,用户必须手动访问openrouter.ai操作。
远程主机依赖SSH:对于多机部署场景,Skill本身无法自动推送更新到远程主机,需用户具备SSH访问权限并手动执行。
配置范围受限:仅处理~/.openclaw/目录内的文件,无法识别systemd环境文件等外部存储位置的密钥。
审计日志缺失:虽然创建文件备份,但未内置操作审计日志功能,企业级合规场景可能需要额外记录。
适合的目标群体
- OpenClaw平台运维人员:需要定期轮换API密钥以符合安全合规要求的团队
- 多代理配置管理者:在OpenClaw中部署了多个AI代理,密钥分散存储难以手动同步的开发者
- 安全意识较强的个人开发者:希望实现密钥轮换自动化,降低人为操作失误概率
- 小型技术团队:缺乏专职DevOps,需要开箱即用的密钥管理脚本
使用风险
配置遗漏风险:若用户曾在优先级较高的文件(如.env)中手动设置密钥,而只在低优先级文件更新,可能导致旧密钥仍被读取。Skill虽会扫描全部位置,但用户需理解优先级机制。
网络中断风险:验证步骤依赖OpenRouter API的可用性,若更新时网络异常或API服务故障,可能误判密钥无效。
备份管理成本:自动备份文件可能累积占用磁盘空间,需用户定期清理旧备份。
密钥泄露风险:虽然Skill会对密钥进行脱敏处理(显示前8位和后4位),但在共享终端或日志系统中运行时仍存在暴露可能,建议在私密环境中操作。
远程同步风险:多机场景下若遗漏某台主机未更新,可能导致服务中断,需建立主机清单核对机制。
技能正文
name: rotate-openrouter-key description: Safely rotate the OpenRouter API key across all config files in an OpenClaw installation. Finds every location where the key is stored, updates them, restarts the gateway, and verifies the new key works. Use when asked to "rotate openrouter key", "change openrouter api key", "update openrouter key", or "replace openrouter key".
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:
~/.openclaw/.env— environment file, overrides everything~/.openclaw/agents/<name>/agent/models.json— per-agent config~/.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
| Error | Cause | Fix |
|---|---|---|
| 401 after update | Missed a config location | Re-run Step 2 to find remaining old keys |
| Key works in curl but not in bot | .env has old key overriding JSON |
Check and update .env |
| Gateway won't restart | Unrelated issue | openclaw gateway stop && openclaw gateway start |
| Remote host still failing | Forgot to update remote configs | SSH 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)
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。