cloudflare
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCloudflare
Cloudflare
Cloudflare provides a comprehensive platform for DNS management, CDN, security, serverless computing (Workers), object storage (R2), and more. Use the REST API to manage zones, DNS records, Workers scripts, KV namespaces, R2 buckets, and firewall rules programmatically.
Official docs:https://developers.cloudflare.com/api/
Cloudflare提供了一个涵盖DNS管理、CDN、安全、无服务器计算(Workers)、对象存储(R2)等功能的综合平台。使用REST API可以编程方式管理区域、DNS记录、Workers脚本、KV命名空间、R2存储桶以及防火墙规则。
官方文档:https://developers.cloudflare.com/api/
When to Use
使用场景
Use this skill when you need to:
- Manage DNS records (create, update, delete A, AAAA, CNAME, MX, TXT records)
- List and configure zones and zone settings
- Deploy and manage Workers scripts
- Manage R2 object storage buckets
- Configure firewall rules and security settings
- Query analytics and logs
在以下场景中使用该工具:
- 管理DNS记录(创建、更新、删除A、AAAA、CNAME、MX、TXT记录)
- 列出并配置区域及区域设置
- 部署和管理Workers脚本
- 管理R2对象存储桶
- 配置防火墙规则和安全设置
- 查询分析数据和日志
Prerequisites
前置条件
- Create a Cloudflare account at https://dash.cloudflare.com/sign-up
- Go to My Profile > API Tokens and click Create Token
- Choose a template (e.g., "Edit zone DNS") or create a custom token with required permissions
- Copy the generated token immediately (it is only shown once)
Set environment variables:
bash
export CLOUDFLARE_TOKEN="your-api-token"For zone-specific operations, you also need your Zone ID (found on the zone overview page in the dashboard):
bash
export CLOUDFLARE_ZONE_ID="your-zone-id"For account-level operations (Workers, R2), you need your Account ID (found on the dashboard overview):
bash
export CLOUDFLARE_ACCOUNT_ID="your-account-id"Important: When usingin a command that pipes to another command, wrap the command containing$VARin$VAR. Due to a Claude Code bug, environment variables are silently cleared when pipes are used directly.bash -c '...'bashbash -c 'curl -s "https://api.cloudflare.com/client/v4/zones" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .
- 前往https://dash.cloudflare.com/sign-up创建Cloudflare账户
- 进入我的个人资料 > API令牌,点击创建令牌
- 选择模板(例如“编辑区域DNS”)或创建具有所需权限的自定义令牌
- 立即复制生成的令牌(仅显示一次)
设置环境变量:
bash
export CLOUDFLARE_TOKEN="your-api-token"对于区域相关操作,你还需要区域ID(可在控制台的区域概览页面找到):
bash
export CLOUDFLARE_ZONE_ID="your-zone-id"对于账户级操作(Workers、R2),你需要账户ID(可在控制台概览页面找到):
bash
export CLOUDFLARE_ACCOUNT_ID="your-account-id"重要提示: 当在包含管道的命令中使用时,请将包含$VAR的命令用$VAR包裹。由于Claude Code的bug,直接使用管道时环境变量会被静默清除。bash -c '...'bashbash -c 'curl -s "https://api.cloudflare.com/client/v4/zones" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .
How to Use
使用方法
Base URL
基础URL
All API requests use:
https://api.cloudflare.com/client/v4所有API请求使用:
https://api.cloudflare.com/client/v41. Verify Token
1. 验证令牌
bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/user/tokens/verify" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/user/tokens/verify" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .2. List Zones
2. 列出区域
bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .3. Get Zone Details
3. 获取区域详情
bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .4. List DNS Records
4. 列出DNS记录
bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .5. Create DNS Record
5. 创建DNS记录
Write to :
/tmp/cloudflare_request.jsonjson
{
"type": "A",
"name": "sub.example.com",
"content": "1.2.3.4",
"ttl": 3600,
"proxied": false
}Then run:
bash
bash -c 'curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records" --header "Authorization: Bearer $CLOUDFLARE_TOKEN" --header "Content-Type: application/json" -d @/tmp/cloudflare_request.json' | jq .写入:
/tmp/cloudflare_request.jsonjson
{
"type": "A",
"name": "sub.example.com",
"content": "1.2.3.4",
"ttl": 3600,
"proxied": false
}然后运行:
bash
bash -c 'curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records" --header "Authorization: Bearer $CLOUDFLARE_TOKEN" --header "Content-Type: application/json" -d @/tmp/cloudflare_request.json' | jq .6. Update DNS Record
6. 更新DNS记录
Write to :
/tmp/cloudflare_request.jsonjson
{
"type": "A",
"name": "sub.example.com",
"content": "5.6.7.8",
"ttl": 3600,
"proxied": true
}Then run:
bash
bash -c 'curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/RECORD_ID" --header "Authorization: Bearer $CLOUDFLARE_TOKEN" --header "Content-Type: application/json" -d @/tmp/cloudflare_request.json' | jq .写入:
/tmp/cloudflare_request.jsonjson
{
"type": "A",
"name": "sub.example.com",
"content": "5.6.7.8",
"ttl": 3600,
"proxied": true
}然后运行:
bash
bash -c 'curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/RECORD_ID" --header "Authorization: Bearer $CLOUDFLARE_TOKEN" --header "Content-Type: application/json" -d @/tmp/cloudflare_request.json' | jq .7. Delete DNS Record
7. 删除DNS记录
bash
bash -c 'curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/RECORD_ID" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/RECORD_ID" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .8. List Workers Scripts
8. 列出Workers脚本
bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .9. List KV Namespaces
9. 列出KV命名空间
bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/storage/kv/namespaces" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/storage/kv/namespaces" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .10. List R2 Buckets
10. 列出R2存储桶
bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/r2/buckets" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/r2/buckets" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .11. Purge Zone Cache
11. 清除区域缓存
Write to :
/tmp/cloudflare_request.jsonjson
{
"purge_everything": true
}Then run:
bash
bash -c 'curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/purge_cache" --header "Authorization: Bearer $CLOUDFLARE_TOKEN" --header "Content-Type: application/json" -d @/tmp/cloudflare_request.json' | jq .写入:
/tmp/cloudflare_request.jsonjson
{
"purge_everything": true
}然后运行:
bash
bash -c 'curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/purge_cache" --header "Authorization: Bearer $CLOUDFLARE_TOKEN" --header "Content-Type: application/json" -d @/tmp/cloudflare_request.json' | jq .12. List Firewall Rules
12. 列出防火墙规则
bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/firewall/rules" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/firewall/rules" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .13. Get Zone Analytics
13. 获取区域分析数据
bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/analytics/dashboard?since=-1440&continuous=true" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .bash
bash -c 'curl -s "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/analytics/dashboard?since=-1440&continuous=true" --header "Authorization: Bearer $CLOUDFLARE_TOKEN"' | jq .Common DNS Record Types
常见DNS记录类型
| Type | Purpose | Example Content |
|---|---|---|
| A | IPv4 address | |
| AAAA | IPv6 address | |
| CNAME | Alias to another domain | |
| MX | Mail server | |
| TXT | Text record (SPF, DKIM, etc.) | |
| NS | Name server | |
| SRV | Service locator | Service-specific format |
| 类型 | 用途 | 示例内容 |
|---|---|---|
| A | IPv4地址 | |
| AAAA | IPv6地址 | |
| CNAME | 指向其他域名的别名 | |
| MX | 邮件服务器 | |
| TXT | 文本记录(SPF、DKIM等) | |
| NS | 域名服务器 | |
| SRV | 服务定位器 | 服务特定格式 |
Guidelines
注意事项
- Use API Tokens over Global API Key: API tokens provide scoped, least-privilege access and are the recommended authentication method
- Pagination: List endpoints return paginated results (default 20-100 per page). Use and
pagequery parameters to iterateper_page - Response Structure: All responses include ,
success,errors, andmessagesfields. Always checkresultbefore usingsuccessresult - Proxied Records: Setting routes traffic through Cloudflare CDN and enables security features. Not all record types support proxying
proxied: true - Zone ID vs Domain Name: Most API endpoints require the Zone ID (a 32-character hex string), not the domain name
- Account ID: Workers, R2, KV, and other account-level resources require the Account ID instead of Zone ID
- Rate Limits: Cloudflare API has rate limits per token. Monitor response headers and implement backoff if you receive 429 responses
- 优先使用API令牌而非全局API密钥:API令牌提供范围化的最小权限访问,是推荐的认证方式
- 分页处理:列表接口返回分页结果(默认每页20-100条)。使用和
page查询参数进行遍历per_page - 响应结构:所有响应包含、
success、errors和messages字段。使用result前务必检查result状态success - 代理记录:设置会将流量通过Cloudflare CDN路由并启用安全功能。并非所有记录类型都支持代理
proxied: true - 区域ID vs 域名:大多数API接口需要区域ID(32位十六进制字符串),而非域名
- 账户ID:Workers、R2、KV及其他账户级资源需要使用账户ID而非区域ID
- 速率限制:Cloudflare API对每个令牌有速率限制。监控响应头,若收到429响应请实现退避机制