This repository provides a Laravel controller to simplify interactions with the Namecheap API, allowing for easy management of domain registrations and queries.
- PHP 7.3 or higher
- Laravel 7 or higher
- Guzzle HTTP client (for making API requests)
-
Install Laravel Project
If you don't have a Laravel project, create a new one by running:
composer create-project --prefer-dist laravel/laravel namecheap-api-integration
-
Add the Controller File
Place the
NamecheapApiController.phpfile in the following directory:app/Http/Controllers/Service/ -
Configure Environment Variables
Add your Namecheap API credentials to your
.envfile:NAMECHEAP_USERNAME=your_username NAMECHEAP_API_KEY=your_api_key
-
Install Guzzle HTTP Client
Install the Guzzle HTTP client if you haven't already:
composer require guzzlehttp/guzzle
The main controller is NamecheapApiController. In this controller, API credentials are set through the constructor.
public function __construct(Request $request)
{
$this->namecheapUserName = env('NAMECHEAP_USERNAME', 'YOUR_USERNAME');
$this->namecheapApiKey = env('NAMECHEAP_API_KEY', 'YOUR_API_KEY');
// other initialization
}Make sure to replace 'YOUR_USERNAME' and 'YOUR_API_KEY' with the actual credentials from your .env file.
This is the constructor function that initializes the class with the provided request. It sets up necessary API credentials.
Sets the base URL or the API host. Used to determine the target API endpoint.
$this->setApiHost('api.namecheap.com');Initializes query parameters required for API requests. This is called before making an API call.
$this->initQueryParam('namecheap.domains.check', ['DomainList' => $domain]);Sets a specific query parameter for an API request.
$this->setApiQueryParam('ApiUser', 'your_user');
$this->setApiQueryParam('ApiKey', 'your_api_key');Returns the full URL for the API endpoint, combining the host and query parameters.
$url = $this->getApiURL();Sends the API request to the server and returns the response.
$response = $this->send();Fetches the list of domains associated with the user.
$response = $this->getDomainListCommand()->send();Checks whether the specified domain is available for registration.
$response = $this->checkDomainCommand('example.com')->send();Checks if a domain is eligible for transfer.
$response = $this->checkDomainTransferCommand('example.com')->send();Initiates the domain transfer to Namecheap.
$response = $this->makeDomainTransferCommand('example.com', 'EPP_CODE')->send();Gets the current transfer status of a domain.
$response = $this->getDomainTransferStatus('example.com')->send();Fetches the registration price of a domain extension (e.g., .com, .net).
$response = $this->getDomainPriceCommand('com')->send();Creates a new domain registration for the given domain and period.
$response = $this->createDomainCommand('example.com', 1)->send();Renews the specified domain for the given number of years.
$response = $this->renewDomainCommand('example.com', 1)->send();Sets the contact information for the domain (e.g., registrant, admin).
$response = $this->setDomainContact('example.com', [
'RegistrantFirstName' => 'John',
'RegistrantLastName' => 'Doe',
])->send();Fetches the nameserver information for a specific domain.
$response = $this->getDomainNameServerInfoCommand('example.com')->send();Sets custom nameservers for a domain.
$response = $this->setDomainNameServerInfoCommand('example.com', 'ns1.example.com,ns2.example.com')->send();Fetches the registrar lock status of a domain.
$response = $this->getDomainRegisterLockCommand('example.com')->send();Sets the registrar lock status for a domain (e.g., 'LOCK' or 'UNLOCK').
$response = $this->setDomainRegisterLockCommand('example.com', 'LOCK')->send();Gets the contact details associated with a domain.
$response = $this->getDomainContactCommand('example.com')->send();Updates the contact information for a domain.
$response = $this->setDomainContactCommand('example.com', [
'RegistrantFirstName' => 'John',
'RegistrantLastName' => 'Doe',
])->send();Fetches the email forwarding settings for a domain.
$response = $this->getDomainEmailForwardingCommand('example.com')->send();Sets the email forwarding configuration for a domain.
$response = $this->setDomainEmailForwardingCommand('example.com', [
'ForwardTo' => 'contact@example.com',
])->send();Gets the full information about a domain.
$response = $this->getDomainInfo('example.com')->send();Fetches a list of domain extensions that are eligible for transfer.
$response = $this->getTransferExtensions()->send();Extracts the price of a domain from the XML response.
$price = $this->getDomainPriceFromXml($xmlResponse, 'com', 'register', 'USD');Fetches the domain price along with renewal prices if needed.
$price = $this->getDomainPrice($request, 'com', 'register', 'USD', true);Converts a domain registration price to its multi-year equivalent.
$multiYearPrice = $this->domainPriceToMuliYearPrice($price, 3); // for 3 yearsIf an error occurs, the response will include an error message. You can check the status of the API response to handle errors.
$response = (new NamecheapApiController($request))->checkDomainCommand('example.com')->send();
if ($response->status == 'ERROR') {
echo 'Error: ' . $response->error->message;
}This integration allows you to interact with Namecheap's API for domain management tasks, such as checking domain availability, transferring domains, updating DNS settings, and more. Simply add your Namecheap credentials to the .env file, and you’re ready to use these features in your Laravel project
.
For additional information, please refer to the official Namecheap API documentation.