---
title: "Magento 2: How to Get Order Details with Custom Extension Attributes using REST API"
url: "https://magecomp.com/blog/magento-2-get-order-details-with-custom-extension-attributes-using-rest-api/"
date: "2021-01-18T13:08:16+00:00"
modified: "2025-02-20T06:58:07+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 610
reading_time: "4 min read"
summary: "Hello Magento Friends,"
description: "Magento default don't return custom extension attribute values Learn How to Get Order Details with Custom Extension Attributes using REST API"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Top Magento 2 Extensions for Maximizing Google Analytics 4 Integration"
    url: "https://magecomp.com/blog/best-google-analytics-4-magento-2-extensions/"
  - title: "Solved – Undefined Index jobs Error While Running Cron After Magento 2 Migration"
    url: "https://magecomp.com/blog/solved-undefined-index-jobs-error-while-running-cron-after-magento-2-migration/"
  - title: "How to Configure Shipping Methods by Customer Group in Magento 2?"
    url: "https://magecomp.com/blog/configure-shipping-methods-by-customer-group-magento-2/"
---

# Magento 2: How to Get Order Details with Custom Extension Attributes using REST API

_Published: January 18, 2021_  
_Author: Dhiren Vasoya_  

![How to Get Order Details with Custom extension attributes using REST API Magento 2](https://magecomp.com/blog/wp-content/uploads/2021/01/How-to-Get-Order-Details-with-Custom-extension-attributes-using-REST-API-Magento-21-1024x512.webp)

**Hello Magento Friends,**

I hope all are doing great. Today I will be explaining Magento 2: How to Get Order Details with Custom Extension Attributes using REST API. In case you missed reviewing our previous blog, [**How to Get Region Code by Region ID in Magento 2**](https://magecomp.com/blog/get-region-code-by-region-id-in-magento-2/).
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-5.webp)](https://magecomp.com/services/hyva-theme-development/)

## Introduction:
When customers purchase products, Magento 2 handles various entities, like products, customers, and orders. Magento provides different ways to fetch that entity data. One of them is to [**Get Order Details using REST API in Magento 2**](https://magecomp.com/blog/magento-2-get-order-details-using-rest-application-program-interface/).
But suppose we have used some custom extension attributes with orders or products or customers. In that case, Magento default does not return that custom extension attributes values while we get details using the REST API.
No worries! With the below code’s help, we will learn How to Get Order Details with Custom Extension Attributes using REST API. So let us get started.
## Steps to Get Order Details with Custom Extension Attributes using REST API in Magento 2:
**Step 1:** Create **extension_attributes.xml** file inside **app\code\Vendor\Extension\etc** folder and add the code as mentioned below:
```
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
	<extension_attributes for="MagentoSalesApiDataOrderInterface">
		<attribute code="delivery_charge" type="decimal" />
		<attribute code="payment_charge" type="decimal"/>
	</extension_attributes>
</config>
```

**Step 2:** Next create **events.xml** file inside **app\code\Vendor\Extension\etc** folder and add the below code:
```
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_load_after">
        <observer name="sales_order_load_attribute" instance="VendorExtensionObserverOrderLoadAfter"/>
    </event>
</config>
```

**Step 3:** Next create **OrderloadAfter.php** file inside **app\code\Vendor\Extension\Observer** folder and add the below-mentioned code:
```
<?php
namespace VendorExtensionObserver;

use MagentoFrameworkEventObserverInterface;

class OrderLoadAfter implements ObserverInterface
{
    protected $_orderExtension;

    public function __construct(MagentoSalesApiDataOrderExtension $orderExtension)
    {
        $this->_orderExtension = $orderExtension;
    }
    public function execute(MagentoFrameworkEventObserver $observer)
    {
            $order = $observer->getOrder();

            $extensionAttributes= $order->getExtensionAttributes();
            if ($extensionAttributes === null)
            {
                $extensionAttributes = $this->_orderExtension;
            }
            $deliverycharge = $order->getData('delivery_charge');
            $paymentcharge = $order->getData('payment_charge');

            $extensionAttributes->setData('delivery_charge',$deliverycharge);
            $extensionAttributes->setData('payment_charge',$paymentcharge);

            $order->setExtensionAttributes($extensionAttributes);
    }
}
```

[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-5.webp)](https://magecomp.com/services/hire-magento-programmer/)

**Step 4:** Next clear the generated folder from Magento root and clear cache.
**Step 5:** Finally, execute the **getOrderDetails.php** script file inside your Magento root folder and add the below code. (You can use this code in your Magento file system. e.g., you can even use it in your custom module).
```
<?php
$userData = array("username" => "admin", "password" => "adminPassword");
$baseUrl = "http://domain.com/index.php"; // your magento base url
$ch = curl_init($baseUrl."/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);

$ch = curl_init($baseUrl."/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 '';print_r($result);?>
```

That’s it!
## Conclusion:
Accordingly, you can Get Order Details with Custom Extension Attributes using REST API in Magento 2.Mention your difficulties in the comment part. I will be happy to solve it.
[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)

Keep Learning! Stay informed!
**Happy Coding**


---

_View the original post at: [https://magecomp.com/blog/magento-2-get-order-details-with-custom-extension-attributes-using-rest-api/](https://magecomp.com/blog/magento-2-get-order-details-with-custom-extension-attributes-using-rest-api/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-10 11:58:05 UTC_  
