> For the complete documentation index, see [llms.txt](https://docs.commercelayer.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.commercelayer.io/core/including-associations.md).

# 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.

{% hint style="warning" %}
The value of the `include` parameter must be a comma-separated list of relationship paths ([see example](#fetching-an-sku-and-some-of-its-associations)) — make sure to avoid whitespaces before or after each comma. A relationship path is a dot-separated list of relationship names ([see example](#using-relationship-paths)). For each included resource you'll find in the `relationships` object of the response an additional `data` array containing their type and ID.
{% endhint %}

{% hint style="info" %}
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](/core/filtering-data.md#filtering-related-resources)).
{% endhint %}

### Examples

#### Fetching an SKU and some of its associations

{% tabs %}
{% tab title="Request" %}
The following request fetches the SKU identified by the ID "xYZkjABcde" and the related prices and stock items:

```sh
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'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `200 OK` status code, returning the resource object and the included associations:

<pre class="language-json"><code class="lang-json">{
  "data": {
    "id": "xYZkjABcde",
    "type": "skus",
    "links": { ... },
    "attributes": { ... },
    "relationships": {
      "shipping_category": { ... },
<strong>      "prices": {
</strong>        "links": { .. },
<strong>        "data": [
</strong><strong>          {
</strong><strong>            "type": "prices",
</strong><strong>            "id": "pxEWUBWXLA"
</strong><strong>          }
</strong><strong>        ]
</strong>      },
<strong>      "stock_items": {
</strong>        "links": { ... },
<strong>        "data": [
</strong><strong>          {
</strong><strong>            "type": "stock_items",
</strong><strong>            "id": "jAdEfdlvBK"
</strong><strong>          },
</strong><strong>          {
</strong><strong>            "type": "stock_items",
</strong><strong>            "id": "jpQRfJlrwN"
</strong><strong>          }
</strong><strong>        ]
</strong>      },
      "stock_reservations": { ... },
      "delivery_lead_times": { ... },
      "sku_options": { ... },
      "attachments": { ... },
      "events": { ... },
      "tags": { ... }
    },
    "meta": {
      "created_with_version": "2017-08"
    }
  },
  "included": [
    {
<strong>      "id": "pxEWUBWXLA",
</strong><strong>      "type": "prices",
</strong>      "links": { ... },
      "attributes": {
        "currency_code": "USD",
        "sku_code": "TSHIRTMM000000FFFFFFMXXX",
        "amount_cents": 3480,
        "amount_float": 34.8,
        "formatted_amount": "$34.80",
        "compare_at_amount_cents": 4524,
        "compare_at_amount_float": 45.24,
        "formatted_compare_at_amount": "$45.24",
        "created_at": "2019-05-14T10:36:53.987Z",
        "updated_at": "2019-05-14T10:36:53.987Z",
        "reference": null,
        "reference_origin": null,
        "metadata": {}
      },
      "relationships": {
        "price_list": { ... },
        "sku": { ... },
        "price_tiers": { ... },
        "price_volume_tiers": { ... },
        "price_frequency_tiers": { ... },
        "attachments": { ... }
      },
      "meta": {
        "created_with_version": "2017-08"
      }
    },
    {
<strong>      "id": "jAdEfdlvBK",
</strong><strong>      "type": "stock_items",
</strong>      "links": { ... },
      "attributes": {
        "sku_code": "TSHIRTMM000000FFFFFFMXXX",
        "quantity": 76,
        "created_at": "2019-05-14T10:36:53.980Z",
        "updated_at": "2019-05-14T10:36:53.980Z",
        "reference": null,
        "reference_origin": null,
        "metadata": {}
      },
      "relationships": {
        "stock_location": { ... },
        "sku": { ... },
        "reserved_stock": { ... },
        "stock_reservations": { ... },
        "attachments": { ... }
      },
      "meta": {
        "created_with_version": "2017-08"
      }
    },
    {
<strong>      "id": "jpQRfJlrwN",
</strong><strong>      "type": "stock_items",
</strong>      "links": { ... },
      "attributes": {
        "sku_code": "TSHIRTMM000000FFFFFFMXXX",
        "quantity": 28,
        "created_at": "2019-05-14T10:36:53.993Z",
        "updated_at": "2019-05-14T10:36:53.993Z",
        "reference": null,
        "reference_origin": null,
        "metadata": {}
      },
      "relationships": {
        "stock_location": { ... },
        "sku": { ... },
        "reserved_stock": { ... },
        "stock_reservations": { ... },
        "attachments": { ... }
      },
      "meta": {
        "created_with_version": "2017-08"
      }
    }
  ],
  "meta": {
    "api_version": "2017-08",
    "api_versions": [ "2017-08", "2026-05" ],
    "mode": "test",
    "organization_id": "EnAvaFOrRe",
    "trace_id": "e7c3f9a1d5b8e2c6f0a4d7c1f5b9e3d7a1c5f8b2e6d0a4c8f3b1e5d9a2c6f0b7"
  }
}
</code></pre>

{% endtab %}
{% endtabs %}

#### Using relationship paths

{% tabs %}
{% tab title="Request" %}
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:

```bash
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'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `200 OK` status code, returning the resource object and the included associations:

<pre class="language-json"><code class="lang-json">{
  "data": {
    "id": "xYZkjABcde",
    "type": "skus",
    "links": { ... },
    "attributes": { ... },
    "relationships": {
      "shipping_category": { ... },
      "prices": { ... },
<strong>      "stock_items": {
</strong>        "links": { ... },
<strong>        "data": [
</strong><strong>          {
</strong><strong>            "type": "stock_items",
</strong><strong>            "id": "jAdEfdlvBK"
</strong><strong>          },
</strong><strong>          {
</strong><strong>            "type": "stock_items",
</strong><strong>            "id": "jpQRfJlrwN"
</strong><strong>          }
</strong><strong>        ]
</strong>      },
      "stock_reservations": { ... },
      "delivery_lead_times": { ... },
      "sku_options": { ... },
      "attachments": { ... },
      "events": { ... },
      "tags": { ... }
    },
    "meta": {
      "created_with_version": "2017-08"
    }
  },
  "included": [
    {
<strong>      "id": "jAdEfdlvBK",
</strong><strong>      "type": "stock_items",
</strong>      "links": { ... },
      "attributes": {
        "sku_code": "TSHIRTMM000000FFFFFFMXXX",
        "quantity": 76,
        "created_at": "2019-05-14T10:36:53.980Z",
        "updated_at": "2019-05-14T10:36:53.980Z",
        "reference": null,
        "reference_origin": null,
        "metadata": {}
      },
      "relationships": {
        "stock_location": { ... },
        "sku": { ... },
<strong>        "reserved_stock": {
</strong>          "links": { ... },
<strong>          "data": null
</strong>        },
        "stock_reservations": { ... },
        "attachments": { ... }
      },
      "meta": {
        "created_with_version": "2017-08"
      }
    },
    {
<strong>      "id": "jpQRfJlrwN",
</strong><strong>      "type": "stock_items",
</strong>      "links": { ... },
      "attributes": {
        "sku_code": "TSHIRTMM000000FFFFFFMXXX",
        "quantity": 28,
        "created_at": "2019-05-14T10:36:53.993Z",
        "updated_at": "2019-05-14T10:36:53.993Z",
        "reference": null,
        "reference_origin": null,
        "metadata": {}
      },
      "relationships": {
        "stock_location": { ... },
        "sku": { ... },
<strong>        "reserved_stock": {
</strong>          "links": { ... },
<strong>          "data": {
</strong><strong>            "type": "reserved_stocks",
</strong><strong>            "id": "noJnMSBLXo"
</strong><strong>          }
</strong>        },
        "stock_reservations": { ... },
        "attachments": { ... }
      },
      "meta": {
        "created_with_version": "2017-08"
      }
    },
    {
<strong>      "id": "noJnMSBLXo",
</strong><strong>      "type": "reserved_stocks",
</strong>      "links": { ... },
      "attributes": {
<strong>        "quantity": 5,
</strong>        "created_at": "2023-07-17T10:29:13.433Z",
        "updated_at": "2023-07-17T10:29:13.441Z",
        "reference": null,
        "reference_origin": null,
        "metadata": {}
      },
      "relationships": {
        "stock_item": { ... },
        "sku": { ... },
        "stock_reservations": { ... }
      },
      "meta": {
        "created_with_version": "2017-08"
      }
    }
  ],
  "meta": {
    "api_version": "2017-08",
    "api_versions": [ "2017-08", "2026-05" ],
    "mode": "test",
    "organization_id": "EnAvaFOrRe",
    "trace_id": "3d9c7f1a5b4e8d2c0f6a3b7e1d5c9f2a8b0e4d6c3f7a1b5e9d2c4f0a6b8e3d1c"
  }
}
</code></pre>

{% endtab %}
{% endtabs %}

{% hint style="info" %}
You can request to include associations also when [creating](/core/creating-resources.md) or [updating](/core/updating-resources.md) resources.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.commercelayer.io/core/including-associations.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
