0

If you use Magento's REST APIs to get product, you use this endpoint:

GET /V1/products/{sku}

This endpoint returns response with category_ids in it. What I want to do is to get the product with category names instead of ids. I tried using GET /V1/categories/list with some search criteria and it worked but this endpoint needs admin privileges which can't be provided to show categories of a product inside the application for guest or registered customer without admin authorization.

The question is, can I do such a thing without building a custom endpoint that gets me either category names by list of ids or a product with categories names instead of ids?

2

Yes you can create your custom end point without asking for Access Token/ Authorization to get the category with its name.

Below is the steps :

create webapi.xml file in your VendorName/ModuleName/etc directory.

<?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/customrouter/customcategorylistingurl/" method="GET">
        <service class="VendorName\ModuleName\Api\CustomCategoryListingInterface" method="getcategorylisting"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Create a di.xml file in your VendorName/ModuleName/etc directory and put below code in it.

<?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="VendorName\ModuleName\Api\CustomCategoryListingInterface"
                type="VendorName\ModuleName\Model\CustomCategory" />
</config>

Now in your Custom Model(CustomCategory) - add the code(logic) for get all the categories with the category name then using above endpoint you will get the categories with the name.

Note : As mention above in your webapi.xml file you must pass ref as anonymous - this parameter is used for guest use which will not asked for authorization.

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.