Retrieve filtered responses for REST APIs
Some REST calls return dozens or even hundreds of parameters, and parsing through all this data can be unwieldy. In addition, mobile app developers might find the bandwidth needed to process a request to be excessive. To resolve these problems, Magento provides a query parameter-based syntax for REST API requests that return partial responses.
This feature is not available for SOAP, because SOAP does not allow partial responses.
You can append ?fields=<field_or_object1>,<field_or_object2>,...
to any GET, POST, or PUT operation to filter unimportant information from the response. <field_or_object>
can be any of the following:
- Simple top-level field
- Top-level object that includes all fields
- Top-level object with selected fields
- Nested objects
Separate each field or object with a comma.
On POST and PUT requests, Magento ignores the fields
parameter as input, but the response includes only the requested fields and objects.
Examples
All examples use Magento Open Source sample data.
Simple fields
The following example returns only the sku
, price
, and name
for the specified product:
GET http://<host>/rest/default/V1/products/24-MB01?fields=sku,price,name
Simple fields and top-level objects with all fields
The following example returns only the customer first name, last name, and the entire billing_address
object from a specified order. Do not include brackets []
after an object name when you want to return all of the object’s contents.
GET http:/<host>/rest/default/V1/orders/2?fields=billing_address,customer_firstname,customer_lastname
Top-level object with selected fields
The following example returns only the name
, qty
, and sku
fields defined in an items
object from a specified shipment:
GET http://<host>/rest/default/V1/shipment/2?fields=items[name,qty,sku]
Nested objects
This example returns only the following:
- The product’s
sku
andname
- The entire
category_links
object, which is defined inextension_attributes
- The
item_id
andqty
fields of thestock_item
object, which is also defined inextension_attributes
GET http://<host>/rest/default/V1/products/MT12?fields=name,sku,extension_attributes[category_links,stock_item[item_id,qty]]
POST operation
The following POST operation and payload creates a catalog category named New Category
. Magento returns only the id
, parent_id
, and name
attributes
POST http://<host>/rest/V1/categories?fields=id,parent_id,name
Using with searchCriteria
The searchCriteria
query parameter allows you to search across multiple objects in a collection. You can use the fields
query parameter in conjunction with searchCriteria
to limit the output. The question mark (?) that precedes fields
in all the other examples in this document is replaced with an ampersand (&).
The following query returns only the sku
and name
parameters for product items whose category_gear
attribute includes the value 86
.
GET http://<host>/rest/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]