A Python script to automatically update your Cloudflare DNS record with your current public IP address.
pip install requests- Log in to your Cloudflare dashboard
- Go to My Profile → API Tokens
- Click "Create Token"
- Use the "Edit zone DNS" template
- Set permissions:
- Zone > DNS > Edit
- Set Zone Resources:
- Include > Specific zone > yourdomain
- Click "Continue to summary" and "Create Token"
- Copy the token (you won't see it again!)
- Go to your Cloudflare dashboard
- Click on your domain
- Scroll down on the Overview page
- Find "Zone ID" in the right sidebar under "API"
- Copy the Zone ID
Edit cloudflare-ddns.py and replace:
YOUR_API_TOKEN_HEREwith your API tokenYOUR_ZONE_ID_HEREwith your zone IDYOUR_DOMAIN_NAME_HEREwith your domain name
chmod +x cloudflare-ddns.pypython3 cloudflare-ddns.pyYou should see output like:
Cloudflare Dynamic DNS Updater for example.org
--------------------------------------------------
Current public IP: 203.0.113.45
DNS record doesn't exist, creating...
✓ Created DNS record: example.org -> 203.0.113.45
Or if the record exists:
Cloudflare Dynamic DNS Updater for example.org
--------------------------------------------------
Current public IP: 203.0.113.45
Current DNS IP: 203.0.113.45
✓ IP address is already up to date, no changes needed
To run this automatically every 5 minutes:
- Open crontab:
crontab -e- Add this line (adjust path to where you saved the script):
*/5 * * * * /usr/bin/python3 /path/to/cloudflare-ddns.py >> /var/log/cloudflare-ddns.log 2>&1
- Open Task Scheduler
- Create Basic Task
- Set trigger to "Daily" and repeat every 5 minutes
- Action: Start a program
- Program:
pythonorpython3 - Arguments:
C:\path\to\cloudflare-ddns.py
In the script, you can modify:
- TTL: Change
"ttl": 120to a different value (in seconds). 120 = 2 minutes is good for dynamic DNS - Proxied: Change
"proxied": FalsetoTrueif you want Cloudflare to proxy the traffic (hides your real IP but adds Cloudflare features)
- Check your internet connection
- The script uses ipify.org to get your public IP
- Verify your API token is correct
- Check that your token has DNS edit permissions
- Verify the Zone ID is correct
- Check your Zone ID
- Verify the domain name in RECORD_NAME is correct
Keep your API token secure! Don't commit it to version control. Consider using environment variables:
import os
CLOUDFLARE_API_TOKEN = os.environ.get('CLOUDFLARE_API_TOKEN')
ZONE_ID = os.environ.get('CLOUDFLARE_ZONE_ID')Then set them in your environment:
export CLOUDFLARE_API_TOKEN="your_token_here"
export CLOUDFLARE_ZONE_ID="your_zone_id_here"