Sparse fieldsets

How to request the API to return only specific fields

When you fetch a resource or collection, you can request the API to return only specific fields, using the fields query parameter. This reduces the response payload, optimizing the performances. If you want the API to return also specific related resources data, they must be included and added to the fields list as well.

The value of the fields parameter must be a comma-separated list that refers to the name(s) of the fields to be returned. Pay attention to avoid whitespaces before or after each comma.

Examples

Requesting specific attributes only

The following request fetches an SKU code and name:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus/xYZkjABcde?fields[skus]=code,name' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

The following request fetches an SKU code, name, and related prices plus the formatted amount and the related price lists of each price:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus/xYZkjABcde?include=prices,prices.price_list&fields[skus]=code,name,prices&fields[prices]=formatted_amount,price_list' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

You can request sparse fieldsets also when creating or updating resources.

Last updated