@kortoaus/cf-ddns
Version:
Automated DDNS updater for Cloudflare DNS records using their API
37 lines (36 loc) • 1.04 kB
JavaScript
// src/index.ts
import * as dotenv from "dotenv";
dotenv.config();
var main = async () => {
try {
const ipRes = await fetch("https://api.ipify.org");
const ip = await ipRes.text();
console.log(`Current IP: ${ip}`);
const response = await fetch(
`https://api.cloudflare.com/client/v4/zones/${process.env.CF_ZONE_ID}/dns_records/${process.env.CF_RECORD_ID}`,
{
method: "PUT",
headers: {
Authorization: `Bearer ${process.env.CF_API_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
type: "A",
name: process.env.CF_RECORD_NAME,
content: ip,
ttl: 300,
proxied: false
})
}
);
const result = await response.json();
if (result.success) {
console.log("\u2705 DNS record updated!");
} else {
console.error("\u274C Failed to update:", result.errors || result);
}
} catch (err) {
console.error("\u{1F525} Unexpected error:", err);
}
};
main();