Magento 2.4: How to get order information using REST API

Last updated on 13th September 2022

This article is intended for integrators working on Magento 2.3 or higher for order import into third party ERP or CRM systems (Sage, Orderwise, Hubspot etc). The images and examples are taken from a Magento 2.4 installati

The REST API endpoint that is used to get the orders is /rest/default/V1/orders/

Step 1: Enable API access on Magento using your admin account

You have to register a web service on admin. From Magento Admin navigate to System > Extensions > Integration > Add New Integration to generate an integration.

Add new Integration

Only the name and admin password are required fields.

Create Integration

Go into the API tab and select the resources you want to give access to. There is an option to select all, however it is best practice to restrict access to required resources only.

In this case we need access to Sales.

Create Sage Integration

Once you save this, you need to activate it.

Activate Sage Integration

When you click activate you will see a popup which allows you to save the Consumer Key, Consumer Secret, Access Token and Access Token Secret. The access key can be used to make REST API calls from a client such as Postman or programatically from Python, PHP, Curl etc.,. On latest versions, this has been disabled and replaced by OAuth based authentication.

Step 2: Test using Postman or CURL

Here is an example of getting a Order in Magento by Id using Postman and Curl.

The endpoint to get Order Id 4 is <magento_website_url>/rest/default/V1/orders/4

Postman REST API call

To do the same from curl,

curl -X GET "<magento_webiste_url>/rest/default/V1/orders/4" -H "Authorization: Bearer <token>"

Replace <token> with the access token obtained in Step 1.

Get All Orders

To return all orders currently on the system we have to use the end point /rest/default/V1/orders with no search criterion

Endpoint URL: <magento_webiste_url>/rest/default/V1/orders/?searchCriteria=''
Magento Orders List REST API

Get Specific Fields in Orders

Here we are using end point /rest/default/V1/orders with fields increment_id and entity_id

Endpoint URL: <magento_webiste_url>/rest/default/V1/orders/?searchCriteria=''&fields=items[increment_id,entity_id]

Sample Response

{
	"items": [
		{
			"entity_id": 4,
			"increment_id": "000000004"
		},
		{
			"entity_id": 5,
			"increment_id": "000000005"
		}
	]
}

Another example where we want to get the increment_id, subtotal and line items.

Endpoint URL: <magento_webiste_url>/rest/default/V1/orders/?searchCriteria='&fields=items[increment_id,subtotal,items]

Sample Response

{
    "items": [
        {
            "increment_id": "000000004",
            "subtotal": 5.99,
            "items": [
                {
                    "amount_refunded": 0,
                   ……………………..
                    "name": "SAMPLE ITEM",
                    "no_discount": 0,
                    "order_id": 4,
                    "original_price": 5.99,
                    "price": 5.99,
                    "price_incl_tax": 5.99,
                    "product_id": 5,
                    ……………………….
                    "updated_at": "2022-09-06 09:29:34"
                }
            ]
        },
        {
            "increment_id": "000000005",
            "subtotal": 5.95,
            "items": [
                {
                    "amount_refunded": 0,
                   …………………………………….
                    "updated_at": "2022-09-12 11:18:34"
                }
            ]
        }
    ]
}

You can retrieve specific order fields and then apply certain transformations before you post it to third party systems. This can be done either using custom code or using an iPaas(Integration Platform as a Service).


Post a comment

Comments

Nothing yet..be the first to share wisdom.