TODO for Custom Get Rest API

  • Register Module: create module.xml and registration.php

  • Creating di.xml file

  • Creating webapi.xml file with Model and API Interface

  • Deploy and Execute on browser

Custom Get Rest API

Today we will create our own rest API in Magento 2 so that we can provide access to our server data using API.

See what Magento says about it “The Magento web API framework provides integrators and developers the means to use web services that communicate with the Magento system."

Note: This is a basic Get Rest API demonstration and can be used to provide access to server store data for remote devices like Apps, CRM, and any application need in data of your Magento Application.

Path : app/code/Nilesh/JsonApi/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="Nilesh_JsonApi" setup_version="1.0.0"/>
</config>

Path : app/code/Nilesh/JsonApi/registration.php

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Nilesh_JsonApi', __DIR__);

Till now we have registered our module so that our module presence get recognize by Magento and get render

Path : app/code/Nilesh/JsonApi/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/nilesh-jsonapi/color">
        <service class="Nilesh\JsonApi\Api\ColorManagementInterface" method="getColor"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
        <data>
            <parameter name="key" force="true">%key%</parameter>
        </data>
    </route>
</routes>

In the above snippet, we have used a route tag to the same way as we do for a Controller and use “url" attr to describe what URL this API should be accessible and also in that we have set method as “GET".

Inside the route tag, there is a service tag that allows us to define what class and method get used by request API.

In our case, we use “NileshJsonApiApiColorManagementInterface" as a class and “getColor" as a method responsible for the response.

Then there is “resources" tag which we kept as “anonymous" as ref which mean anyone can access this API or a public API

Then we have “data" tag followed by “parameter" tag which is simply responsible for parm that we are going to pass in our case it’s “key"

Path : app/code/Nilesh/JsonApi/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="Nilesh\JsonApi\Api\ColorManagementInterface" type="Nilesh\JsonApi\Model\ColorManagement"/>
</config>

Now we have created di.xml file with preference as we have pointed to ColorManagementInterface class in webapi.xml file “service" tag.

You might come up with an idea of using Model directly but all developers count that as bad practice.

And creating two files with few readable codes is not a bad idea for me to keep best practice or for security in mind.

Path : app/code/Nilesh/JsonApi/Api/ColorManagementInterface.php

<?php

declare(strict_types=1);

namespace Nilesh\JsonApi\Api;

interface ColorManagementInterface
{
    public function getColor($key);
}

I take this class to define a method with parameters that are going to get implements by the model class.

In our case “ColorManagement" class implements “ColorManagementInterface" class

Path : app/code/Nilesh/JsonApi/Model/ColorManagement.php

<?php

declare(strict_types=1);

namespace Nilesh\JsonApi\Model;

class ColorManagement implements \Nilesh\JsonApi\Api\ColorManagementInterface
{
    public function getColor($key)
    {
        return 'hello api GET return the $key ' . $key;
    }
}

I want to keep this simple by saying this as our area where we put our logic, connect with database, manipulate the response and many more

Command : php bin/magento setup:upgrade
Command: php bin/magento setup:static-content:deploy -f
Command : php bin/magento c:f
Browse : :lt:your_domain:gt://rest/V1/nilesh-jsonapi/color?key=hello

For more : https://devdocs.magento.com/guides/v2.4/get-started/bk-get-started-api.html

Happy coding and Thanks

Please comment below whatever every comes in your mind weather it is question, anger, suggestion, improvement. Please write down below. I want to hear you and if you want me to make a post on something or wanting snippet please that also you can mention in comment.

I will surely answer your comment not letting go unanswered or help you in your query.

Thanks Again.

You all are the semicolon to my statements; Please support me; Thanks