ContactUs REST API: Create Magento 2 Custom Contact Us REST API

So as we all know Magento 2 needs token authentication first then only that will allow you to communicate with his data so we have to request first and then include with security token to gain access to the ContactUs REST API endpoints.

So basically we use this contact us form to link in the footer of Magento 2 shopping store. This contact us allows customers to send a message directly to the store owner which is a way to communicate with the website owner.

When a customer faces any kind of issue then they contact the website representative or help team which help them to requests or concerns resolved immediately,

Here we will communicate the customer name, email, phone and comment through the contact us Rest API keys.

app/code/Wishusucess/ContactUs/registration.php

app/code/Wishusucess/ContactUs/etc/module.xml

app/code/Wishusucess/ContactUs/etc/di.xml

app/code/Wishusucess/ContactUs/etc/webapi.xml

app/code/Wishusucess/ContactUs/Model/ContactUsRepository.php

app/code/Wishusucess/ContactUs/Api/ContactUsInterface.php

Magento 2 ConatctUs REST API Keys

 

Register Contact US Rest API Module

app/code/Wishusucess/ContactUs/registration.php

<?php
/**
* Developer:Wishusucess Magento 2x Developer Team
* Module: Wishusucess_ContactUs
* Author: Hemant Singh Magento 2X Developer
* Website: http://www.wishusucess.com/
*
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_ContactUs',
__DIR__
);

 

Step 2: Module Basic Information

app/code/Wishusucess/ContactUs/etc/module.xml

<?xml version="1.0"?>
/**
* Developer: Wishusucess Magento 2x Developer Team
* Module:    Wishusucess_ContactUs
* Author:    Hemant Singh Magento 2X Developer
* Website:   http://www.wishusucess.com/
*
*/
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Wishusucess_ContactUs" setup_version="1.0.0" />
</config>

 

Define Dependency Injection

app/code/Wishusucess/ContactUs/etc/di.xml

<?xml version="1.0"?>
/**
* Developer:Wishusucess Magento 2x Developer Team
* Module: Wishusucess_ContactUs
* Author: Hemant Singh Magento 2X Developer
* Website: http://www.wishusucess.com/
*
*/
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Wishusucess\ContactUs\Api\ContactUsInterface" type="Wishusucess\ContactUs\Model\ContactUsRepository" />
</config>

 

Create ContactUs REST API Route ID

Magento 2 Web APIs are Magento default features in Magento 2 and the onward version of Magento which help and save a lot of time for Magento 2 developers to communicate with the Magento 2 stores. They directly use their keys and can fill the contact us form from the different servers using these keys.

This file helps us define the route ID so here we have defined the route URL which is /V1/Wishusucess/contact_us and we are using the post method to send the data to the Magento store.

app/code/Wishusucess/ContactUs/etc/webapi.xml

<?xml version="1.0"?>
/**
* Developer:Wishusucess Magento 2x Developer Team
* Module: Wishusucess_ContactUs
* Author: Hemant Singh Magento 2X Developer
* Website: http://www.wishusucess.com/
*
*/
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/Wishusucess/contact_us" method="POST">
<service class="Wishusucess\ContactUs\Api\ContactUsInterface" method="sendContactUs"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>

 

Rest API Model Class of Contact US

The model class basically we are using to send the details from the contact us form using the sendEmail() method and we are sending the data of name, email, phone, comment.

Then we will validate the data using validatedParams( ) method which checks the validation at the server-side.

app/code/Wishusucess/ContactUs/Model/ContactUsRepository.php

<?php
/**
* Developer:Wishusucess Magento 2x Developer Team
* Module: Wishusucess_ContactUs
* Author: Hemant Singh Magento 2X Developer
* Website: http://www.wishusucess.com/
*
*/
namespace Wishusucess\ContactUs\Model;
use Wishusucess\ContactUs\Api\ContactUsInterface;
use Magento\Contact\Model\MailInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;

class ContactUsRepository implements ContactUsInterface
{
/**
* @var Context
*/
private $context;

/**
* @var MailInterface
*/
private $mail;

/**
* @param Context $context
* @param MailInterface $mail
*/
public function __construct(
Context $context,
MailInterface $mail
) {
$this->context = $context;
$this->mail = $mail;
}

/**
* @inheritdoc
*/
public function sendContactUs($name, $email, $phone, $comment){
if ($this->validatedParams($name, $email, $phone, $comment)) {
try {
$this->sendEmail(["name"=>$name, "email"=>$email, "phone"=>$phone, "comment"=>$comment]);
return 'Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.';
} catch (LocalizedException $e) {
throw $e;
} catch (\Exception $e) {
throw $e;
} 
}
}

/**
* @param array $post Post data from contact form
* @return void
*/
private function sendEmail($post)
{
$this->mail->send(
$post['email'],
['data' => $post]
);
}

/**
* @return array
* @throws \Exception
*/
private function validatedParams($name, $email, $phone, $comment)
{
if (trim($name) === '') {
throw new LocalizedException(__('Enter the Name and try again.'));
}
if (trim($comment) === '') {
throw new LocalizedException(__('Enter the comment and try again.'));
}
if (false === \strpos($email, '@')) {
throw new LocalizedException(__('The email address is invalid. Verify the email address and try again.'));
}

return true;
}
}

 

Contact Us Interface in API

app/code/Wishusucess/ContactUs/Api/ContactUsInterface.php

<?php
/**
* Developer:Wishusucess Magento 2x Developer Team
* Module: Wishusucess_ContactUs
* Author: Hemant Singh Magento 2X Developer
* Website: http://www.wishusucess.com/
*
*/
namespace Wishusucess\ContactUs\Api;

/**
* @api
*/
interface ContactUsInterface
{
/**
* Contact Us
*
* @param string $name
* @param string $email
* @param string $phone
* @param string $comment
* @return string success
* @throws \Magento\Framework\Exception\AuthenticationException
*/
public function sendContactUs($name, $email, $phone, $comment);
}

 

Hire Magento 2 Expert Developer to Develop Your Store

 

Related Post:

Custom Shipping Text Filed: Show Custom Text Magento 2

Share Product on WhatsApp: With Products Image And URL in Magento 2

 

Recommended Post:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

SEO Packages: How Much Do SEO Packages Cost in India, SEO Pricing