I have a lot of websites behind Cloudflare’s excellent firewall system.
with google new update policy to use https,I want to change all my domain to https.But if I have to do all changes manually it will take a very long time, and there will be a high likelihood of mistakes being made.Solution of course is to use the Cloudflare API to make this all happen automatically. But unfortunately I cant make it work, and get me a Headache LOL,is there any solution to make it simple.Ive tried google it and cant found any solution.
Nope, there’s no simple solution that’s been posted. Not that it can’t be made simple.
Someone could write a fancy single-purpose script that pulls all your Zone IDs, then enables this feature.
Or they could write a very complicated interactive script that pulls all your Zone IDs, then asks you which feature to enable or disable.
But I feel your pain. Sometimes I want to implement (or turn off) a global change and I have to click through every single one of my domains.
hi @keenan.alfatih85 old thread. But wow did this one take some time to figure out. I want to share with the community. Hope this python script can help many other people and save time.
Python 3 script using Global API and email to auth
import requests
import json
# Cloudflare API details
CF_EMAIL = 'your cloudflare email address to sign in here'
GLOBAL_API_KEY = 'YouGlobal-API-Key'
ACCOUNT_ID = 'Your-account-key-here' # Ensure this is correct for the account you want to modify
API_URL = "https://api.cloudflare.com/client/v4/zones"
HEADERS = {
"X-Auth-Email": CF_EMAIL,
"X-Auth-Key": GLOBAL_API_KEY,
"Content-Type": "application/json"
}
def get_all_zones():
all_zones = []
page = 1
per_page = 100 # Adjust this to increase the number of zones per page, if needed
print(f"Fetching all zones for account ID: {ACCOUNT_ID}...")
while True:
response = requests.get(f"{API_URL}?page={page}&per_page={per_page}", headers=HEADERS)
if response.status_code != 200:
print(f"Error fetching zones: {response.status_code}")
print(response.text)
break
data = response.json()
# Print raw response for debugging
print(f"Raw API Response for page {page}:")
print(json.dumps(data, indent=4))
if not data.get('success', False):
print("Error: No success flag found in API response.")
break
# Filter zones by account ID
zones = [
zone for zone in data['result'] if zone['account']['id'] == ACCOUNT_ID
]
if not zones:
print(f"No zones found for account ID: {ACCOUNT_ID}.")
break
all_zones.extend(zones)
# Check if we've fetched all zones (i.e., if the number of zones fetched is less than the per_page limit)
if len(data['result']) < per_page:
break
page += 1
return all_zones
def enable_always_use_https(zone_id):
url = f"{API_URL}/{zone_id}/settings/always_use_https"
data = {"value": "on"}
response = requests.patch(url, headers=HEADERS, json=data)
if response.status_code == 200:
print(f"Enabled 'Always Use HTTPS' for zone: {zone_id}")
else:
print(f"Failed to enable 'Always Use HTTPS' for zone: {zone_id}")
print(response.text)
def main():
zones = get_all_zones()
if zones:
print(f"Zones to be updated:")
for zone in zones:
print(f"- {zone['name']} (ID: {zone['id']})")
print("\nEnabling 'Always Use HTTPS' for all zones...")
for zone in zones:
enable_always_use_https(zone['id'])
print("\nHTTPS redirect process completed for all zones!")
else:
print("No zones found or invalid API credentials!")
if __name__ == "__main__":
main()
I hope this helps any one else search for this randomly I would say common use, but seems to be non existent on internet.