Skip to content

Instantly share code, notes, and snippets.

@jonshipman
Created January 21, 2025 18:40
Show Gist options
  • Save jonshipman/a5f097e6952e5487a783c253100b5d85 to your computer and use it in GitHub Desktop.
Save jonshipman/a5f097e6952e5487a783c253100b5d85 to your computer and use it in GitHub Desktop.
Update Cloudflare IP Address
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