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

Examples

How to perform Metrics API queries on the order resource

Breakdown

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

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/orders/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"
        },
        "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": {
    "billing_address.city": [
      {
        "label": "HtOxKL",
        "value": {
          "count": 1,
          "min": 190.9,
          "max": 190.9,
          "avg": 190.9,
          "sum": 190.9
        },
        "line_items.code": [
          {
            "label": "BABYONBU000000E63E7412MX",
            "value": 79.5
          },
          {
            "label": "BABYONBUFFFFFF00000012MX",
            "value": 104.4
          }
        ]
      },
      {
        "label": "RqBweQ",
        "value": {
          "count": 1,
          "min": 153.4,
          "max": 153.4,
          "avg": 153.4,
          "sum": 153.4
        },
        "line_items.code": [
          {
            "label": "SWEETSMU000000FFFFFFMXXX",
            "value": 42.0
          },
          {
            "label": "BABYONBU000000E63E74NBXX",
            "value": 104.4
          }
        ]
      }
    ]
  },
  "meta": {
    "type": "breakdown",
    "trace_id": "1ab75530-6c00-4140-bdb7-bf948e1fe7f7",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}

Date breakdown

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

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/orders/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": "2022-01-01T00:00:00Z",
        "date_to": "2022-12-31T23:59:00Z"
      },
      "line_items": {
          "types": { "in": ["skus"]}
      }
    }
}'

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": 1,
        "min": 153.4,
        "max": 153.4,
        "avg": 153.4,
        "sum": 153.4
      },
      "order.country_code": [
        {
          "label": "US",
          "value": 1
        }
      ]
    },
    {
      "date": "2022-01-01T00:00:00.000Z",
      "value": {
        "count": 2,
        "min": 190.9,
        "max": 4980.0,
        "avg": 2585.45,
        "sum": 5170.9
      },
      "order.country_code": [
        {
          "label": "IT",
          "value": 1
        },
        {
          "label": "SE",
          "value": 1
        }
      ]
    }
  ],
  "meta": {
    "type": "date_breakdown",
    "trace_id": "b62d648b-5a39-417b-a62a-b8626e88fafa",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}

Stats

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

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

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": 1,
      "min": 0.0,
      "max": 0.0,
      "avg": 0.0,
      "sum": 0.0
    }
  },
  "meta": {
    "type": "stats",
    "trace_id": "9d7b9778-95a3-41cd-bd36-4fd5555eab62",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}

Search

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

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/orders/search' \
  -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.number",
          "line_items.item_type",
          "line_items.quantity"
        ]
    },
    "filter": {
        "order": {
            "date_from": "2022-01-01T00:00:00Z",
            "date_to": "2022-12-31T23:59:00Z",
            "date_field": "updated_at"
        },
        "billing_address": {
          "localized": true,
          "business": false
        }
    }
}'

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": "wOBnhmmDOB",
      "number": "17913643",
      "line_items": [
        {
          "item_type": "payment_methods",
          "quantity": 1
        },
        {
          "item_type": "shipments",
          "quantity": 1
        },
        {
          "item_type": "skus",
          "quantity": 2
        }
      ]
    },
    {
      "id": "PEBrhoMKWG",
      "number": "17464241",
      "line_items": [
        {
          "item_type": "shipments",
          "quantity": 1
        },
        {
          "item_type": "payment_methods",
          "quantity": 1
        },
        {
          "item_type": "skus",
          "quantity": 3
        },
        {
          "item_type": "skus",
          "quantity": 2
        }
      ]
    },
    {
      "id": "NlalhKyjjp",
      "number": "14501974",
      "line_items": [
        {
          "item_type": "payment_methods",
          "quantity": 1
        },
        {
          "item_type": "shipments",
          "quantity": 1
        },
        {
          "item_type": "skus",
          "quantity": 1
        },
        {
          "item_type": "skus",
          "quantity": 3
        }
      ]
    },
    {
      "id": "wAezheQkka",
      "number": "14501970",
      "line_items": [
        {
          "item_type": "payment_methods",
          "quantity": 1
        },
        {
          "item_type": "shipments",
          "quantity": 1
        },
        {
          "item_type": "bundles",
          "quantity": 1
        },
        {
          "item_type": "bundles",
          "quantity": 1
        }
      ]
    }
  ],
  "meta": {
    "pagination": {
      "record_count": 4,
      "cursor": null
    },
    "type": "search",
    "trace_id": "2998cf1a-578b-4f17-99e3-601d8570aefb",
    "mode": "test",
    "organization_id": "mzRmbZFPyO",
    "market_ids": [
      "AjRevhvqvo",
      "dlQbPhqGro"
    ]
  }
}
PreviousFiltersNextReturns

Last updated 1 month ago