Rest API in Magento 2 defines a set of functions used by the developers to perform requests and get responses using the HTTP protocol. By default, Magento 2 provides some predefined rest APIs like Product API, Order API, Customer data API with which you can take virtual control of everything happening on the site. You can read our complete guide on Magento 2 API if you are just getting started.
Even though Magento 2 has these many rest APIs, they are not enough when it comes to using the custom data and their values. To manage your custom data and fields, you need to create custom rest API in Magento 2 and today, I have come up with the steps for the same 🙂
Also Read: How to Create a Custom Theme in Magento 2?
Steps to Create Custom Rest API in Magento 2:
1. Create module.xml at app/code/Meetanshi/CustomApi/etc/module.xml with the below code:
<?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="Meetanshi_CustomApi" setup_version="1.0.0" /> </config>
2. Create registration.php at app/code/Meetanshi/CustomApi/registration.php and paste the below code:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Meetanshi_CustomApi', __DIR__ );
3. Create webapi.xml at app/code/Meetanshi/CustomApi/etc/webapi.xml with the below code:
<?xml version="1.0"?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd"> <route method="POST" url="/V1/custom/custom-api/:value"> <service class="Meetanshi\CustomApi\Api\CustomInterface" method="getPost"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>
4. Create di.xml at app/code/Meetanshi/CustomApi/etc/di.xml with the below code:
<?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="Meetanshi\CustomApi\Api\CustomInterface" type="Meetanshi\CustomApi\Model\Api\Custom"/> </config>
5. Create CustomInterface.php in path app/code/Meetanshi/CustomApi/Api/CustomInterface.php
<?php namespace Meetanshi\CustomApi\Api; interface CustomInterface { /** * GET for Post api * @param string $value * @return string */ public function getPost($value); }
6. Create Custom.php in path app/code/Meetanshi/CustomApi/Model/Api/Custom.php
<?php namespace Meetanshi\CustomApi\Model\Api; use Psr\Log\LoggerInterface; class Custom { protected $logger; public function __construct( LoggerInterface $logger ) { $this->logger = $logger; } /** * @inheritdoc */ public function getPost($value) { $response = ['success' => false]; try { // Your Code here $response = ['success' => true, 'message' => $value]; } catch (\Exception $e) { $response = ['success' => false, 'message' => $e->getMessage()]; $this->logger->info($e->getMessage()); } $returnArray = json_encode($response); return $returnArray; } }
Finally, run the setup upgrade and deploy commands and you are done with creating a custom rest API in Magento 2. You can check the created custom rest API using [webiste/domain]/swagger
Don’t forget to share this simple guide to help your fellow Magento developers!