Use #Namecheap #API to configure your #DNS
If you have domains registered with Namecheap, and you’d like a programmatic way to update DNS setttings – i.e. you want to change the IP address of a domain automatically in the event of a server outage, here’s how their API works.
First, you have to active the API via the control panel as follows;
- Login to your Namecheap account.
- Go to the Profile > Tools menu.
- Scroll down to the Business & Dev Tools section.
- Click MANAGE, next to Namecheap API Access.
- Toggle ON/OFF, read the Terms of Service
- Enter your account password.
Then add IP addresses that you are going to use to access the API. i.e. your local IP, and server IP.
When you call the command namecheap.domains.dns.setHosts on the namecheap API, it overwrites all existing DNS records, so you need to first read all the DNS records on a domain, then set them all back again, plus your change. So first step is to read all DNS records.
So, create a class to hold a DNS record
class HostRecord
{
public string Name { get; set; }
public string Type { get; set; }
public string Value { get; set; }public override string ToString()
{
return string.Format(“[{0}] {1} : {2}”, Name, Type, Value);
}
}
The ToString() is just for debug purposes
Another helper method we need is some code to get our current IP, which is required by the API
private static string GetIP()
{
var wc = new WebClient();
var strIP = wc.DownloadString(“http://icanhazip.com”).Trim();
return strIP;
}
To get DNS hosts for a Domain registered on Namecheap;
private static List<HostRecord> GetHostsFromDomain(string SLD, string TLD)
{
var strUrl = “https://api.namecheap.com/xml.response?”;
strUrl += “ApiUser=….”;
strUrl += “&ApiKey=…..”;
strUrl += “&UserName=…..”;
strUrl += “&Command=namecheap.domains.dns.getHosts”;
strUrl += “&ClientIp=” + GetIP();
strUrl += “&SLD=” + SLD;
strUrl += “&TLD=” + TLD;
var wc = new WebClient();
var strXml = wc.DownloadString(strUrl);
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(strXml);
XmlNamespaceManager ns = new XmlNamespaceManager(xdoc.NameTable);
ns.AddNamespace(“xxx”, “http://api.namecheap.com/xml.response”);
var hosts = xdoc.SelectNodes(“//xxx:host”, ns);
var lHosts = new List<HostRecord>();
Console.WriteLine(“Previous DNS Records”);
foreach (XmlNode host in hosts)
{
var record = new HostRecord()
{
Name = host.Attributes[“Name”].Value,
Type = host.Attributes[“Type”].Value,
Value = host.Attributes[“Address”].Value
};
lHosts.Add(record);
Console.WriteLine(record);
}
return lHosts;
}
If your domain is “boo.com”, then SLD is “boo” and TLD is “com”.
Once you have the list of Hostrecords, you can add or modify this collection, and then update the hosts as follows;
public static string SetHostRecords(string SLD, string TLD, List<HostRecord> records)
{
var strUrl2 = “https://api.namecheap.com/xml.response”;
string strPostData = “ApiUser=….”;
strPostData += “&ApiKey=….”;
strPostData += “&UserName=….”;
strPostData += “&Command=namecheap.domains.dns.setHosts”;
strPostData += “&ClientIp=” + GetIP();
strPostData += “&SLD=” + SLD;
strPostData += “&TLD=” + TLD;
for (int i = 0; i< records.Count; i++)
{
strPostData += “&HostName” + (i+1) + “=” + records[i].Name;
strPostData += “&RecordType” + (i+1) + “=” + records[i].Type;
strPostData += “&Address” + (i+1) + “=” + records[i].Value;
strPostData += “&TTL” + (i+1) + “=60”;
}
var wc2 = new WebClient();
wc2.Headers[“Content-Type”] = “application/x-www-form-urlencoded”;
var strXml2 = wc2.UploadString(strUrl2,strPostData);
return strXml2;
}
If you want to create a console app for this, where you can pass in command line arguments, then here’s some sample code for this;
var strSLD = args[0];
var strTLD = args[1];
var strName = args[2];
var strAddress = args[3];
var hosts = GetHostsFromDomain(strSLD, strTLD);
if (hosts.Any(h => h.Name == strName))
{
Console.WriteLine(“Updating Record “+ strName);
hosts.First(h => h.Name == strName).Value = strAddress;
}
else
{
Console.WriteLine(“Adding Record ” + strName);
hosts.Add(new HostRecord() { Type = “A”, Name = strName, Value = strAddress });
}
var strResponse = SetHostRecords(strSLD, strTLD, hosts);
Console.WriteLine(strResponse);
This only updates A records, you could modify the code to also update MX records, if you need to change email providers.
-
November 2, 2021 at 11:56 amAutomatically add #Sedo verification to a batch of domain names using #NameCheap #API | Network Programming in .NET
-
November 2, 2021 at 12:32 pmAutomatically add #Sedo verification to a batch of domain names using #NameCheap #API - Online Code Generator
-
November 2, 2021 at 1:51 pm➧Automatically add #Sedo verification to a batch of domain names using #NameCheap #API • Softbranchdevelopers