ChatGPT wrote me a script to export DNS records from CloudFlare

in blurtaustralia •  2 years ago 

I use CloudFlare for hosting DNS on my websites. I needed a way to export my DNS records from all my hosted domains. ChatGPT made my life easier! Thanks for the code snippet which I just run via cron daily. #chatgpt #openai #cloudflare #javascript

IMG_9935.jpg

const api_token = "YOUR_API_TOKEN";
const headers = {
    "Authorization": "Bearer " + api_token
};

// Get all zones (i.e. domain names)
fetch("https://api.cloudflare.com/client/v4/zones", {headers: headers})
    .then(response => response.json())
    .then(data => {
        let zones = data.result;
        // Loop through each zone and download DNS records
        zones.forEach(zone => {
            let zone_id = zone.id;
            fetch(`https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records`, {headers: headers})
                .then(response => response.json())
                .then(data => {
                    // data.result contains an array of DNS records for this zone
                    console.log(data.result);
                })
                .catch(error => {
                    console.log("Error: ", error);
                });
        });
    })
    .catch(error => {
        console.log("Error: ", error);
    });

Cronjob: 0 12 * * * node /path/to/script.js > DNS.json

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!
Sort Order:  
  ·  2 years ago  ·  

Interesting use of chatGPT 👍

Thank you :)