Namecheap DNS Management
Safe wrapper around the Namecheap API for DNS operations. Prevents accidental record wipeout by always fetching existing records first and merging changes.
⚠️ Why This Skill Exists
The Namecheap API's setHosts method replaces ALL DNS records for a domain. One wrong API call = your entire DNS config is gone. This skill:
- - ✅ Always fetches existing records first
- ✅ Merges new records with existing ones (unless explicitly replacing)
- ✅ Shows a diff preview before applying changes
- ✅ Auto-backups before every change
- ✅ Supports dry-run mode for safe testing
- ✅ One-command rollback from backups
Setup
1. Install dependencies
CODEBLOCK0
2. Enable Namecheap API access
- 1. Go to https://ap.www.namecheap.com/settings/tools/apiaccess/
- Toggle "API Access" ON
- Whitelist your IP address
- Copy your API key
3. Set environment variables
Add to ~/.zshrc or ~/.bashrc:
CODEBLOCK1
Usage
Verify DNS and detect ghost records
⚠️ IMPORTANT: Run this first!
CODEBLOCK2
This command compares DNS records visible to the Namecheap API with actual live DNS records (via dig). It will warn you about "ghost records" that exist in DNS but are invisible to the API (email forwarding, URL redirects, etc.).
List current DNS records
CODEBLOCK3
Note: This only shows records visible to the API. Use verify to see ALL records including those managed by Namecheap subsystems.
Add records (safe merge)
CODEBLOCK4
Safety: The skill automatically checks for "ghost records" before making changes. If detected, it will refuse to proceed unless you use --force.
Remove records
CODEBLOCK5
Backup & Restore
CODEBLOCK6
Record Format
TXT Records
CODEBLOCK7
CNAME Records
CODEBLOCK8
MX Records
CODEBLOCK9
A Records
CODEBLOCK10
Backup Location
Default: ./backups/ (relative to skill directory)
Configurable via environment variable:
CODEBLOCK11
Format: INLINECODE7
Each backup includes:
- -
apiHosts: Records visible to Namecheap API - INLINECODE9 : Actual DNS records captured via INLINECODE10
- Timestamp and domain metadata
This allows you to see what was ACTUALLY live in DNS, not just what the API knew about.
Safety Features
- 1. Ghost record detection — automatic check for records invisible to API
- Auto-backup before changes — every
add or remove creates a timestamped backup (includes DNS snapshot) - Dry-run mode —
--dry-run shows what will change without applying - Diff preview — see exactly what records will be added/removed
- Fetch-first — always gets current DNS state before changes
- Merge logic — adds to existing records instead of replacing
- Rollback — one command to restore from backup
- Safety override —
--force flag for when you need to bypass ghost record warnings
Examples
Mailgun Setup
CODEBLOCK12
Review the diff, then run without --dry-run to apply.
Known Limitations
⚠️ The Namecheap API is Destructive
The Namecheap domains.dns.setHosts API method replaces ALL DNS records for a domain. There is no "add one record" or "update one record" endpoint. Every change requires:
- 1. Fetch all existing records (
getHosts) - Modify the list
- Upload the entire list (
setHosts)
This skill handles this for you by always fetching first and merging changes.
🔍 Ghost Records: The Hidden Danger
Problem: domains.dns.getHosts does NOT return all DNS records. Records managed by Namecheap subsystems are invisible to the API:
- - Email Forwarding — MX, SPF, and DKIM records
- URL Redirect — A/CNAME records for domain parking/redirects
- Third-party integrations — Records added through Namecheap's dashboard for services
Since setHosts replaces all records, using the API can silently delete these hidden records.
🛡️ How This Skill Protects You
- 1.
verify command — Compares API records with actual live DNS (via dig) and warns about ghost records - Automatic safety check — Before any
add, remove, or restore, the skill checks for ghost records - Refuses to proceed — If ghost records are detected, the operation is blocked (unless
--force is used) - Clear warnings — Shows exactly which records will be lost if you proceed
- DNS snapshots in backups — Captures actual DNS state via
dig, not just API state
When to Use --force
Only use the --force flag when:
- - You've manually verified the ghost records are no longer needed
- You're intentionally removing email forwarding or URL redirects
- You understand and accept that those records will be deleted
Never use --force blindly. Always run verify first to see what will be lost.
Example: The Production Incident
This skill was created after adding Mailgun DNS records via the API wiped out Namecheap's email forwarding records. The email forwarding MX/SPF/TXT records were invisible to getHosts, so the fetch-merge-write pattern deleted them.
Now, the skill would have:
- 1. Detected the ghost records during INLINECODE33
- Refused to proceed without INLINECODE34
- Shown exactly which email forwarding records would be deleted
- Created a backup including the DNS snapshot
Troubleshooting
"API request failed: IP not whitelisted"
- - Add your current IP to https://ap.www.namecheap.com/settings/tools/apiaccess/
- Check with: INLINECODE35
"Invalid API key"
- - Verify
NAMECHEAP_API_KEY is set correctly - Re-enable API access if needed
"Domain not found"
- - Ensure domain is in your Namecheap account
- Check spelling (case-sensitive)
API Reference
Namecheap API docs: https://www.namecheap.com/support/api/methods/domains-dns/
Namecheap DNS 管理
针对 Namecheap API 的 DNS 操作安全封装。通过始终先获取现有记录再合并更改,防止意外清空记录。
⚠️ 为什么需要这个技能
Namecheap API 的 setHosts 方法会替换域名的所有 DNS 记录。一次错误的 API 调用 = 整个 DNS 配置消失。本技能:
- - ✅ 始终先获取现有记录
- ✅ 将新记录与现有记录合并(除非明确要求替换)
- ✅ 在应用更改前显示差异预览
- ✅ 每次更改前自动备份
- ✅ 支持试运行模式进行安全测试
- ✅ 一键从备份回滚
设置
1. 安装依赖
bash
cd ~/.openclaw/workspace/skills/namecheap-dns
npm install
2. 启用 Namecheap API 访问
- 1. 访问 https://ap.www.namecheap.com/settings/tools/apiaccess/
- 开启API 访问开关
- 将你的 IP 地址加入白名单
- 复制你的 API 密钥
3. 设置环境变量
添加到 ~/.zshrc 或 ~/.bashrc:
bash
export NAMECHEAPAPIKEY=你的-api-密钥
export NAMECHEAP_USERNAME=你的用户名
export NAMECHEAPAPIUSER=你的用户名 # 通常与用户名相同
使用方法
验证 DNS 并检测幽灵记录
⚠️ 重要:请先运行此命令!
bash
./namecheap-dns.js verify example.com
此命令将 Namecheap API 可见的 DNS 记录与实际实时 DNS 记录(通过 dig)进行比较。它会警告你 DNS 中存在但 API 不可见的幽灵记录(邮件转发、URL 重定向等)。
列出当前 DNS 记录
bash
./namecheap-dns.js list example.com
注意: 这仅显示 API 可见的记录。使用 verify 查看所有记录,包括由 Namecheap 子系统管理的记录。
添加记录(安全合并)
bash
添加单个 TXT 记录
./namecheap-dns.js add example.com \
--txt mail.example.com=v=spf1 include:mailgun.org ~all
一次添加多个记录
./namecheap-dns.js add example.com \
--txt mail=v=spf1 include:mailgun.org ~all \
--cname email.mail=mailgun.org \
--mx mail=10 mxa.mailgun.org
试运行(预览更改而不应用)
./namecheap-dns.js add example.com \
--txt test=hello \
--dry-run
强制覆盖安全检查(如果你确定可以删除幽灵记录)
./namecheap-dns.js add example.com \
--txt test=hello \
--force
安全性: 该技能在进行更改前会自动检查幽灵记录。如果检测到,它将拒绝继续,除非你使用 --force。
删除记录
bash
按主机名+类型删除
./namecheap-dns.js remove example.com \
--host old-record \
--type TXT
先试运行
./namecheap-dns.js remove example.com \
--host old-record \
--type TXT \
--dry-run
备份与恢复
bash
创建手动备份
./namecheap-dns.js backup example.com
列出可用备份
./namecheap-dns.js backups example.com
从最新备份恢复
./namecheap-dns.js restore example.com
从特定备份恢复
./namecheap-dns.js restore example.com \
--backup example.com-20260213-114500.json
记录格式
TXT 记录
--txt 子域名=值
--txt @=值 # 根域名
CNAME 记录
--cname 子域名=目标.com
MX 记录
--mx 子域名=10 mx.目标.com
--mx @=10 mx.目标.com # 根域名
A 记录
--a 子域名=192.168.1.1
--a @=192.168.1.1 # 根域名
备份位置
默认: ./backups/(相对于技能目录)
可通过环境变量配置:
bash
export NAMECHEAPBACKUPDIR=/自定义/备份路径
格式:{域名}-{时间戳}.json
每个备份包含:
- - apiHosts:Namecheap API 可见的记录
- liveDNS:通过 dig 捕获的实际 DNS 记录
- 时间戳和域名元数据
这让你可以看到 DNS 中实际生效的记录,而不仅仅是 API 知道的记录。
安全特性
- 1. 幽灵记录检测 — 自动检查 API 不可见的记录
- 更改前自动备份 — 每次 add 或 remove 都会创建带时间戳的备份(包含 DNS 快照)
- 试运行模式 — --dry-run 显示将要更改的内容而不应用
- 差异预览 — 精确查看将要添加/删除的记录
- 先获取后操作 — 更改前始终获取当前 DNS 状态
- 合并逻辑 — 添加到现有记录而非替换
- 回滚 — 一键从备份恢复
- 安全覆盖 — --force 标志用于需要绕过幽灵记录警告时
示例
Mailgun 设置
bash
./namecheap-dns.js add menuhq.ai \
--txt mail.menuhq.ai=v=spf1 include:mailgun.org ~all \
--txt smtp._domainkey.mail.menuhq.ai=k=rsa; p=MIGfMA0... \
--txt _dmarc.mail.menuhq.ai=v=DMARC1; p=quarantine; \
--cname email.mail.menuhq.ai=mailgun.org \
--mx mail.menuhq.ai=10 mxa.mailgun.org \
--mx mail.menuhq.ai=20 mxb.mailgun.org \
--dry-run
查看差异,然后不带 --dry-run 运行以应用更改。
已知限制
⚠️ Namecheap API 具有破坏性
Namecheap 的 domains.dns.setHosts API 方法会替换域名的所有 DNS 记录。没有添加一条记录或更新一条记录的端点。每次更改需要:
- 1. 获取所有现有记录(getHosts)
- 修改列表
- 上传整个列表(setHosts)
本技能为你处理这个问题,始终先获取再合并更改。
🔍 幽灵记录:隐藏的危险
问题: domains.dns.getHosts 不会返回所有 DNS 记录。由 Namecheap 子系统管理的记录对 API 不可见:
- - 邮件转发 — MX、SPF 和 DKIM 记录
- URL 重定向 — 域名停放/重定向的 A/CNAME 记录
- 第三方集成 — 通过 Namecheap 控制面板为服务添加的记录
由于 setHosts 替换所有记录,使用 API 可能会静默删除这些隐藏记录。
🛡️ 本技能如何保护你
- 1. verify 命令 — 将 API 记录与实际实时 DNS(通过 dig)进行比较,并警告幽灵记录
- 自动安全检查 — 在任何 add、remove 或 restore 之前,技能会检查幽灵记录
- 拒绝继续 — 如果检测到幽灵记录,操作将被阻止(除非使用 --force)
- 明确警告 — 精确显示如果继续将丢失哪些记录
- 备份中的 DNS 快照 — 通过 dig 捕获实际 DNS 状态,而不仅仅是 API 状态
何时使用 --force
仅在以下情况下使用 --force 标志:
- - 你已手动确认不再需要幽灵记录
- 你故意删除邮件转发或 URL 重定向
- 你理解并接受这些记录将被删除
切勿盲目使用 --force。 始终先运行 verify 查看会丢失什么。
示例:生产事故
这个技能是在通过 API 添加 Mailgun DNS 记录导致 Namecheap 的邮件转发记录被清除后创建的。邮件转发的 MX/SPF/TXT 记录对 getHosts 不可见,因此获取-合并-写入模式删除了它们。
现在