Tutorial: How to register and purchase a domain using GoDaddy API

Alvin Brown provides a tutorial on how to use GoDaddy’s API to register and purchase domains.

Today’s tutorial aims to help many domain investors with a desire to harness the power of technology to streamline the registration and purchasing of domains using GoDaddy’s API.

Instead of using GoDaddy’s web interface and having to perform many time-consuming clicks at risk of losing out on hand registering domains, this tutorial will be the foundation for what could become an automated purchasing script (with a bit of elbow grease and database integration).

Script automation and database integration won’t be covered in this tutorial, but you can likely search the web for a few simple tutorials on automating PHP and MySQL database integration.

First things first, let’s discuss what you’ll need to start this tutorial. If you’re new to using GoDaddy’s Developer Portal and API, then I recommend perusing the following documentation:

  • GoDaddy Developer Portal – Create Account
  • GoDaddy API Credentials
  • GoDaddy API Documentation

TIP: More in-depth details are provided in the following tutorial: Get Started Using GoDaddy API with PHP.

API Method for Purchasing Domain via GoDaddy

Once you have your API account and credentials created, go to the Domains API section and search for the method to purchase and register domains: /v1/domains/purchase.

The only parameter required to make a successful API request to register and purchase a domain is the body — an instance document expected to match the JSON schema returned by the /v1/domains/purchase/schema/{tld} method.

Within the body parameter, the following attributes must be submitted:

  • agreedAt — the time of day (DATETIME) for registration and purchase
  • agreedBy — the IP address making the API request
  • agreementKeys — the acknowledge of and agreement to the terms and conditions for registering and purchasing domains with GoDaddy; value is DNRA
  • period — number years to register domain
  • privacy — whether or not to add privacy to purchase
  • nameServers — primary and secondary name servers to be set for domain
  • renewAuto — whether or not auto renewing is enabled or disabled at the time of purchase

In addition, be sure to have the following contact information on hand for the Billing, Registrant, Tech, and Admin sections of the body:

  • Address One
  • Address Two
  • City
  • Country
  • Postal Code
  • State
  • Email
  • Phone
  • Fax
  • Job Title
  • First Name
  • Last Name
  • Middle Name
  • Organization

Setting up required and optional variables

Open a text editor of your choice, naming and saving the following file: domain-purchase.php.

The first and most critical step is defining API Key and Secret variables with your respective GoDaddy API key and secret values.

$API_SECRET = “your secret”;
$API_KEY = “your key”;

Without properly defined secret and key variables with their respective values, this tutorial will be unable to register and purchase domains using the API.

Next, set and define the domain variable with the domain you desire to purchase.

$domain = “alvinbrownie.xyz”;

If you so desire, you could integrate this tutorial with a previous tutorial about searching for whether or not a domain is available to register and purchase before attempting the domain purchase API method.

The next set of variables were mentioned in the previous section as attributes of the body. Set their values accordingly:

$dnra = “DNRA”;
$autoRenw = “true”; // either true or false
$period = 2; // for 2 year registration
$privacy = “false”; // either true or false for privacy to be included
$agreeAtTime = “2019-11-22T18:50:13Z”; //format: YYYY-MM-DDTHH:MM:SSZ
$agreedByIP = “123.123.123.123”; //IP address of your client or web server executing script

// custom name server values to point/redirect domains
$nameserver_one = “ns50.domaincontrol.com”;
$nameserver_two = “ns60.domaincontrol.com”;

Last but not least, the following contact information variables need values.

// address information for domain
$addressOne = “12345 Test Blvd”;
$addressTwo = ” “;
$city = “Austin”;
$country = “US”;
$postalCode = “78910”;
$state = “Texas”

// contact information for domain
$email = “info@dnstrategies.com”;
$fax = “+1.5121234567”;
$jobTitle = “Owner”;
$nameFirst = “Joe”;
$nameLast = “Doe”;
$nameMiddle = “B”;
$organization = “JBD Strategies, LLC”
$phone = “+1.5121234567”;

The most tricky variables are the fax and phone variables. Both variables need to include the country code and the decimal after country code. Do not include spaces, dashes, or parentheses.

TIP: Using a domain you currently own, perform a GoDaddy’s Whois Lookup to view an example of what the expected contact information, specifically the phone, fax, and date for $agreeAtTime variable, should be and how it should be formatted.

Once all variables are defined with their respective values, each variable can be concatenated as values within the JSON of the body variable content (see below).

