Home > Uncategorized > Use #Namecheap #API to configure your #DNS

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;

  1. Login to your Namecheap account.
  2. Go to the Profile > Tools menu.
  3. Scroll down to the Business & Dev Tools section.
  4. Click MANAGE, next to Namecheap API Access.
  5. Toggle ON/OFF, read the Terms of Service
  6. 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?&#8221;;
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();
XmlNamespaceManager ns = new XmlNamespaceManager(xdoc.NameTable);
ns.AddNamespace(“xxx”, “http://api.namecheap.com/xml.response&#8221;);
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
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&#8221;;
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;
Console.WriteLine(“Adding Record ” + strName);
hosts.Add(new HostRecord() { Type = “A”, Name = strName, Value = strAddress });
var strResponse = SetHostRecords(strSLD, strTLD, hosts);

This only updates A records, you could modify the code to also update MX records, if you need to change email providers.



Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: