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:
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
- 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 = “[email protected]”;
$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].’ </h2>
‘;
} 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!
you know there’s a json_encode function as well? you don’t have to type the json code in manually instead just create a standard php array and use json_encode on it. it’ll be much easier to maintain too.
While I appreciate your comments, this is a warning that your comments are deleted if you continue to use a fake email address when you submit comments. Please also choose a different handle.
Yes, I know there’s a json_encode function that can encode a php array. This example is not fully optimized code ready for a production environment. It’s simply a quick and dirty example meant for non-tech person to build upon. 🙂
Great Post Alvin, will save time to all coders who want to use the GoDaddy API to register names.
Thanks Francois! 🙂
I got this => parsing body body from “” failed, because invalid character ‘\n’ in string literal
Why this is happened..
Are you using downloaded file or did you copy/paste code from this web page? Could be ‘\n’ was blindly copied/pasted.
after modifying bodyContent into array
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($bodyContent));
i get this error
^ array:2 [▼
“code” => “INVALID_BODY”
“message” => “Request body doesn’t fulfill schema, see details in `fields`”
]
You likely need brackets ‘[ ]’. Try the following: https://www.godaddy.com/community/Managing-Domains/DDNS-update-failed/td-p/103222
I’m not into coding but willing to learn
So, the only programming knowlege requiered is PHP?
Thanks
Correct
Hi,
I have this error :
“code” => 400
“message” => “parsing body body from “” failed, because invalid character ‘\n’ in string literal”
Hi Jack – Feel free to email me at tech [@] kickstartcommerce.com.
This worked wonderfully on the GoDaddy environment however when i put it together on a script something in the cURL portion goes wrong and i end up with an error “404”
Hi Ownway – Feel free to email me at tech [@] kickstartcommerce.com.
After obtaining the orderId response, how can you make the purchase with a gateway to assign that domain to a customer?
Alvin thank you so much for this script. Finally getting round to it!
I am wondering for the domain variable line:
$domain = “aDomain1.com”
Can we make it an array to register multiple domains?
$domain = array(“aDomain1.com, “aDomain2.com”, “aDomain3.com”);
I wonder would that work?
¿some of you have faced this case?
{
“code”: “INVALID_PAYMENT_INFO”,
“message”: “Unable to authorize credit based on specified payment information”
}
when executing the api.ote purchase, I could not buy / register a domain