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="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Oodles_Module">

Step 2 Create registration.php file

path : app/code/Oodles/Module/


Step 3 Create webapi.xml file

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

<?xml version="1.0"?>
<routes xmlns:xsi="" 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"/>
            <resource ref="anonymous"/>

Step 4 Create di.xml file

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

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Oodles\Module\Api\ApiInterface"
                type="Oodles\Module\Model\AllApiModal" />           

Step 4 Create ApiInterface.php file

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

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/

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);

Now with all this, we can create an API with URL 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 is hard working and quick learner and have experience of Magento, SQL, PHP. He is good team player and he is very honest towards his work.

Request for Proposal

Name is required

Comment is required

Sending message..