Created
January 21, 2025 18:40
-
-
Save jonshipman/a5f097e6952e5487a783c253100b5d85 to your computer and use it in GitHub Desktop.
Update Cloudflare IP Address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cloudflare from 'cloudflare'; | |
const apiEmail = process.env.apiEmail; | |
const apiKey = process.env.apiKey; | |
const domain = process.env.domain; | |
const cloudflare = new Cloudflare({ | |
apiEmail, | |
apiKey, | |
}); | |
const ip = await fetch("https://api.ipify.org").then((r) => r.text()); | |
console.log(ip); | |
async function exec() { | |
let page = await cloudflare.zones.list(); | |
for (const zone of page.result) { | |
const id = zone.id; | |
const { result } = await cloudflare.dns.records.list({ zone_id: id }); | |
for (const record of result) { | |
if (!record.id) continue; | |
if (record.name === domain) { | |
if (ip === record.content) continue; | |
await cloudflare.dns.records.update(record.id, { | |
content: ip, | |
zone_id: zone.id, | |
type: "A", | |
name: record.name, | |
}); | |
} | |
} | |
} | |
} | |
if (ip) await exec(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment