JavaScript script to get a machine's public IP and update Cloudflare DNS records using Cloudflare API. Works on anything that can run Node.js.
Node.js v16 or later
- Clone the repository
- Run
npm install
to install dependencies - Rename
config.example.json
toconfig.json
- Ger your Cloudflare Zone ID and Account ID from Cloudflare Dashboard -> Overview and set them in
config.json
. - Click on Get your API token and create a new API Token with Edit DNS rights for your domain and set it in
config.json
. - Run
node getDNS.js
to get the DNS id(s). You only need to run this once. Configure all your domains with their IDs inconfig.json
CLOUDFLARE_DNS_RECORDS
array. - Other settings such as
proxied
ortype
are hard-coded in theupdateDNS
function but can be changed as needed. Refer to the Cloudflare DNS API documentation. node .
to run the script manually
You can use a cron job to run the script at a specific interval. For example, to run the script every 10 minutes, add the following line to your crontab by running crontab -e
:
*/10 * * * * cd /path/to/script/cloudflare-ddns/ && /usr/bin/node /path/to/script/cloudflare-ddns/index.js
You can use Task Scheduler to run the script at a specific interval. Create a new basic task with the following settings:
Program/script: "Powershell"
Add arguments (optional): cd "C:\path\to\script\" | node index.js