zone identifier: 即 Zone ID,可在 Cloudflare 控制台页面的 Overview 页面右下角或 URL 获取
dns records identifier: 通过上述【列出所有】接口的返回结果的 id 获取
X-Auth-Key: 在个人中心的 My Profile -> API Tokens -> Global API Key
这里有一点需要注意:X-Auth-Key 必须使用 Global API Key,但这样导致授权过大,可以在 My Profile -> API Tokens -> API Tokens -> Create Token 创建相应权限的 TOKEN,这里只需要有相应 DNS 的编辑权限即可,相应的,请求时的 header 也要做修改,如
# Get dns record of cloudflare record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$dns_identifier" -H "X-Auth-Email: $email" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" | jq .result.content)
# Get current ip ip=$(curl -s whatismyip.akamai.com)
# Edit records only when IP changes if [ ${record//\"/} != $ip ]; then curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$dns_identifier" -H "X-Auth-Email: $email" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" --data '{"type":"A","name":"'$domain'","content":"'$ip'","ttl":1,"proxied":false}' fi