Magento 2 Create Custom API

Loading...
Magento 2 Create Custom API


Create Custom Rest API in Magento 2

API stands for Application Programming Interface to allow you access the data from an application. API can be called as a middleman between a programmer and an application. When the programmer calls for a request via the middleman, if the request is approved, the right data will be turned back.

REST API basically helps to define a set of functions to perform requests and get responses using the HTTP protocol.


=> Create module.xml at app/code/Tridhyatech/CustomApi/etc/module.xml with the below code:



<‍config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <‍module name="Tridhyatech_CustomApi" setup_version="1.0.0" />
<‍/config>

=> Create registration.php at app/code/Tridhyatech/CustomApi/registration.php and paste the below code:


<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Tridhyatech_CustomApi',
__DIR__
);

=> Create webapi.xml at app/code/Tridhyatech/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/">
        <‍service class="Tridhyatech\CustomApi\Api\CustomInterface" method="getPost"/>
        <‍resources>
            <‍resource ref="anonymous"/>
        <‍/resources>
    <‍/route>
<‍/routes>

=> Create di.xml at app/code/Tridhyatech/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="Tridhyatech\CustomApi\Api\CustomInterface" type="Tridhyatech\CustomApi\Model\Api\Custom"/>
<‍/config>

=> Create CustomInterface.php in path app/code/Tridhyatech/CustomApi/Api/CustomInterface.php


<?php

namespace Tridhyatech\CustomApi\Api;

interface CustomInterface
{
  /**
  * GET for Post api
  * @param string $value
  * @return string
  */

  public function getPost($value);
}

=> Create Custom.php in path app/code/Tridhyatech/CustomApi/Model/Api/Custom.php


<?php

namespace Tridhyatech\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 postman.



Copyright © 2024 Tridhya Tech Limited, Inc. All rights reserved.