Cloudflare API技能使用说明
2026-03-30
新闻来源:网淘吧
围观:14
电脑广告
手机广告
Cloudflare技能
连接至CloudflareAPI,用于DNS管理、隧道和区域管理。
设置
1. 获取您的API令牌
- 前往dash.cloudflare.com/profile/api-tokens
- 创建具有所需权限的令牌:
- 区域:读取- 列出域名
- DNS:编辑- 管理DNS记录
- 账户:Cloudflare隧道:编辑- 管理隧道
- 复制令牌
2. 配置
# Option A: Store in file (recommended)
echo "YOUR_API_TOKEN" > ~/.cloudflare_token
chmod 600 ~/.cloudflare_token
# Option B: Environment variable
export CLOUDFLARE_API_TOKEN="YOUR_API_TOKEN"
3. 测试连接
./scripts/setup.sh
命令
区域(域名)
./scripts/zones/list.sh # List all zones
./scripts/zones/list.sh --json # JSON output
./scripts/zones/get.sh example.com # Get zone details
DNS记录
# List records
./scripts/dns/list.sh example.com
./scripts/dns/list.sh example.com --type A
./scripts/dns/list.sh example.com --name api
# Create record
./scripts/dns/create.sh example.com \
--type A \
--name api \
--content 1.2.3.4 \
--proxied
# Create CNAME
./scripts/dns/create.sh example.com \
--type CNAME \
--name www \
--content example.com \
--proxied
# Update record
./scripts/dns/update.sh example.com \
--name api \
--type A \
--content 5.6.7.8
# Delete record
./scripts/dns/delete.sh example.com --name api --type A
隧道
# List tunnels
./scripts/tunnels/list.sh
# Create tunnel
./scripts/tunnels/create.sh my-tunnel
# Configure tunnel ingress
./scripts/tunnels/configure.sh my-tunnel \
--hostname app.example.com \
--service http://localhost:3000
# Get run token
./scripts/tunnels/token.sh my-tunnel
# Delete tunnel
./scripts/tunnels/delete.sh my-tunnel
令牌权限
| 功能 | 所需权限 |
|---|---|
| 列出区域 | 区域:读取 |
| 管理DNS | DNS:编辑 |
| 管理隧道 | 账户:Cloudflare隧道:编辑 |
创建令牌地址:dash.cloudflare.com/profile/api-tokens

常见工作流程
将子域名指向服务器
./scripts/dns/create.sh mysite.com --type A --name api --content 1.2.3.4 --proxied
为本地服务设置隧道
# 1. Create tunnel
./scripts/tunnels/create.sh webhook-tunnel
# 2. Configure ingress
./scripts/tunnels/configure.sh webhook-tunnel \
--hostname hook.mysite.com \
--service http://localhost:8080
# 3. Add DNS record
TUNNEL_ID=$(./scripts/tunnels/list.sh --name webhook-tunnel --quiet)
./scripts/dns/create.sh mysite.com \
--type CNAME \
--name hook \
--content ${TUNNEL_ID}.cfargotunnel.com \
--proxied
# 4. Run tunnel
TOKEN=$(./scripts/tunnels/token.sh webhook-tunnel)
cloudflared tunnel run --token $TOKEN
输出格式
| 标志 | 描述 |
|---|---|
--json | API返回的原始JSON |
--table | 格式化表格(默认) |
--quiet | 最小化输出(仅ID) |
故障排除
| 错误 | 解决方案 |
|---|---|
| “未找到API令牌” | 运行设置或配置CLOUDFLARE_API_TOKEN |
| "401 未授权" | 检查令牌是否有效 |
| "403 禁止访问" | 令牌缺少所需权限 |
| "未找到区域" | 验证域名是否在您的账户中 |
文章底部电脑广告
手机广告位-内容正文底部


微信扫一扫,打赏作者吧~