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

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.

hyva theme development
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.

Step 1: First, we need to create “webapi.xml” file inside extension etc folder

app\code\Vendor\Extension\etc

<?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 method="GET" url="/V1/getorderstatus">
        <service class="Vendor\Extension\Api\Getorderstatus" method="getorderstatusarray"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route> 
</routes>

Step 2: Then, we need to create “di.xml” file inside extension etc folder

app\code\Vendor\Extension\etc

<?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="Vendor\Extension\Api\Getorderstatus" type="Vendor\Extension\Model\GetorderstatusModel" />
</config>

Step 3: After that, we need to create “Getorderstatus.php” file inside extension Api folder

app\code\Vendor\Extension\Api\

<?php 
namespace Vendor\Extension\Api; 
interface Getorderstatus 
{ 
    /**
    * Get All Order Status
    * @return string
    */
    public function getorderstatusarray(); 
}

Hire Magento Programmer

Step 4: Lastly, Create “GetorderstatusModel.php” file inside extension Model folder

app\code\Vendor\Extension\Model

<?php 
namespace Vendor\Extension\Model; 
use Vendor\Extension\Api\Getorderstatus;

class GetorderstatusModel implements Getorderstatus 
{ 
    protected $statusCollectionFactory; 
    public function __construct( 
\Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory $statusCollectionFactory 
){ 
        $this->statusCollectionFactory = $statusCollectionFactory; 
    } 
    public function getorderstatusarray() 
    { 
        $options = $this->statusCollectionFactory->create()->toOptionArray(); 
        return $options; 
    } 
}

Now, Go to Admin -> System -> Integrations and Get Access Token value and set authorization value into postman application. Then, execute {base_url}/rest/V1/getorderstatus this URL in postman.

And, you will able to fetch all Magento 2 order status.

Restful api in Magento 2

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.

Magento version upgrade
Happy Coding!

Previous Article

How Social Media Helps To Drive Your Business Sales

Next Article

Why Choose Cloudways Managed Hosting for a Magento store?

Write a Comment
  1. How can i display restapi data in frontend of Magento 2? Can you please let me know.

    1. 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();
      }
      }
      }

    2. Gagan Bafna

      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

  2. Gagan Bafna

    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)”}

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