Magento 2 Custom Rest Api

Posted By : Rahul Gupta | 06-Jan-2022

Do you know how to create a custom Rest API in Magento?

In this blog, we will provide you with information about how to create a custom Rest API in Magento

Step 1 Create a module.xml file to create a new module 

path : app/code/Oodles/Module/etc/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Oodles_Module">
  </module>
</config>

Step 2 Create registration.php file

path : app/code/Oodles/Module/

<?php
  \Magento\Framework\Component\ComponentRegistrar::register(
      \Magento\Framework\Component\ComponentRegistrar::MODULE,
      'Oodles_Module',
      __DIR__
  );

Step 3 Create webapi.xml file

path : app/code/Oodles/Module/etc/

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
   
    <route url="/V1/UpdateSurplus/:surplus" method="GET">
        <service class="Oodles\Module\Api\ApiInterface" method="updateSurplus"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Step 4 Create di.xml file

path : app/code/Oodles/Module/etc/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Oodles\Module\Api\ApiInterface"
                type="Oodles\Module\Model\AllApiModal" />           
    
</config>

Step 4 Create ApiInterface.php file

path : app/code/Oodles/Module/Api/

<?php
namespace Oodles\Module\Api;
 
interface ApiInterface
{

/**
     * GET for get api
     * @param string $surplus
     * @return string
     */
    public function updateSurplus($surplus);
}

Step 5 Create AllApiModal.php file

path : app/code/Oodles/Module/Modal/

<?php
namespace Oodles\Module\Model;

use Oodles\Module\Api\ApiInterface;
use Magento\Checkout\Model\Session;
use Magento\Quote\Model\QuoteRepository;

class AllApiModal implements ApiInterface
{
    protected $_resourceConnection;
    protected $scopeConfig;
    protected $_sessionFactory;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\App\ResourceConnection $resourceConnection,
        Session $checkoutSession,
        \Magento\Customer\Model\Session $sessionFactory,
        QuoteRepository $quoteRepository
    )
    {
        $this->_resourceConnection = $resourceConnection;
        $this->scopeConfig = $scopeConfig;
        $this->_sessionFactory = $sessionFactory;
        $this->checkoutSession = $checkoutSession;
        $this->quoteRepository = $quoteRepository;
    }
    
    public function updateSurplus($surplus=0) {
        
        $quoteId = $this->checkoutSession->getQuoteId();
        $quote = $this->quoteRepository->get($quoteId);
        $sessionManager = $this->_sessionFactory;
        $custId = $sessionManager->getCustomerId();
        $userAuthData = $this->userAuthData($custId);
        $quote->setSurplusUsed($surplus);
        $quote->setAuthCode($userAuthData["value"]);
        $quote->save();
    }
}

Now with all this, we can create an API with URL www.example.com/rest/V1/UpdateSurplus/surplusvalue this API will update surplus-value in the quote table.
In this project, we created custom APIs to update values in the database and much more. To know more about creating or using custom API in Magento reach us at [email protected].

About Author

Author Image
Rahul Gupta

Rahul Gupta is an experienced backend developer with over 1+ years of experience. He has a wide range of technical skills, including proficiency in several programming languages such as C, C++, Java, and PHP. Additionally, Rahul has expertise in using various tools such as Eclipse, Prolog, Linux (Ubuntu), VS Code, Postman, Git, Trello, and Zoho. He also has a strong understanding of web technologies such as HTML, CSS, and JavaScript, as well as RDBMS systems like SQL Server. Rahul has worked with several frameworks including Magento 2, Laravel, and WordPress. He has contributed to several projects such as Fidem Management System, Colmena, Yami Paws, Oodles Technology Site, Email Sending Tool, Dermava, Info Sharing Website Phase, Oodles Scaffold (CRM/HRM/POS), Fiore. He is a self-motivated individual who takes a proactive approach to his work and is always eager to learn new technologies and techniques.

Request for Proposal

Name is required

Comment is required

Sending message..