正文内容
Porkbun DNS 管理
通过 Porkbun REST API v3 管理 DNS 记录与域名。
快速开始
配置 API 凭据
- 生成 API 密钥:https://porkbun.com/account/api
-
将凭据保存至配置文件:
~/.config/porkbun/config.json{ "apiKey": "your-api-key", "secretApiKey": "your-secret-api-key" }
或设置环境变量:
export PORKBUN_API_KEY="your-api-key" export PORKBUN_SECRET_API_KEY="your-secret-api-key"
- 为每个域名启用 API 访问权限:域名管理 → 详情 → API 访问 → 启用
测试连接
node ~/.openclaw/workspace/skills/public/porkbun/scripts/porkbun-dns.js ping
使用 CLI 工具
scripts/porkbun-dns.js 脚本提供了一种可靠、确定性的方式执行 DNS 操作。对于常见任务,请直接使用该脚本,无需编写自定义代码。
常见操作
列出所有域名
node scripts/porkbun-dns.js list
列出某域名的 DNS 记录
node scripts/porkbun-dns.js records example.com
创建记录
# A 记录 node scripts/porkbun-dns.js create example.com type=A name=www content=1.1.1.1 ttl=600 # CNAME 记录 node scripts/porkbun-dns.js create example.com type=CNAME name=docs content=example.com # MX 记录 node scripts/porkbun-dns.js create example.com type=MX name= content="mail.example.com" prio=10 # TXT 记录(电子邮件 SPF) node scripts/porkbun-dns.js create example.com type=TXT name= content="v=spf1 include:_spf.google.com ~all"
编辑记录
# 按 ID 编辑(ID 可通过 `records` 命令获取) node scripts/porkbun-dns.js edit example.com 123456 content=2.2.2.2 # 按类型和子域名编辑(更新所有匹配的记录) node scripts/porkbun-dns.js edit-by example.com A www content=2.2.2.2
删除记录
# 按 ID 删除 node scripts/porkbun-dns.js delete example.com 123456 # 按类型和子域名删除 node scripts/porkbun-dns.js delete-by example.com A www
获取特定记录
# 获取某域名下全部记录 node scripts/porkbun-dns.js get example.com # 按类型筛选 node scripts/porkbun-dns.js get example.com A # 按类型和子域名筛选 node scripts/porkbun-dns.js get example.com A www
支持的记录类型
支持的记录类型包括:A、AAAA、CNAME、ALIAS、TXT、NS、MX、SRV、TLSA、CAA、HTTPS、SVCB、SSHFP
如需各类型字段要求与示例说明,请参阅 [references/dns-record-types.md](references/dns-record-types.md)。
常见配置模式
网站部署
创建根域 A 记录与 www CNAME 记录:
node scripts/porkbun-dns.js create example.com type=A name= content=192.0.2.1 node scripts/porkbun-dns.js create example.com type=CNAME name=www content=example.com
电子邮件配置(Google Workspace)
配置 MX 记录:
node scripts/porkbun-dns.js create example.com type=MX name= content="aspmx.l.google.com" prio=1 node scripts/porkbun-dns.js create example.com type=MX name= content="alt1.aspmx.l.google.com" prio=5 node scripts/porkbun-dns.js create example.com type=MX name= content="alt2.aspmx.l.google.com" prio=5 node scripts/porkbun-dns.js create example.com type=MX name= content="alt3.aspmx.l.google.com" prio=10 node scripts/porkbun-dns.js create example.com type=MX name= content="alt4.aspmx.l.google.com" prio=10
添加 SPF 记录:
node scripts/porkbun-dns.js create example.com type=TXT name= content="v=spf1 include:_spf.google.com ~all"
动态 DNS(DDNS)
更新家庭公网 IP(可脚本化/自动化):
HOME_IP=$(curl -s ifconfig.me) node scripts/porkbun-dns.js edit-by example.com A home content=$HOME_IP
泛域名(Wildcard)DNS
创建指向根域的泛域名记录:
node scripts/porkbun-dns.js create example.com type=A name=* content=192.0.2.1
参考文档
- [references/dns-record-types.md](references/dns-record-types.md) — 所有 DNS 记录类型的详细参考及字段要求
- [https://porkbun.com/api/json/v3/documentation](https://porkbun.com/api/json/v3/documentation) — 完整 API 文档
故障排查
“未找到 API 密钥”(API key not found)
- 确认配置文件
~/.config/porkbun/config.json存在且路径正确 - 检查环境变量是否已设置:
echo $PORKBUN_API_KEY - 确保目标域名已在 Porkbun 控制台中启用了 API 访问权限
“传入了无效的记录类型”(Invalid type passed)
- 记录类型必须为大写(例如
A,不可写作a) - 请参考上文列出的支持类型列表
HTTP 错误
- 登录 https://porkbun.com/account/api 确认 API 密钥有效
- 检查网络连通性
- 确认 API 请求地址为
api.porkbun.com(而非porkbun.com)
TTL 错误
- 最小 TTL 为 600 秒(10 分钟)
- 默认 TTL 为 600 秒
- 常用 TTL 值:300(动态场景)、3600(常规用途)、86400(长期稳定场景)
注意事项
- TTL 最小值为 600 秒
- 根域记录请使用
@表示 - 泛域名记录请使用
*表示 - 包含空格的 TXT 记录内容需用英文双引号包裹
- 支持配置多个 MX 记录,通过
prio参数区分优先级 - API v3 当前使用的主机名:
api.porkbun.com