Examples
How to perform Metrics API queries on the cart resource
Breakdown
The following request performs a breakdown query on the cart resource:
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
}
}
}
}
}'
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": []
},
"meta": {
"type": "breakdown",
"trace_id": "d864cbc9-6596-4a0c-baab-e659eb85c9db",
"mode": "test",
"organization_id": "mzRmbZFPyO",
"market_ids": [
"AjRevhvqvo",
"dlQbPhqGro"
]
}
}
Date breakdown
The following request performs a date breakdown query on the cart resource:
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.created_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"]
}
},
"billing_address": {
"localized": true,
"business": false
}
}
}'
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": "2022-01-01T00:00:00.000Z",
"value": {
"count": 0,
"min": null,
"max": null,
"avg": null,
"sum": 0.0
},
"order.country_code": []
}
],
"meta": {
"type": "date_breakdown",
"trace_id": "2c3d77cb-352b-4b53-8dd0-31ca2998a051",
"mode": "test",
"organization_id": "mzRmbZFPyO",
"market_ids": [
"AjRevhvqvo",
"dlQbPhqGro"
]
}
}
Stats
The following request performs a stats query on the cart resource:
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": "2022-01-01T00:00:00Z",
"date_to": "2022-12-31T23:59:00Z",
"date_field": "created_at",
"total_amount": {
"gte": 1000
}
},
"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": 0,
"min": null,
"max": null,
"avg": null,
"sum": 0.0
}
},
"meta": {
"type": "stats",
"trace_id": "9aaa67f3-6dad-4cae-8ace-faca03f41254",
"mode": "test",
"organization_id": "mzRmbZFPyO",
"market_ids": [
"AjRevhvqvo",
"dlQbPhqGro"
]
}
}
Search
The following request performs a search query on the cart resource:
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": "2022-01-01T00:00:00Z",
"date_to": "2022-12-31T23:59:00Z"
},
"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": [],
"meta": {
"pagination": {
"record_count": 0,
"cursor": null
},
"type": "search",
"trace_id": "fa81a1e1-e9f4-41d0-bcec-69b407150f2c",
"mode": "test",
"organization_id": "mzRmbZFPyO",
"market_ids": [
"AjRevhvqvo",
"dlQbPhqGro"
]
}
}
Last updated