Skip to content

Instantly share code, notes, and snippets.

@tommyready
Last active January 4, 2024 17:52
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tommyready/3f357cff773c9aa08b157e9d0669f040 to your computer and use it in GitHub Desktop.
Save tommyready/3f357cff773c9aa08b157e9d0669f040 to your computer and use it in GitHub Desktop.
Update Cloud Flare DNS Record via Cloud Flare API v4 using C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json;
namespace CloudFlareApiClient
{
class Program
{
static void Main(string[] args)
{
string CF_ApiUrl = "https://api.cloudflare.com/client/v4/";
string CF_AuthEmail = "{INSERT YOUR Cloud Flare Account Email Address HERE}";
string CF_AuthKey = "{INSERT YOUR AUTH-KEY HERE}";
string CF_DNS_ZONE_ID = "{INSERT YOUR CLOUD FLARE DNS ZONE ID}";
string CF_DNS_RECORD_ID = "{INSERT YOUR CLOUD FLARE DNS RECORD ID}";
string publicIpAddress = GetIPAddress(); // Get Public IP of my Home Server
// Build a JSON string to PUT to CloudFlare API
Dictionary<string, string> jsonData = new Dictionary<string, string>();
jsonData.Add("type", "A");
jsonData.Add("name", "mydomain.com");
jsonData.Add("content", publicIpAddress.Trim()); // Trim Public IP Address Variable
string jsonPostData = JsonConvert.SerializeObject(jsonData);
PutJsonDataToApi(CF_ApiUrl,CF_AuthEmail,CF_AuthKey,CF_DNS_ZONE_ID,CF_DNS_RECORD_ID,jsonPostData);
}
public static string GetIPAddress()
{
WebClient webClient = new WebClient();
return webClient.DownloadString("https://icanhazip.com/");
}
public static void PutJsonDataToApi(string apiUrl, string authEmail, string authKey, string dnsZoneId, string dnsRecordId, string jsonData)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(apiUrl+ "/zones/" + dnsZoneId + "/dns_records/" + dnsRecordId);
httpWebRequest.ReadWriteTimeout = 100000;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "*/*";
httpWebRequest.Method = "PUT";
httpWebRequest.Headers.Add("X-Auth-Email", authEmail);
httpWebRequest.Headers.Add("X-Auth-Key", authKey);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
jsonData = jsonData.Replace("\n", "");
jsonData = jsonData.Replace("\r", "");
streamWriter.Write(jsonData);
streamWriter.Flush();
streamWriter.Close();
}
try
{
HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
string respStr = new StreamReader(resp.GetResponseStream()).ReadToEnd();
Console.WriteLine("Response : " + respStr); // Output response to Console Window
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
}
}
@ljchuello
Copy link

Hello Tommy,

You may be interested to know that I've created a library specifically for managing DNS records. It's called CloudFlare.Dns and is available on GitHub and Nuget Package. It also includes a well-documented step-by-step wiki with each use case.

It's very comprehensive and easy to use, and I'm confident it can readily adapt to your project or others. I would appreciate it if you could review it and provide your feedback on it.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment