Magento as default provide API, but if you want to manage different data or you want to add a custom field, then Magento API will not be able to attain your requirement. That generates the demand to develop custom API through which you will able to manage your data and fields.
In this article, I will explain you the process of creating API(Application Program Interface) in Magento 2.
Add module.xml file in app/code/Magemonkeys/CustomApi/etc and copy the following code in it:
1 2 3 4 |
<?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="Magemonkeys_CustomApi" setup_version="1.0.0" /> </config> |
Add registration.php in app/code/Magemonkeys/CustomApi and copy the following code in it:
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_CustomApi', __DIR__ ); |
Add webapi.xml file in app/code/Magemonkeys/CustomApi/etc and copy the following code in it:
1 2 3 4 5 6 7 8 9 |
<?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/hello/name/:name" method="GET"> <service class="Magemonkeys\CustomApi\Api\HelloInterface" method="name"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes> |
Add di.xml file in app/code/Magemonkeys/CustomApi/etc and copy the following code in it:
1 2 3 4 5 |
<?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="Magemonkeys\CustomApi\Api\HelloInterface" type="Magemonkeys\CustomApi\Model\Hello" /> </config> |
Add HelloInterface.php file in app/code/Magemonkeys/CustomApi/Api and copy the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace Magemonkeys\CustomApi\Api; interface HelloInterface { /** * Returns greeting message to user * * @api * @param string $name Users name. * @return string Greeting message with users name. */ public function name($name); } |
Add Hello.php file in app/code/Magemonkeys/CustomApi/Model and copy the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace Magemonkeys\CustomApi\Model; use Magemonkeys\CustomApi\Api\HelloInterface; class Hello implements HelloInterface { /** * Returns greeting message to user * * @api * @param string $name Users name. * @return string Greeting message with users name. */ public function name($name) { return "Hello, " . $name; } } |
To test REST you can go to http://{domain_name}/rest/V1/{method}/{attribute}/{value}.
Example: http://magento2.loc/rest/V1/hello/name/Jayesh.
This is how the response should look like for this example:
1 |
<response>Hello, Jayesh</response> |
Hope this guide helped you understand the procedure of creating custom API in Magento 2. If you have any queries regarding the blog, please comment below.
[crayon-63d3d9afdd031343270772/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.