Python Cloudflare TTL Changer.
Using the Cloudflare API, this script updates the TTL (Time To Live) of DNS records. It can be useful for changing the TTL of multiple records at once.
import requests
API_TOKEN = (
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # Replace with your Cloudflare API token
)
ZONE_ID = "00000000000000000000000000000000" # Replace with your Cloudflare Zone ID found in the dashboard
HEADERS = {"Authorization": f"Bearer {API_TOKEN}", "Content-Type": "application/json"}
CURRENT_TTL = 1
TARGET_TTL = 3600
PROXIED = False
TYPES = ["A", "AAAA", "CNAME"]
def list_dns_records():
page, all_records = 1, []
while True:
url = f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records?per_page=100&page={page}"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
all_records.extend(data["result"])
if data["result_info"]["total_pages"] == page:
break
page += 1
return all_records
def update_dns_record(record):
url = f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records/{record['id']}"
response = requests.put(url, headers=HEADERS, json=record)
response.raise_for_status()
return response.json()
dns_records = list_dns_records()
print(f"Total DNS records: {len(dns_records)}")
for record in dns_records:
if (
record["ttl"] == CURRENT_TTL
and record["type"] in TYPES
and record["proxied"] is PROXIED
):
record["ttl"] = TARGET_TTL
print(
f"Updating {record['name']} ({record['type']}) from {CURRENT_TTL} to {TARGET_TTL}"
)
update_dns_record(record)