free
web stats

Fill the form & Get Installation Support

We don't monkey around your business.

GET IN TOUCH WITH US NOW





captcha

A step by step guide to create custom API in Magento 2

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:

Add registration.php in app/code/Magemonkeys/CustomApi and copy the following code in it:

Add webapi.xml file in app/code/Magemonkeys/CustomApi/etc and copy the following code in it:

Add di.xml file in app/code/Magemonkeys/CustomApi/etc and copy the following code in it:

Add HelloInterface.php file in app/code/Magemonkeys/CustomApi/Api and copy the following code in it:

Add Hello.php file in app/code/Magemonkeys/CustomApi/Model and copy the following code in it:

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:

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.