CloudFlare does provide free plan where one can add one domain and proxy the traffic for FREE. Cloudflare also do give the provision to manage the domain using API’s.

We will see how to create a DNS record and update the record using PowerShell.

To manage the DNS record in cloudflare, we need to obtain the API token and permission to edit the DNS records.

To obtain the API token, login to cloudflare, under your domain portal, click on “Get your API token”. Also note the “Zone Id” in the page, which is required in the API.

Click on “API Tokens” tab and click “Create Token” button.

click the “Use Template” button as shown below.

Select the zone setings as below with the required domain details and click “Continue to summary”

Click “Create Token” to generate the api key.

Copy the generated key to access the DNS using PowerShell.

Sample Token : p6bfghn0rsdfghc-34ggb5tdas8w7ysdftj-C4
Sample ZoneID : 6cd345qefad7c71dfg5q23573017

Validating cloudflare API key using Powershell

Run the below script in PowerShell to validate the access

Invoke-RestMethod -Method Get -Uri "" -Headers @{
 "Authorization" = "Bearer p6bfghn0rsdfghc-34ggb5tdas8w7ysdftj-C4"
 "Content-Type" = "application/json"

Result with success = True shows that the key is valid and accepted.

To know your public IP, please refer to my other post

Adding a new record to Cloudflare DNS

 $token = "p6bfghn0rsdfghc-34ggb5tdas8w7ysdftj-C4"
 $hostname = ""
 $ip = Invoke-RestMethod -uri ""  #Your Public IP 
 $zoneid = "6cd345qefad7c71dfg5q23573017"
 $url = "$zoneid/dns_records"

 $Body = @{
     "type" = "A"
     "name" =  $hostname
     "content" = $ip
     "proxied" = $true # To mask the real IP

 $Body = $Body | ConvertTo-Json

 $result = Invoke-RestMethod -Method post -Uri $url -Headers @{
 "Authorization" = "Bearer p6bfghn0rsdfghc-34ggb5tasdas8w7ysdftj-C4"
 } -Body $Body -ContentType "application/json"


Edit an existing Cloudflare DNS record

 $hostname = ""
 $zoneid = "6cd345qefad7c71dfg5q23573017"
 $token = "p6bfghn0rsdfghc-34ggb5tdas8w7ysdftj-C4"
 $url = "$zoneid/dns_records" 

 # Fetch the record information
 $record_data = Invoke-RestMethod -Method get -Uri "$url/?name=$hostname" -Headers @{
 "Authorization" = "Bearer $token"

 # Modify the IP from the fetched record
 $record_ID = $record_data.result[0].id
 $record_data.result[0].content = Invoke-RestMethod -uri "" #Your Public IP 

 $body = $record_data.result[0] | ConvertTo-Json

 # Update the record
 $result = Invoke-RestMethod -Method put -Uri "$url/$record_ID" -Headers @{
 "Authorization" = "Bearer $token"
 } -Body $body -ContentType "application/json"

Run this script in a Scheduled Task, your Public IP will get updated in DNS !!!

Hope you like this article and thank you for reading.