How to update IP address on Cloudflare

Cloudflare has saved me countless times from attacks.

How to update IP address on Cloudflare
Photo by Sander Weeteling / Unsplash

Hosting your services from home? But ISP is providing Dynamic IP Address?
In my scenario, I’ll be using a Synology’s Task Scheduler and Cloudflare.

  1. You’ll need to get all the details for the following variables
#Email associated with your Cloudflare account
EMAIL=""
#Global API Key
KEY=""
#Unique ID provided by Cloudflare
ZONE_ID=""
  1. To get your KEY, go to your Cloudflare Profile > API Tokens and copy your Global API Key

  2. To get your ZONE_ID, go to your domain Overview, on the left panel you’ll see the API Zone ID, copy that.

  3. Put them all together and you get something like this:

#Email associated with your Cloudflare account
EMAIL="admin@example.com"
#Global API Key
KEY="xxxxxxxx"
#Unique ID provided by Cloudflare
ZONE_ID="xxxxxxxx"
#Record type
TYPE="A"
#Domain or Subdomain
NAME="example.com"
#Get IP Address
CONTENT="`curl https://ifconfig.co`"
#Enable Cloudflare proxy
PROXIED="true" 
#In seconds, use 1 for automatic
TTL="1"
   
#The Code
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/" /
	-H "X-Auth-Email: $EMAIL" /
	-H "X-Auth-Key: $KEY" /
	-H "Content-Type: application/json" /
	--data '{"type":"'"$TYPE"'","name":"'"$NAME"'","content":"'"$CONTENT"'","proxied":'"$PROXIED"',"ttl":'"$TTL"'}'
  1. Create a Task Scheduler and drop this script there.
  2. Buy me a drink on ko-fi :)

And that’s how you update Cloudflare with a Dynamic IP Address.