> 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/metrics-api-reference/resources/carts/examples.md).

# Examples

How to perform Metrics API queries on the cart resource

### Breakdown

{% tabs %}
{% tab title="Request" %}
The following request performs a breakdown query on the cart resource:

```javascript
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/carts/breakdown' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "breakdown": {
        "by": "billing_address.city",
        "field": "order.total_amount_with_taxes",
        "operator": "stats",
        "sort": "desc",
        "limit": 10,
        "breakdown": {
            "by": "line_items.code",
            "field": "line_items.total_amount",
            "operator": "sum",
            "sort": "asc",
            "limit": 10
        }
    },
    "filter": {
        "order": {
            "date_from": "2021-02-28T00:00:00Z",
            "date_to": "2022-02-28T23:59:59Z",
            "date_field": "current_date"
        },
        "line_items" : {
            "types": { 
                "in": ["skus"]
             },
            "options": {
              "quantity": {
                "gte": 1
              }
            }
        }
    }
}'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `200 OK` status code, returning the aggregated values in the `data` object and extra information in the `meta` object:

```json
{
  "data": {
    "billing_address.city": [
      {
        "label": "199.0",
        "value": {
          "count": 1,
          "min": 199.0,
          "max": 199.0,
          "avg": 199.0,
          "sum": 199.0
        }
      }
    ]
  },
  "meta": {
    "type": "breakdown",
    "trace_id": "1ab75530-6c00-4140-bdb7-bf948e1fe7f7",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}
```

{% endtab %}
{% endtabs %}

### Date breakdown

{% tabs %}
{% tab title="Request" %}
The following request performs a date breakdown query on the cart resource:

```javascript
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/carts/date_breakdown' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "date_breakdown": {
      "by": "order.placed_at",
      "field": "order.total_amount_with_taxes",
      "operator": "stats",
      "interval": "year",
      "breakdown": {
        "by": "order.country_code",
        "field": "order.id",
        "operator": "value_count",
        "sort": "desc",
        "limit": 5
      }
    },
    "filter": {
      "order": {
        "date_from": "2021-01-01T00:00:00Z",
        "date_to": "2021-12-31T23:59:00Z"
      },
      "line_items": {
        "types": { 
          "in": ["skus"]
        }
      },
      "billing_address": {
        "localized": true,
        "business": false
      }
    }
  }'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `200 OK` status code, returning the aggregated values over the selected time intervals in the `data` object and extra information in the `meta` object:

```json
{
  "data": [
    {
      "date": "2021-01-01T00:00:00Z",
      "value": {
        "count": 1,
        "min": 199.0,
        "max": 199.0,
        "avg": 199.0,
        "sum": 199.0
      }
    }
  ],
  "meta": {
    "type": "date_breakdown",
    "trace_id": "1ab75530-6c00-4140-bdb7-bf948e1fe7f7",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}
```

{% endtab %}
{% endtabs %}

### Stats

{% tabs %}
{% tab title="Request" %}
The following request performs a stats query on the cart resource:

```javascript
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/carts/stats' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "stats": {
      "field": "order.total_amount",
      "operator": "stats"
    },
    "filter": {
      "order": {
        "date_from": "2021-01-01T00:00:00Z",
        "date_to": "2021-12-31T23:59:00Z",
        "date_field": "approved_at",
        "total_amount": {
          "gte": 1000
        }
      },
      "line_items": {
        "types": { 
          "in": ["skus"]
        },
        "options": {
          "quantity": {
            "gte": 1
          }
        }
      }
    }
  }'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `200 OK` status code, returning the computed value in the `data` object and additional information in the `meta` object:

```json
{
  "data": {
    "value": {
      "count": 1,
      "min": 199.0,
      "max": 199.0,
      "avg": 199.0,
      "sum": 199.0
    }
  },
  "meta": {
    "type": "stats",
    "trace_id": "1ab75530-6c00-4140-bdb7-bf948e1fe7f7",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}
```

{% endtab %}
{% endtabs %}

### Search

{% tabs %}
{% tab title="Request" %}
The following request performs a search query on the cart resource:

```javascript
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/carts/search' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "search": {
      "limit": 5,
      "sort": "desc",
      "sort_by": "order.created_at",
      "fields": [
        "order.id",
        "order.number",
        "line_items.item_type",
        "line_items.quantity"
      ]
    },
    "filter": {
      "order": {
        "date_from": "2021-01-01T00:00:00Z",
        "date_to": "2021-12-31T23:59:00Z"
      },
      "billing_address": {
        "localized": true,
        "business": false
      }
    }
  }'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `200 OK` status code, returning the requested fields in the `data` object and additional information alongside pagination in the `meta` object:

```json
{
  "data": [
    {
      "id": "SWEETS67542",
      "number": 1,
      "line_items": [
        {
          "item_type": "SWEETS67542",
          "quantity": 1
        }
      ]
    }
  ],
  "meta": {
    "type": "search",
    "trace_id": "1ab75530-6c00-4140-bdb7-bf948e1fe7f7",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ],
    "pagination": {
      "record_count": 1,
      "cursor": null
    }
  }
}
```

{% endtab %}
{% endtabs %}

### Export

{% tabs %}
{% tab title="Request" %}
The following request performs an export query on the cart resource:

```shell
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/carts/export' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "search": {
        "limit": 100,
        "sort": "desc",
        "sort_by": "order.created_at",
        "fields": [
          "order.id",
          "order.created_at",
          "order.reference"
        ]
    },
    "filter": {
        "order": {
            "date_from": "2026-01-01T00:00:00Z",
            "date_to": "2026-12-31T23:59:00Z",
            "date_field": "updated_at"
        }
    }
}'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `200 OK` status code, returning inline the requested data in a plain CSV (text/csv) format:

```csv
order.id,order.created_at,order.reference
NpyAhQVaBM,2026-01-02T14:22:34.062Z,test
PWYOheJpGg,2026-01-02T14:22:03.474Z,external
wxzYhpeLAn,2026-01-02T14:10:18.592Z,external_2
NlalhbzOZb,2026-01-02T14:09:24.316Z,void
qQgYhrvjDX,2026-01-01T06:02:42.349Z,test
```

{% endtab %}
{% endtabs %}


---

# 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/metrics-api-reference/resources/carts/examples.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.
