Scripting with the NameCheap API

I manage a large number of domains that are registered through NameCheap. I’ve mentioned before that I’m a big fan of NameCheap. They have the best registration prices, a clean easy interface, and their support is always helpful. But recently I felt like I needed to put some better controls in place to manage and monitor my domain renewals. What I wanted was a Python script that would run daily, check all my domain expirations, and post any alerts or warning to a Slack channel. After poking around NameCheap’s website for a few minutes I found that they expose an API for customers to use. Perfect, I could now start scripting with the NameCheap API.

Setting up the NameCheap API

Before I could do any scripting I need to enable the API in my account. Login to your NameCheap account, from the left hand navigation select Tools from under Profile. On the Tools page scroll to the bottom and find NameCheap API Access, click Manage. On this page you need to change the API Access to On. NameCheap Support will have to manually approve your request. The request should take 1 – 2 days. I ended up waiting about a week, then I contact Support and they expedited the request for me. Once your request is approved you need to complete 2 things in the NameCheap API Access page.

API Key – Generate an API Key, you’ll need this later in your script.

Whitelisted IPs – You need to add all of the IP addresses that will be accessing the API. I added two of my servers where I will run the script, and my laptop’s Internet IP address.

Accessing the NameCheap API

Now that you’ve whitelisted your IP addresses and created an API key, you’re ready to start to start accessing the API. NameCheap has full documentation on their API available, so that’s a good place to start. NameCheap’s API gives you access to do almost everything you can do from their website. A lot of customers use this to create custom scripts and tools for registering and managing domains. Another consideration of this API is that it returns responses in an XML format. I would have prefered JSON, but I’ll discuss that below.

This is the IP address of the client that is making the request, your server’s IP address.

NameCheap Python Scripting

As I mentioned earlier I want a script that will run weekly, iterate through all of my domains and flag any that have an expiration coming up. I’ll then post a message in my Slack channel to notify me. NameCheap’s getList command will pull all my domains with their expiration dates, so that will cover my requirement. I’m going to use the Slack module I wrote about in an earlier article to handle the Slack integration.

As I mentioned earlier, the API response is in XML. While I was researching this I stumbled on a GitHub project for a Python client to the NameCheap API. The PyNamecheap project by Bemmu is pretty nice Python class that handles all the necessary XML parsing. So if you’re planning to use Python, there’s no need to reinvent the wheel here. I Imported the PyNamecheap Api class into my script.

api.domains_getList() will return an iterable of dictionary objects. Each domain will have a dictionary object, and that object will contain the domain properties. To interrogate each domain, I can use the following code: