How to Create REST API in Magento 2, For Beginners Guide

I am going to show you a step-by-step way to how create custom rest API in Magento 2 store.

Rest API works as Request and Response to perform the certain task in Magento 2.

When we make an application using Magento 2 as a mobile application then we use the request-response concept to receive data or save data.

Magento provides some APIs to you by default you just need to use them.

If you want to change it, you can still change it, but for that, you need to know the basics to customize the core rest api.

In order to create the custom rest API in Magento 2, you can follow the below steps.

Create REST API in Magento 2

 

Step 1: Create REST API Registration File

In the first step of custom rest api we have to create a registration file through which we can register our extension.

Wishusucess/CustomApi/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_CustomApi',
__DIR__
);

 

Step 2: Create Module File

Now in the second step, you have to create a module.xml file of this custom rest api extension so that we can give its basic details like its version, model name, etc.

Wishusucess/CustomApi/etc/module.xml
<?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="Wishusucess_CustomApi" setup_version="1.0.0" />
</config>

 

Step 3: Create REST API Webapi File

In its third step, first of all, we will decide the route URL, how we want to get the data.

Its endpoint will be this route url which will decide that when we hit this endpoint, we will get the data.

Wishusucess/CustomApi/etc/webapi.xml

In this file of Web API, we also decide that the endpoint will work on the key method.

<?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/">
<service class="Wishusucess\CustomApi\Api\CustomInterface" method="getPost"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>

 

Step 4: Dependency Injection for Custom API

Through this file, we will set the preferences and give its path to this file so that it can recognize the extension when run.

Wishusucess/CustomApi/etc.di.xml
<?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="Wishusucess\CustomApi\Api\CustomInterface" type="Wishusucess\CustomApi\Model\Api\Custom"/>
</config>

 

Step 5: Create REST API Custom Interface

This will write the custom interface of the custom API in the file, which will process the data and send it to us as a data response.

Wishusucess/CustomApi/Api/CustomInterface.php
<?php
namespace Wishusucess\CustomApi\Api;
interface CustomInterface
{
/**
* GET for Post api
* @param string $value
* @return string
*/
public function getPost($value);
}

 

Step 6: Custom API Model Class

You have to decide with this file what kind of data you want to take from your application, and how you want to take and show it on the front end.

Wishusucess/CustomApi/Model/Api/Custom.php
<?php
namespace Wishusucess\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; 
}
}

 

Read more: How To Create Blogging Website at Cheap Price in 2022

 

Step 7: Check Custom API with Postman

We will test the custom rest API that we have created above using the postman free tool.

The method which we have mentioned in the file will be selected in this postman's method option.

We will give the endpoint which we have defined in the web api's route url, in the url option.

Now we will hit the endpoint then we will see some data like the below screen in the form of a response.

 

 

Similar Posts:

Product Search REST API: List of REST APIs in Magento 2

Quick and Advanced Search Container REST API Magento 2

Search REST API List: Magento 2 API List of Product Search Endpoint