Magento 2 Rest API have many default methods. I want to use those method and display data.

For that i have create one new user and assigned full admin access.

Trying to get the product information by SKU using default method: GET /V1/products/:sku which is given here

Now i am running following url:

When i run this url in browser it

    <message>Consumer is not authorized to access %resources</message>

if i use curl code like this:


   //parameters passing with URL

     $data = array("username" => "wuser", "password" => "admin@123");

     $data_string = json_encode($data);

        $ch = curl_init($apiURL);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));

         $token = curl_exec($ch);

      //decoding generated token and saving it in a variable

      $token=  json_decode($token); 

   //decoding generated token and saving it in a variable

    $headers = array("Authorization: Bearer ".$token);

//API URL to get all Magento 2 modules


     $ch = curl_init($requestUrl);
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 $result = curl_exec($ch);

             //decoding result

             $result=  json_decode($result);

    //printing result
            echo "<pre>";

Which works!

How can i access the default methods and show output? Do i need to curl for this?

For standard practice and MobileAPP output is it good to use CURL to get there?

Is it necessary to write entire token code to get response?

using token it takes time to reponse. Is there any better way?

Any help would be apprecaited.

  • @patel please answer.
    – jack
    Commented Jun 21, 2018 at 13:35
  • Hello Jack, Yes you need to pass Access token for Specific API, But here in your case GET /V1/products/:sku is accessed by admin only, that's why you're facing an error when you don't use admin's token Commented Jun 21, 2018 at 14:03
  • you can change resource for the solution. Commented Jun 21, 2018 at 14:03
  • Hey Aditya, Thanks for reply. But don;t you think it will give perfomance hit using token way? Do i need to create my custom api and have anonymous resource along with the same code to give access to guest and customer? I want to make sure it doesn't hit performance
    – jack
    Commented Jun 22, 2018 at 4:48
  • no i have a solution and it will not affect security and performance :) Commented Jun 22, 2018 at 5:00

1 Answer 1


There are two way for this solution -

1) First let's go to the way where you're facing an issue.

  • You created the admin user and give all access but still not abel to fetch the product details.

So,here's is the solution and it's working fine in my Magento Version 2.2.4

I created an Admin User.

enter image description here

  • Now i need to fetch the token for that admin user, so i did it using POSTMAN.

Url request type POST


which returns token.

"xcph3thmoaiyt0ylm58lf2dn150qlkfr" something like this.

Now using this token we'll fetch the product by SKU for your URL.

URL request type GET

Payload - Headers

Authorization Bearer xcph3thmoaiyt0ylm58lf2dn150qlkfr

You need pass token in header, that is authntication process which checks the user access in Magento.

In response you'll get Product data

Solution 2

This path of magento catalog API's


To fetch products using SKU.

<route url="/V1/products/:sku" method="GET">
        <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="get"/>
            <resource ref="Magento_Catalog::products" />

In here, there's resource defined. it means whichever user have access of "Magento_Catalog::products" Only those user can access this API.

<resource ref="Magento_Catalog::products" />

So, if you want to use this API for other users like customer then you can change the resource to self.

Again it will require of token for the same, We don't compromise the security of course

<route url="/V1/products/:sku" method="GET">
            <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="get"/>
                <resource ref="self" />

Now, it means we can access it.

  • Using Admin's Token
  • Using Admin User's Token
  • Using Customer's Token.

Yeah,You need to override this file in your module


and then you can access it from customer's token too.

Let me know if you have any query.

  • magento.stackexchange.com/questions/228815/… Commented Jun 23, 2018 at 6:43
  • Thanks for your reply aditya. I have one question like, i am developing this APIS to given json data for android app so to get the response using rest api I need to use token everytime? for ex: To fetch the product i need to write the admin token code and fetch the response. for category list i need to write the admin token code along with category code and so. so don't you think everytime token generation will delay in response? Or there is some best methodologies?
    – jack
    Commented Jun 25, 2018 at 4:46
  • No @jack Token is for security purpose and it's essential and every admin token expires in 4 hours and it's configurable from admin side. and you can achieve all this using customer Token too as per solution 2. Commented Jun 25, 2018 at 4:49
  • Let me know if you have any query. Commented Jun 25, 2018 at 4:49
  • Yes have one query it means we need to pass token with every response we send and we need to pass the token in to response we get? Also, Do i need to write curl code everytime to get response of all apis? what's use of this? <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="get"/>
    – jack
    Commented Jun 25, 2018 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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