# Examples

### Breakdown

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

```javascript
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/returns/breakdown' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "breakdown": {
        "by": "organization.id",
        "field": "return.id",
        "operator": "value_count",
        "sort": "desc",
        "limit": 5,
        "breakdown": {
            "by": "market.id",
            "field": "customer.id",
            "operator": "value_count",
            "sort": "asc",
            "limit": 10
        }
    },
    "filter": {
        "return": {
            "date_from": "2021-05-30T00:00:00Z",
            "date_to": "2022-05-30T23:59:00Z",
            "date_field": "created_at"
        },
        "market": {
          "names": {
            "in": ["USA"]
          }
        }
    }
}'
```

{% 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": {
    "organization.id": [
      {
        "label": "SWEETS67542",
        "value": 1
      }
    ]
  },
  "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 return resource:

```javascript
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/returns/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": "return.shipped_at",
        "field": "return.skus_count",
        "interval": "year",
        "operator": "stats",
        "breakdown": {
            "by": "origin_address.country_code",
            "field": "origin_address.state_code",
            "operator": "value_count",
            "sort": "desc",
            "limit": 5
        }
    },
    "filter": {
        "return": {
            "date_from": "2021-01-01T00:00:00Z",
            "date_to": "2021-12-30T23:59:00Z"
        },
        "market": {
          "names": {
            "in": ["USA"]
          }
        }
    }
}'
```

{% 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 return resource:

```javascript
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/returns/stats' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "stats": {
        "field": "return_line_items.quantity",
        "operator": "stats"
    },
    "filter": {
        "return": {
            "date_from": "2021-01-01T00:00:00Z",
            "date_to": "2021-12-31T23:59:00Z"
        },
        "market": {
          "names": {
            "in": ["USA"]
          }
        }
    }
}'
```

{% 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 return resource:

```javascript
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/returns/search' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "search": {
        "limit": 20,
        "sort": "asc",
        "sort_by": "return.created_at",
        "fields": ["return.id", "return.approved_at", "market.id", "return.status"]
        },
    "filter": {
        "return": {
            "date_from": "2022-01-01T18:36:01Z",
            "date_to": "2022-12-30T18:36:02Z"
        },
        "market": {
          "names": {
            "in": ["USA"]
          }
        }
    }
}'
```

{% 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",
      "approved_at": "2025-10-21T00:00:00.000Z",
      "market": {
        "id": "SWEETS67542"
      },
      "status": "SWEETS67542"
    }
  ],
  "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 return resource:

```shell
curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/returns/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": "return.created_at",
        "fields": [
          "return.id",
          "return.created_at",
          "return.reference"
        ]
    },
    "filter": {
        "return": {
            "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
return.id,return.created_at,return.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 %}
