Send Domain Renewal and Expiry Notifications using Namecheap API, Twilio SMS and PHP
Time to read: 4 minutes
According to Domain Name Industry Brief a report by VeriSign,a global leader in domain names and Internet security, there were approximately 342.4 million domain name registrations across all top-level domains (TLDs) by the third quarter of 2018, an increase of approximately 2.6 million compared to the second quarter of 2018. This means approximately 28000 domain names were registered per day, and recent projections show the numbers are expected to rise even further in 2019. The stakeholders in this sector, therefore, have to implement a number of systems to manage and automate the huge number of tasks and business processes involved. Domain name sellers, resellers, domain name managers and even individual users need to get domain name related notifications in a timely and reliable manner to efficiently manage their domains.
In this tutorial, we are going to discuss how to send domain name renewal and expiry notifications in Vanilla PHP using Namecheap API and Twilio SMS.
Prerequisites
To follow this tutorial, you need:
- PHP installed
- Twilio Account
- Namecheap Sandbox and Production Accounts
- Composer
Setting up Namecheap Account
Create both sandbox and production Namecheap accounts if you haven’t already, and then request for API access.
The steps to enable API access are similar in both sandbox and production environments. To enable API access, follow these steps:
- 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.
NOTE: You should test your API calls in the sandbox environment and only point them to production once you are sure they are working correctly.
For your Namecheap API calls to work, you need to allow your impacted IPs. These IPs are a list of IPs that are considered to be spam-free and trustworthy.
To add an IP, go to the API page described above, click EDIT and add the IP(s).
Setting up PHP Application
First, we need to install the Twilio PHP SDK using Composer. In your terminal, navigate to your project directory and run the command below:
Next, we need to install the PHP dotenv package to avoid storing sensitive credentials in the codebase. The package loads configuration variables from the .env
file and makes them accessible in our application. In the project directory, run the command below:
Create an .env
file in the root directory of your PHP application and update it with credentials from your Twilio and Namecheap dashboards as shown below:
Creating Our Application
Now that we are done with configurations, let's create our application. We are going to use the Namecheap PHP SDK by Maksym Karazieiev. The SDK is simple and easy to use.
Clone the repository and copy the contents of src/Namecheap
into your application.
Since the tests are done in a sandbox environment, we need to create virtual domain names to simulate how our API calls would work in production.
Just like in production, you first need to check for domain name availability before purchase and thereafter proceed to register it.
Create a file create.php
in the root directory of your application. Open the file and add the code below:
Creating Notifications Functions
With the domain names registered in our sandbox account, we can write code to send renewal and expiry SMS notifications.
The first step is to get a list of all the domains associated with a user using Namecheap API method getList
. The method returns all domain details such as the name and expiry date. Create a file index.php
in the root directory of your application and add getDomainList
function:
Next, we need to get the contact information of the requested domain using Namecheap API method getContacts
. We can then extract the registrant and admin phone numbers from that information. Open index.php
and add the getContacts
function:
We also need a function to renew an expiring domain using Namecheap API method renew
. Add the renewDomain
function:
Finally, let’s create a function to send an SMS using the Twilio API. Open index.php
and add the sendSMS
function:
That is all we need to send domain renewal and expiry notifications. Let’s put all the functions together and send notifications. Add sendNotifications
function to index.php
:
Testing the App
To test the app, you first need to create one or more domains in your Namecheap sandbox account. You can create the domain(s) manually from your sandbox dashboard or update the details in create.php
and hit the URL base_url/create.php
in the browser.
Once you have created the domains, you can then test the notifications by hitting the URL base_url/index.php
in the browser. You should get messages similar to the one shown in the screenshot below.
Note: Remember to update the code to production by setting “sandbox” parameter to false and updating your API keys with your production Namecheap API keys when you move to a production environment.
Conclusion
In this tutorial, we have used Namecheap API and Twilio SMS to send SMS notifications. We have just scratched the surface of what you can do using Namecheap API. Apart from domain name renewal and expiry notifications, you can also use Namecheap API for DNS management, domain transfer, SSL management and so much more. The code is available on Github.
I would love to know what you build using the API.
Feel free to reach out to me:
- Twitter: @mikelidbary
- GitHub: mikelidbary
- Web: www.talwork.net
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.