How To

How to Fetch All Order Status using REST API in Magento 2

One of the most popular types of API is REST that is designed to take advantage of existing HTTP protocols. Mainly, RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. Also, it doesn’t require to install any addon or libraries if you want to make use of REST API. The only main advantage of REST APIs is that it provides better flexibility & can handle multiple types of calls, return different data formats and can structurally implement correct hypermedia. Which means it lets developers build an API that meets all business needs by reaching new customers.
Same way, Magento 2 API framework let developers, develop new services for syncing Magento 2 store data using CRUD operations.
Also, using Rest API is as easy as pie, if you are familiar with the flow to call APIs in PHP. And if you are developing such services, you probably come across a requirement of fetching order data using REST API. So, we are again back with another blog, that allows you to fetch all order status using REST API in Magento 2.
First, we need to create “Registration.php” file inside our extension at this path.
app\code\VENDOR\EXTENSION


app\code\VENDOR\EXTENSION\etc


 

Now, we have to create one more file “Webapi.xml” inside the same etc folder.
app\code\VENDOR\EXTENSION\etc



 
        
        
            
        
    

After that, we need to create “Di.xml” file in same etc folder.
app\code\VENDOR\EXTENSION\etc



    

After that, we need to create “Getorderstatus.php” file inside our extension API folder to serve a response.
app\code\VENDOR\EXTENSION\Api\


app\code\VENDOR\EXTENSION\Model

Then, execute {base_url}/rest/V1/getorderstatus this URL in postman.
And, you will able to fetch all Magento 2 order status.

That’s it for today! You have successfully added fetched your Magento 2 order statuses and you are free to customize this code according to your need of fetching data using REST API.
Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing an issue while implementing this code.
Happy Coding!

 
Click to rate this post!
[Total: 13 Average: 4.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • Error show when hit url in postman.
    Errors show in log.exception file
    C:\\wamp64\\www\\jodhpur\\vendor\\magento\\framework\\Webapi\\ErrorProcessor.php:205, InvalidArgumentException(code: 0): Each getter must have a doc block. See Chirag\\Customorder\\Api\\Getorderstatus::getorderstatusarray() at C:\\wamp64\\www\\jodhpur\\vendor\\magento\\framework\\Reflection\\TypeProcessor.php:743)"}

    • Please Confirm you have implemented the code properly into the extension, you have to assign proper permission to the authorization User.

    • I created module .After hitting url in postman show errors.
      internal Error. Details are available in Magento log file. Report ID: webapi-5f0592f979532
      Exception.log show errors
      [2020-07-08 09:33:45] main.CRITICAL: Report ID: webapi-5f0592f979532; Message: Each getter must have a doc block. See Chirag\Customorder\Api\Getorderstatus::getorderstatusarray() {"exception":"[object] (Exception(code: 0): Report ID: webapi-5f0592f979532; Message: Each getter must have a doc block. See Chirpn\\Customorder\\Api\\Getorderstatus::getorderstatusarray() at C:\\wamp64\\www\\jodhpur\\vendor\\magento\\framework\\Webapi\\ErrorProcessor.php:205, InvalidArgumentException(code: 0): Each getter must have a doc block. See Chirag\\Customorder\\Api\\Getorderstatus::getorderstatusarray() at C:\\wamp64\\www\\jodhpur\\vendor\\magento\\framework\\Reflection\\TypeProcessor.php:743)
      Please help

    • please check this code.

      class YOURCLASS
      {
      protected $zendClient;

      public function __construct(
      .....
      ZendHttpClient $zendClient
      .....
      ) {

      $this->zendClient = $zendClient;
      }

      public function yourfunction()
      {
      try
      {
      $this->zendClient->reset();
      $this->zendClient->setUri('YOUR THIRD PARTY API URL');
      $this->zendClient->setMethod(ZendHttpRequest::METHOD_POST);
      $this->zendClient->setHeaders([
      'Content-Type' => 'application/json',
      'Accept' => 'application/json',
      'Authorization' => 'Bearer yourvalue',
      ]);
      $this->zendClient->setParameterPost([
      'yourparameter1' => 'yourvalue1',
      ]);
      $this->zendClient->send();
      $response = $this->zendClient->getResponse();
      }
      catch (ZendHttpExceptionRuntimeException $runtimeException)
      {
      echo $runtimeException->getMessage();
      }
      }
      }

Recent Posts

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 days ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 days ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

5 days ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

5 days ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

6 days ago

How to Choose the Best Shopify Development Company?

In today’s digital landscape, e-commerce has become a cornerstone for businesses looking to thrive. Among…

6 days ago