Cloudflare-API操作

批量添加域名

export CF_API_EMAIL=XXX
export CF_API_KEY=XXX
for domain in $(cat a); do curl -X POST -H "X-Auth-Key: $CF_API_KEY" -H "X-Auth-Email: $CF_API_EMAIL" -H "Content-Type: application/json" "https://api.cloudflare.com/client/v4/zones" --data '{"name":"'$domain'","jump_start":false}'; done

添加域名

# jump_start, 是自动扫描二级记录,但是获取不到一级记录
# 账户ID: 7f677a8f70ac44f3f7d3e4006952ced2
import requests

url = "https://api.cloudflare.com/client/v4/zones"

payload = "{\"account\": {\"id\": \"7f677a8f70ac44f3f7d3e4006952ced2\"}, \"name\": \"a.com\",\"jump_start\": false}"
headers = {
  'X-Auth-Email': 'xxxxxxxxx@gmail.com',
  'X-Auth-Key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

删除域名

# 
import requests

url = "https://api.cloudflare.com/client/v4/zones" + '/' + '域名ID'

headers = {
  'X-Auth-Email': 'xxxxxxxxx@gmail.com',
  'X-Auth-Key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Content-Type': 'application/json'
}

response = requests.request("DELETE", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

添加记录

https://dash.cloudflare.com/api/v4/zones/626ae693d2f81dfe13f36290fd2e5502/dns_records

# 域名ID: 101b56350bd48d345a30a195a4190fb9

import requests

url = "https://api.cloudflare.com/client/v4/zones/101b56350bd48d345a30a195a4190fb9/dns_records"

payload = "{\"content\": \"2.2.2.2\",\"data\": {},\"name\": \"www\",\"proxiable\": true,\"proxied\": false,\"ttl\": 1,\"type\": \"A\",\"zone_id\":\"101b56350bd48d345a30a195a4190fb9\",\"zone_name\": \"b.com\"}"

headers = {
  'X-Auth-Email': 'xxxxxxxxx@gmail.com',
  'X-Auth-Key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

获取域名的所有记录

  • 首先需要获取域名所有 记录ID https://api.cloudflare.com/client/v4/zones/626ae693d2f81dfe13f36290fd2e5502/dns_records/f01e9c9d277982804208ba38ca005916


# 域名ID: 626ae693d2f81dfe13f36290fd2e5502
import requests

url = "https://api.cloudflare.com/client/v4/zones/626ae693d2f81dfe13f36290fd2e5502/dns_records"

payload  = {}
headers = {
  'X-Auth-Email': 'xxxxxxxxx@gmail.com',
  'X-Auth-Key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Content-Type': 'application/json'
}

response = requests.request("GET", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

删除某个域名的某一条记录


# 域名ID: 626ae693d2f81dfe13f36290fd2e5502
# 记录ID: a724ce45f9d130a30cd085133779f729
import requests

url = "https://api.cloudflare.com/client/v4/zones/626ae693d2f81dfe13f36290fd2e5502/dns_records/a724ce45f9d130a30cd085133779f729"

payload  = {}
headers = {
  'X-Auth-Email': 'xxxxxxxxx@gmail.com',
  'X-Auth-Key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Content-Type': 'application/json'
}

response = requests.request("DELETE", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

获取所有域名

import requests

# 分页显示: https://api.cloudflare.com/client/v4/zones?page=1&per_page=20&order=type&direction=asc
url = "https://api.cloudflare.com/client/v4/zones"

payload  = {}
headers = {
  'X-Auth-Email': 'xxxxxxxxx@gmail.com',
  'X-Auth-Key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Content-Type': 'application/json'
}

response = requests.request("GET", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

修改某一个域名的某条记录


# 域名ID: 101b56350bd48d345a30a195a4190fb9
# 域名记录ID: aed0976a6507842946b982186872e3e5
import requests

url = "https://api.cloudflare.com/client/v4/zones/101b56350bd48d345a30a195a4190fb9/dns_records/aed0976a6507842946b982186872e3e5"

payload = "{\"content\": \"1.1.1.2\",\"data\": {},\"name\": \"www.b.com\",\"proxiable\": true,\"proxied\": false,\"ttl\": 1,\"type\": \"A\",\"zone_id\":\"101b56350bd48d345a30a195a4190fb9\",\"zone_name\": \"b.com\"}"
headers = {
  'X-Auth-Email': 'xxxxxxxxx@gmail.com',
  'X-Auth-Key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Content-Type': 'application/json'
}

response = requests.request("PUT", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

Last updated