$bodyContent = ‘
{
“consent”: {
“agreedAt”: “‘.$agreeAtTime.’”,
“agreedBy”: “‘.$agreedByIP.’”,
“agreementKeys”: [
“‘.$dnra.’”
]
},
“contactAdmin”: {
“address1”: “‘.$addressOne.’”,
“address2”: “‘.$addressTwo.’”,
“city”: “‘.$city.’”,
“country”: “‘.$country.’”,
“postalCode”: “‘.$postalCode.’”,
“state”: “‘.$state.’”
},
“email”: “‘.$email.’”,
“fax”: “‘.$fax.’”,
“jobTitle”: “‘.$jobTitle.’”,
“nameFirst”: “‘.$nameFirst.’,
“nameLast”: “‘.$nameLast.’”,
“nameMiddle”: “‘.$nameMiddle.’”,
“organization”: “‘.$organization.’”,
“phone”: “‘.$phone.’”
},
“contactBilling”: {
“addressMailing”: {
“address1”: “‘.$addressOne.’”,
“address2”: “‘.$addressTwo.’”,
“city”: “‘.$city.’”,
“country”: “‘.$country.’”,
“postalCode”: “‘.$postalCode.’”,
“state”: “‘.$state.’”
},
“email”: “‘.$email.’”,
“fax”: “‘.$fax.’”,
“jobTitle”: “‘.$jobTitle.’”,
“nameFirst”: “‘.$nameFirst.’,
“nameLast”: “‘.$nameLast.’”,
“nameMiddle”: “‘.$nameMiddle.’”,
“organization”: “‘.$organization.’”,
“phone”: “‘.$phone.’”
},
“contactRegistrant”: {
“addressMailing”: {
“address1”: “‘.$addressOne.’”,
“address2”: “‘.$addressTwo.’”,
“city”: “‘.$city.’”,
“country”: “‘.$country.’”,
“postalCode”: “‘.$postalCode.’”,
“state”: “‘.$state.’”
},
“email”: “‘.$email.’”,
“fax”: “‘.$fax.’”,
“jobTitle”: “‘.$jobTitle.’”,
“nameFirst”: “‘.$nameFirst.’,
“nameLast”: “‘.$nameLast.’”,
“nameMiddle”: “‘.$nameMiddle.’”,
“organization”: “‘.$organization.’”,
“phone”: “‘.$phone.’”
},
“contactTech”: {
“addressMailing”: {
“address1”: “‘.$addressOne.’”,
“address2”: “‘.$addressTwo.’”,
“city”: “‘.$city.’”,
“country”: “‘.$country.’”,
“postalCode”: “‘.$postalCode.’”,
“state”: “‘.$state.’”
},
“email”: “‘.$email.’”,
“fax”: “‘.$fax.’”,
“jobTitle”: “‘.$jobTitle.’”,
“nameFirst”: “‘.$nameFirst.’,
“nameLast”: “‘.$nameLast.’”,
“nameMiddle”: “‘.$nameMiddle.’”,
“organization”: “‘.$organization.’”,
“phone”: “‘.$phone.’”
},
“domain”: “‘.$domain.’”,
“nameServers”: [
“‘.$nameserver_one.’”,”‘.$nameserver_two.’”
],
“period”: ‘.$period.’,
“privacy”: ‘.$privacy.’,
“renewAuto”: ‘.$autoRenew.’
}’;

Making API Request to Register and Purchase Domain

With all necessary variables defined, PHP and curl will be used to make the API request to register and purchase a domain. To do so, define a url variable to have the following value:

$url = “https://api.godaddy.com/v1/domains/purchase”;

Next, set your API key and secret variables:

$header = array(
“Authorization: sso-key $API_KEY:$API_SECRET”,
‘Content-Type: application/json’,
‘Accept: application/json’
);

Once the url and header variables are set, the following curl attributes can established with their necessary variables as arguments — $url, $timeout, $bodyContent, and $header.

//open connection
$ch = curl_init();
$timeout=60;

//set the url and other options for curl
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘POST’); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyContent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);

//execute call and return response data.
$result = curl_exec($ch);

//close curl connection
curl_close($ch);

// decode the json response
$dn = json_decode($result, true);

The aforementioned curl code does the heavy lifting to execute the call using GoDaddy’s API to purchase and registered submitted domain.

Using an if/else statement, the last bit of code to add is error checking should a non successful API request be made.

Once error checking is added, then the $errmsg variable can be printed or echoed to the screen to know the status of your API Request to purchase and registered submitted domain.

$errmsg = ”;

// check if error code
if(isset($dn[‘code’])){

$errmsg = explode(“:”,$dn[‘message’]);

$errmsg = ‘
< h2 style=”text-align: center;”>’.$errmsg[0].’

 
‘;

} else {

$errmsg = ‘Domain purchased.’;

}

echo $errmsg;

Closing thoughts

That’s all there is to this tutorial, and you’re now be able to register and purchase a domain using GoDaddy’s API in a fraction of the time it takes to use the web interface.

As stated at the beginning of this tutorial, this tutorial can be extended to include a domain search for availability, and easily automated and integrated with MySQL using a bit of PHP.

In addition, a submission form much like the domain search could be used to submit domains for registration and purchase using GoDaddy’s API.

I encourage you to download the tutorial code, make necessary modifications, and see how far you can take this simple, yet robust tool.

DOWNLOAD domain-purchase.zip for GoDaddy Domains API

Last but not least, please don’t hesitate to leave comments should you have questions or encounter technical challenges with tutorial implementation.

Thanks and that’s all for now!

© WebsiteHostingReview.org 2019. This is copyrighted content. Website Hosting Review full-text RSS feeds are made available for personal use only, and may not be published on any site without permission. If you see this message on a website, contact editor (at) WebsiteHostingReview.org. Latest domain news at WHR.NEWS: Website Hosting Review.