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.
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62876a0681a07200382868/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...