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"?> |
Step 2 Create registration.php file
path : app/code/Oodles/Module/
<?php |
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 /**
* 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 use Oodles\Module\Api\ApiInterface; class AllApiModal implements ApiInterface public function __construct( |
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].
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.