Magento 2 API Get All Products, Orders, Categories

In Magento 2, you can call API to get all products with ease. Several REST calls return thousands or even hundreds of parameters. And parsing through all this data can sometimes be cumbersome.

Moreover, mobile app developers may find the bandwidth is necessary to process an unreasonable request. To handle these issues, a query parameter-based syntax for REST requests is provided, that returns partial responses.

<host>/rest/<store_code> can be replaced by your website.

The searchCriteria query parameter enables you to search across multiple objects which are in a collection. The fields query parameter can be used in conjunction with searchCriteria to limit the output. The question mark (?) which precedes fields in all examples in this document will be replaced with an ampersand (&).

Get All Products, Orders, Categories

Get All Products

Only the sku and name parameters for product items whose category_gear attribute includes the value 86 are returned by the following query.

GET <host>/rest/<store_code>/V1/products/?searchCriteria[filter_groups][0][filters][0][field]=category_gear&searchCriteria[filter_groups][0][filters][0][value]=86&searchCriteria[filter_groups][0][filters][0][condition_type]=finset&fields=items[sku,name]

{
"items":
  {
    "sku": "24-MG04"
    "name": "Aim Analog Watch"
  }
  {
    "sku": "24-MG01"
    "name": "Endurance Watch"
  }
  {
    "sku": "24-MG03"
    "name": "Summit Watch"
  }
  {
    "sku": "24-MG05"
    "name": "Cruise Dual Analog Watch"
  }
  {
    "sku": "24-MG02"
    "name": "Dash Digital Watch"
  }
  {
    "sku": "24-WG09"
    "name": "Luma Analog Watch"
  }
  {
    "sku": "24-WG01"
    "name": "Bolo Sport Watch"
  }
  {
    "sku": "24-WG03"
      "name": "Clamber Watch"
  }
  {
    "sku": "24-WG02"
    "name": "Didi Sport Watch"
  }
}

Limit 20 items per page: /V1/products?searchCriteria[pageSize]=20

You can view more about catalog API in here

Get Products with Categories

GET <host>/rest/<store_code>/V1/products/MT12?fields=name,sku,extension_attributes[category_links,stock_item[item_id,qty]]

{
  "sku": "MT12"
  "name": "Cassius Sparring Tank"
  "extension_attributes": {
    "category_links": {
      "position": 1
      "category_id": "18"
    }
    "stock_item": {
      "item_id": 732
      "qty": 0
      }
  }
}

The above example returns the following results:

  • The sku and name of the product
  • The entire category_links object. This is defined in extension_attributes
  • The stock_item object’s item_id and qty fields. These are also defined in extension_attributes

Get All Orders

GET <host>/rest/<store_code>/V1/orders/2?fields=billing_address,customer_firstname,customer_lastname

{
  "customer_firstname": "Veronica"
  "customer_lastname": "Costello"
  "billing_address": {
    "address_type": "billing"
    "city": "Calder"
    "country_id": "US"
    "customer_address_id": 1
    "email": "roni_cost@example.com"
    "entity_id": 4
    "firstname": "Veronica"
    "lastname": "Costello"
    "parent_id": 2
    "postcode": "49628-7978"
    "region": "Michigan"
    "region_code": "MI"
    "region_id": 33
    "street": "6146 Honey Bluff Parkway"
    "telephone": "(555) 229-3326"
    }
  }

The above example returns the first name and last name of customer as well as the entire billing_address object from a specified order.

When you want all contents of the object to be returned, do not add brackets [] after an object name.

Get Categories

POST <host>/rest/<store_code>/V1/categories?fields=id,parent_id,name

Payload

{
  "category": {
    "name": "New Category",
    "is_active": true
  }
}

Response

{
  "id": 43,
  "parent_id": 2,
  "name": "New Category"
}

The above POST operation and payload is for creating a catalog category named New Category. In here only the id, parent_id, and name attributes are returned.

Conclusion

In today post, I have just guided you on how to call API to get All Products, Orders, and Categories in Magento 2. I hope it would be helpful. In case you have any questions or new ideas, feel free to leave a comment below.

Enjoyed the tutorial? Spread it to your friends!