4

I want to know is there any REST API available to get customer's Order in Magento 2?

  • Let me know if you have still any query ? – Manthan Dave Feb 14 '17 at 4:40
4

To get customer's Order using REST API try below code :

First you need to get accesstoken of admin and then needs to call order api with passing that AccessToken.

Code for get admin accesstoken :

$userData = array("username" => "admin", "password" => "admin123");
$ch = curl_init("http://yourwebsiteurl/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);

Code for get order details :

$ch = curl_init("http://yourwebsiteurl/index.php/rest/V1/orders/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

$result = curl_exec($ch);

$result = json_decode($result, 1);
echo '<pre>';print_r($result);

here is the link for all the rest api available in magento 2 - http://devdocs.magento.com/guides/v2.1/rest/list.html

  • @Dipak Valecha Let me know if you have still any query ? – Manthan Dave Feb 14 '17 at 4:40
  • 1
    If there is any way to use customer token without admin token. It is not security wise. – Jsparo30 Apr 6 '17 at 9:48
  • As a part of security , if you want to use customer token then you can get only specific customer order data not all data . example - if you create token of X customer then you can get only that X customer data – Manthan Dave Apr 6 '17 at 9:50
  • yes I tried that for a specific user ex. https://domain.com/index.php/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=customer_email&searchCriteria[filter_groups][0][filters][0][value]=aaa@gmail.com. but to run this I must use admin token not customer. Do you have any help ? – Jsparo30 Apr 6 '17 at 9:58
0

To get an admin token you need to have the credentials setup within magento and call /V1/integration/admin/token endpoint with the username/password and get the token. Then pass the token to other requests with Authorization: Bearer

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.