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
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
Interesting use of chatGPT 👍
Thank you :)