Magento 2 Guide To Create Custom Api

As we have already seen how to create a basic module in Magento 2, now lets learn how to create you own custom api. So start creation of your new module by creating module.xml and registration.php. I have created my module ‘CustomApi’ under ‘W3solver’ namespace so below is the code of my module.xml and registration.php

Module.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * w3solver.com
 *
 * @category       W3solver
 * @package        W3solver_CustomApi
 * @Description    module
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
   <module name="W3solver_CustomApi" setup_version="2.0.1"></module>
</config>

Registration.php

<?php

/**
 * I95Dev.com
 *
 *
 * @category    W3solver
 * @package     W3solver_CustomApi
 * @Description Module registration file
 * @author      W3solver
 */
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'W3solver_CustomApi',
    __DIR__
);

Now to create any custom API we need to add ‘webapi.xml’ file under ‘etc’ folder. In below code you can see we have a rest api with method as GET and url as /V1/customapi/shipping/:name, the class to which it refers is W3solver\CustomApi\Api\ShippingInterface

<?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 url="/V1/customapi/shipping/:name" method="GET">
        <service class="W3solver\CustomApi\Api\ShippingInterface" method="exist"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Now we will create the interface(ShippingInterface) file under ‘Api’ folder

<?php
namespace W3solver\CustomApi\Api;
 
interface ShippingInterface
{
    /**
     * check if shipping method exists
     *
     * @api
     * @param string $name shipping method name
     * @return bool
     */
    public function exist($name);
}

The interface is implemented in its respective model, in our case we are implementing it in Shipping.php file placed directly under ‘Model’ Folder.

<?php

namespace W3solver\CustomApi\Model;

use W3solver\CustomApi\Api\ShippingInterface;

class Shipping implements ShippingInterface
{

    public $shipconfig;
    public $scopeConfig;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Shipping\Model\Config $shipconfig
    ) {
        $this->shipconfig = $shipconfig;
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * checks if shipping method is active
     *
     * @api
     * @param string $name shipping method name
     * @return string Greeting message with users name.
     */
    public function exist($name)
    {

        $activeCarriers = $this->shipconfig->getActiveCarriers();
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        foreach ($activeCarriers as $carrierCode => $carrierModel) {
            if ($carrierMethods = $carrierModel->getAllowedMethods()) {
                foreach ($carrierMethods as $methodCode => $method) {
                    $code = $carrierCode . '_' . $methodCode;
                    if ($code == $name) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}

We are all set for your API to test, above API checks the availability of method.

Leave a Comment.