create-custom-rest-api-in-magento-2

Create Custom REST API

When we develop a fully functional e-commerce store sometime we need to Create Custom REST API to interact with some third party service for shipping, payment, and ERP. 

By default, Magento provides a bunch of API for the Customer, Order, and Products. You can get all API lists using http://<yourwebsite>/swagger but still, sometimes you need some custom API for your custom module or to pass some data which can not be fulfilled by the default API.

So, In today’s blog, I will show you how you can create a Custom REST API.

How to create REST API in Magento 2?

To create a new REST API in Magento 2 We have to follow the below steps.

Step 1: Create a new module.
Step 2: Create a webapi.xml file.
Step 3: Create a di.xml file.
Step 4: Create an Interface file.
Step 5: Create a Model file.

1. Create a new module.

Create new module Rk\CustomRestApi. Check how to Create a Module in Magento 2 to create a new module.

2. Create a webapi.xml.

Create webapi.xml file in the etc folder and add following code.

Path: app/code/Rk/CustomRestApi/etc/webapi.xml

<?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 method="GET" url="/V1/customrestapi/getlist">
        <service class="Rk\CustomRestApi\Api\CustomApiInterface" method="getCustomerList"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

3. Create a di.xml.

Create di.xml file in the etc folder and add following code.

Path: app/code/Rk/CustomRestApi/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="Rk\CustomRestApi\Api\CustomApiInterface" type="Rk\CustomRestApi\Model\CustomApi"/>
</config>

4. Create an Interface File.

Create CustomApiInterface.php file in Api folder and add following code.

Path: app/code/Rk/CustomRestApi/Api/CustomApiInterface.php

<?php

namespace Rk\CustomRestApi\Api;

/**
 * Interface CustomApiInterface
 *
 * @package Rk\CustomRestApi\Api
 */
interface CustomApiInterface
{

    /**
     * Get Customer List
     * @return string
     */
    public function getCustomerList();
}

5. Create a Model File.

Create CustomApi.php file in the Model folder and add following code.

Path: app/code/Rk/CustomRestApi/Model/CustomApi.php

<?php

namespace Rk\CustomRestApi\Model;

/**
 * Class CustomApi
 *
 * @package Rk\CustomRestApi\Model
 */
class CustomApi implements \Rk\CustomRestApi\Api\CustomApiInterface
{
    protected $_customerFactory;

    public function __construct(
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory
    ) {
        $this->_customerFactory = $customerFactory;
    }

    /**
     * Get Customer List
     * @return string
     */
    public function getCustomerList()
    {
        $customerCollection = $this->_customerFactory->create();
        $response = ['status' => false, 'message' => 'Error while fetching data'];
        if (count($customerCollection->getData())) {
            $customerList = $customerCollection->getData();
            $response = ['status' => true, 'data' => $customerList];
        } else {
            $response = ['status' => false, 'message' => 'No customer found'];
        }
        return json_encode($response);
    }
}

 

After following above steps run following commands.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
php bin/magento setup:di:compile

Now when you hit the following url you will get list of customer in json format. http://<yourwebsite>/rest/V1/customrestapi/getlist

 

Hope you find this Tutorial is helpful. Do comment if you face any issue or Contact me If you want any help or customization in your existing Project or Extension.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top