Enabled resource errors field for orders and carts —
Check the use case
LogoLogo
Metrics APIOther APIsChangelog
API reference
API reference
  • Introduction
  • Resources
    • Orders
      • Breakdown
      • Date breakdown
      • Stats
      • Search
      • Filters
      • Examples
    • Returns
      • Breakdown
      • Date breakdown
      • Stats
      • Search
      • Filters
      • Examples
    • Carts
      • Breakdown
      • Date breakdown
      • Stats
      • Search
      • Filters
      • Examples
  • Getting started
    • API specification
    • Queries
    • Filters
    • Analysis
    • Errors
    • Use cases
On this page
  • Breakdown
  • Date breakdown
  • Stats
  • Search
  1. Resources
  2. Returns

Examples

How to perform Metrics API queries on the return resource

Breakdown

The following request performs a breakdown query on the return resource:

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"]
          }
        }
    }
}'

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:

{
  "data": {
    "organization.id": [
      {
        "label": "jXbGbFKEBn",
        "value": 2,
        "market.id": [
          {
            "label": "AjRevhvqvo",
            "value": 2
          }
        ]
      },
      {
        "label": "JXgWZFlAZX",
        "value": 1,
        "market.id": [
          {
            "label": "AjRevhvqvo",
            "value": 1
          }
        ]
      },
      {
        "label": "mzRmbZFPyO",
        "value": 1,
        "market.id": [
          {
            "label": "AjRevhvqvo",
            "value": 1
          }
        ]
      }
    ]
  },
  "meta": {
    "type": "breakdown",
    "trace_id": "b9f77ec2-fcf5-450a-8a6f-46a372e41587",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}

Date breakdown

The following request performs a date breakdown query on the return resource:

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"]
          }
        }
    }
}'

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:

{
  "data": [
    {
      "date": "2021-01-01T00:00:00.000Z",
      "value": {
        "count": 2,
        "min": 1.0,
        "max": 1.0,
        "avg": 1.0,
        "sum": 2.0
      },
      "origin_address.country_code": [
        {
          "label": "IT",
          "value": 2
        }
      ]
    }
  ],
  "meta": {
    "type": "date_breakdown",
    "trace_id": "5ceabc44-7470-4b8c-aa95-cbddd3c71b77",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}

Stats

The following request performs a stats query on the return resource:

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"]
          }
        }
    }
}'

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:

{
  "data": {
    "value": {
      "count": 0,
      "min": null,
      "max": null,
      "avg": null,
      "sum": 0.0
    }
  },
  "meta": {
    "type": "stats",
    "trace_id": "baa49d56-3437-4675-bf31-bedebe0051c1",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}

Search

The following request performs a search query on the return resource:

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"]
          }
        }
    }
}'

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:

{
  "data": [
    {
      "id": "nWYGQubnEr",
      "approved_at": "2022-01-11T18:31:37.305Z",
      "status": "received",
      "market": {
        "id": "AjRevhvqvo"
      }
    },
    {
      "id": "QZpXvupjZY",
      "approved_at": "2022-01-12T15:27:34.435Z",
      "status": "received",
      "market": {
        "id": "AjRevhvqvo"
      }
    }
  ],
  "meta": {
    "pagination": {
      "record_count": 2,
      "cursor": null
    },
    "type": "search",
    "trace_id": "8bd2ec0a-54cf-4138-8118-82ff88ff3f2d",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}
PreviousFiltersNextCarts

Last updated 1 month ago