Updating CloudFlare with your Home IP using Python

This blog will teach you how to write a Python script that allows you to update a CloudFlare domain with your home IP address. The problem we’re trying to solve is that my home has a dynamic IP address instead of a static one. If I have services at my house I want to access from the internet, should the IP address change, I want to be able to consistently go to my home services. You can use Dynamic DNS services out there (like DuckDNS) but with this script, you’re basically just writing your own, under the assumption you already have a domain you pay for and have the nameservers configured in CloudFlare (e.g. you could be using GoDaddy or Namecheap but CloudFlare must be managing your domain).

In a previous, but deprecated post, we used the Python CloudFlare library to communicate with the CloudFlare API.

In this article, we’re going vanilla by simply using the requests library.

The code can be downloaded from my GitHub account: https://github.com/sigmaenigma/CloudFlare/tree/main/DNSRecordUpdate

What does the Code Do?

  1. Loads in a Configuration File with the:
    • CloudFlare API token
    • Zone Name / Zone ID (unique identifier for the domain you want to update)
    • Record Name you want updated
    • force_update flag (true or false) whether you want to force the update via the API
  2. Verify if the Token works
  3. Get the Current IP address by hitting the ipify API
  4. Get the Zone Data from CloudFlare (this has the old IP)
  5. Compare the Old IP and the New IP
    • If the New IP is different than the Old IP, Update the CloudFlare record (if the force_update flag is set to true instead of false, the update happens regardless)
  6. That’s it!

Running the Code

Go ahead and do a git clone of the below repository.

Navigate to the /DNSRecordUpdate folder. In here, we have two files. A config.json file and the actual python file cloudflare_dns_record_update.py

The configuration file below contains everything we need to run the python script.

  1. First, you need to obtain your Cloudflare API key. You can find your API key in your Cloudflare account settings.
  2. Log into your Cloudflare account.
  3. Click on your profile picture in the top-right corner of the screen and select My Profile
  4. Click on the API Tokens tab on the left
  5. In the API Keys page, click on Create Token
  6. You should see Edit zone DNS as a template. Click on that
  7. Make sure the Permissions shows ‘Edit‘. Also make sure to select your specific zone (e.g. example.com)
  8. Click Continue to Summary
  9. Click Create Token
  10. Copy your token

Now that you have your token, save that to your token parameter in the config.json file. The zone_name can be found when you click on Overview. You’ll see the Zone ID on the bottom right. Copy that and paste it into Zone Name.

Record Name is the domain you wish to update. In my case, this will be a subdomain (e.g. test.example.com instead of example.com).

You’ll also notice a force_update flag above. This bypasses a piece of code where the saved IP in CloudFlare and the current IP detected are compared. Even if the IP addresses are the same, this forces an update in CloudFlare.

Once you have your config.json file updated, you can now run the python script below. Make sure you have the requests library installed:

With the requests library installed, you can run the below code by running:

This will execute the code and if everything ran correctly, you should see the comment updated verifying you ran the IP address.

Now that you’ve verified the script runs, it’s up to you to automate it. I use HomeAssistant to run a script like this but you can either run it manually, via a cron job or you can have a separate service fire off the script at a specified interval.

Hope you found this useful!

Leave a Comment

Your email address will not be published. Required fields are marked *