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

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

What value do they have? How to use them?


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"

    <route url="/V1/calculator/add/:num1/:num2" method="GET">
        <service class="Test\Calculator\Api\CalculatorInterface" method="add"/>
            <resource ref="anonymous"/>

I will hit url,

where route url matches url and paramerters as above,

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


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.

  • Thanks RJ07, can you please tell whats 'rest' 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

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.


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.