2

I need explanation for the different attributes used in webapi.xml in magento2.

<?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/product" method="GET">
    <service class="Vendor\Module\Api\ProductGetInterface" method="getProduct"/>
    <resources>
        <resource ref="Vendor_Module::product" />
    </resources>
</route>

Like what does route url, service class and resource ref mean?

What value do they have? How to use them?

3

Route url specify the url of your call in your case its V1/product so after your store url you need to hit this url. If suppose we are doing sum of two prodcts and my webapi.xml is as below,

<?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/calculator/add/:num1/:num2" method="GET">
        <service class="Test\Calculator\Api\CalculatorInterface" method="add"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

I will hit url,http://10.16.16.190/magentoce27/index.php/rest/V1/calculator/add/1/2

where route url matches url and paramerters as above,

Now, service class defines API Interface for the purpose of method declaration for example,

<?php

namespace Test\Calculator\Api;

interface CalculatorInterface
{
    /**
     * Add two numbers.
     *
     * @param int $num1
     * @param int $num2
     * @return int
     */
    public function add($num1, $num2);
}

?>

If you get a token with a customer username/password, you can only access API methods with <resource ref="self"></resource> or <resource ref="anonymous"></resource>. The <resource ref="anonymous"></resource> methods can be called without any authorization.

12
  • Thanks RJ07, can you please tell whats 'rest' 10.16.16.190/magentoce27/index.php/rest/V1/calculator/add/1/2 in ur url
    – Sushivam
    Jul 19 '16 at 10:28
  • And is route url can be given any name right? since we are using only in URl? Does it need to match with the method name ?
    – Sushivam
    Jul 19 '16 at 10:28
  • I suppose u refered this link alankent.me/2015/07/24/… in ur explanation! Here in AlanKent\CalculatorWebService\Api\Data\PointInterface, is Data\PointInterface really required in our custom module
    – Sushivam
    Jul 19 '16 at 10:33
  • Yes When I was creating example earlier before I followed this link. Its helpful
    – Rushvi
    Jul 19 '16 at 10:37
  • I see many places they use: use Magento\Framework\Api\SearchCriteriaInterface; is it required in custom module
    – Sushivam
    Jul 19 '16 at 10:37
0

resource ref means who can access my API. (Guest user, Admin, Customer)

Route url specify the url of your call.

service class defines API Interface.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.