Including associations

How to include a resource and its associations in the same request

When you fetch a resource or a collection, you can include its associations in the same request, using the include query parameter. This reduces the number of roundtrips, optimizing the performances.

The value of the include parameter must be a comma-separated list of relationship paths (see example) — make sure to avoid whitespaces before or after each comma. A relationship path is a dot-separated list of relationship names (see example). For each included resource you'll find in the relationships object of the response an additional data array containing their type and ID.

Included resources cannot be sorted or filtered. Sort rules and filters apply to the parent resource (e.g. skus in the examples below) or to related resources (see examples).

Examples

Fetching an SKU and some of its associations

The following request fetches the SKU identified by the ID "xYZkjABcde" and the related prices and stock items:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus/xYZkjABcde?include=prices,stock_items' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

Using relationship paths

The following request features the relationship path stock_items.reserved_stock as the value of the include parameter, where stock_items is a relationship listed under the SKU resource object, and reserved_stock is a relationship listed under the stock item resource object:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus/xYZkjABcde?include=stock_items.reserved_stock' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

You can request to include associations also when creating or updating resources.

Last updated