Search…
API Reference
Sorting results
How to request a specific sort for the results of a collection of resources
When you fetch a collection of resources, you can request a specific sort for the results, using the sort query parameter.
The value of the sort parameter must be a comma-separated list of fields. Pay attention to avoid whitespaces before or after each comma.
The sort order for each field is ascending unless prefixed with a - (minus) in which case it's descending.

Example

Request
Response
The following request fetches a collection of SKUs sorted by their creation date (descending) and code:
1
curl -g -X GET \
2
'https://yourdomain.commercelayer.io/api/skus?sort=-created_at,code' \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token'
Copied!
On success, the API responds with a 200 OK status code, returning a paginated collection** **of the resource objects, sorted in the requested order:
1
{
2
"data": [
3
{
4
"id": "xYZkjABcde",
5
"type": "skus",
6
"links": {...},
7
"attributes": {
8
"code": "TSHIRTMM000000FFFFFFXLXX",
9
"name": "Black Men T-shirt with White Logo (XL)",
10
"description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
11
"image_url": "https://img.yourdomain.com/skus/xYZkjABcde.png",
12
"tag_names": "Men, Black, XL",
13
"pieces_per_pack": 6,
14
"weight": 300.0,
15
"unit_of_weight": "gr",
16
"hs_tariff_number": null,
17
"do_not_ship": false,
18
"do_not_track": false,
19
"created_at": "2018-01-01T12:00:00.000Z",
20
"updated_at": "2018-01-01T12:00:00.000Z",
21
"reference": "ANYREFEFERNCE",
22
"metadata": {
23
"foo": "bar"
24
}
25
},
26
"relationships": {
27
"shipping_category": {
28
"links": {...}
29
},
30
"prices": {
31
"links": {...}
32
},
33
"stock_items": {
34
"links": {...}
35
},
36
"delivery_lead_times": {
37
"links": {...}
38
},
39
"sku_options": {
40
"links": {...}
41
}
42
},
43
"meta": {
44
"mode": "test"
45
}
46
},
47
{
48
"other": "... 9 skus (first page)"
49
}
50
],
51
"meta": {
52
"record_count": 140,
53
"page_count": 14
54
},
55
"links": {
56
"first": "https://yourdomain.commercelayer.io/api/skus?sort=-created_at,code&page[number]=1&page[size]=10",
57
"next": "https://yourdomain.commercelayer.io/api/skus?sort=-created_at,code&page[number]=2&page[size]=10",
58
"last": "https://yourdomain.commercelayer.io/api/skus?sort=-created_at,code&page[number]=14&page[size]=10"
59
}
60
}
Copied!
You can get the full list of sortable attributes from the documentation of each resource.
Last modified 1mo ago
Copy link
Contents
Example