For the complete documentation index, see llms.txt. This page is also available as Markdown.

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 sort order for each field is ascending unless prefixed with a - (minus) in which case it's descending.

Sortable fields

Not all resource attributes can be used as sorting parameters. You can get the full list of the sortable attributes from the documentation of each resource in the "List all" section (e.g. sortable fields for SKUs).

Please note that if you try to sort a collection of resources by a non-sortable attribute the API which will respond with a 400 Bad Request status code, due to invalid sort criteria.

Examples

Sorting the parent resource by one of its attribute

The following request fetches a collection of SKUs sorted alphabetically by their code:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus?sort=code' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

On success, the API responds with a 200 OK status code, returning a paginated collection of the resource objects, sorted in the requested order:

{
  "data": [
    {
      "id": "yzXKjYzaCx",
      "type": "skus",
      "links": {...},
      "attributes": {
        "code": "BABYONBU000000E63E7412MX",
        "name": "Baby's Black Onesie Short Sleeve with Pink Logo (12 Months)",
        "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
        "image_url": "https://img.yourdomain.com/skus/yzXKjYzaCx.png",
        "pieces_per_pack": 6,
        "weight": 300.0,
        "unit_of_weight": "gr",
        "hs_tariff_number": null,
        "do_not_ship": false,
        "do_not_track": false,
        "created_at": "2018-01-01T12:00:00.000Z",
        "updated_at": "2018-01-01T12:00:00.000Z",
        "reference": "ANYREFEFERNCE",
        "metadata": {
          "foo": "bar"
        }
      },
      "relationships": {
        "shipping_category": {
          "links": {...}
        },
        "prices": {
          "links": {...}
        },
        "stock_items": {
          "links": {...}
        },
        "stock_reservations": {
          "links": {...}
        },
        "delivery_lead_times": {
          "links": {...}
        },
        "sku_options": {
          "links": {...}
        },
        { ... }
      },
      "meta": {
        "mode": "test",
        "organization_id": "JxYabZKcAw",
        "trace_id": "a4d8f2b6e0c9a5d1f7b3e8c4a9d3f5b1e6c2a7d0f4b9e5c1a8d2f6b0e7c3a5d9"
      }
    },
    {
      "other": "... 9 skus (first page)"
    }
  ],
  "meta": {
    "record_count": 140,
    "page_count": 14,
    "page_number": 1,
    "page_size": 10,
    "mode": "test",
    "organization_id": "JxYabZKcAw",
    "trace_id": "a4d8f2b6e0c9a5d1f7b3e8c4a9d3f5b1e6c2a7d0f4b9e5c1a8d2f6b0e7c3a5d9"
  },
  "links": {
    "first": "https://yourdomain.commercelayer.io/api/skus?sort=code&page[number]=1&page[size]=10",
    "next": "https://yourdomain.commercelayer.io/api/skus?sort=code&page[number]=2&page[size]=10",
    "last": "https://yourdomain.commercelayer.io/api/skus?sort=code&page[number]=14&page[size]=10"
  }
}
Pagination

The following request fetches a collection of SKUs sorted alphabetically by the name of the associated shipping category:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus?sort=shipping_category.name' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

On success, the API responds with a 200 OK status code, returning a paginated collection of the resource objects, sorted in the requested order:

{
  "data": [
    {
      "id": "WFrbSXqyoZ",
      "type": "skus",
      "links": {...},
      "attributes": {
        "code": "TSHIRTMM000000FFFFFFXLXX",
        "name": "Men's Black T-shirt with White Logo (XL)",
        "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
        "image_url": "https://img.yourdomain.com/skus/WFrbSXqyoZ.png",
        "pieces_per_pack": 3,
        "weight": 350.0,
        "unit_of_weight": "gr",
        "hs_tariff_number": null,
        "do_not_ship": false,
        "do_not_track": false,
        "created_at": "2018-01-01T12:00:00.000Z",
        "updated_at": "2018-01-01T12:00:00.000Z",
        "reference": "ANYREFEFERNCE",
        "metadata": {
          "foo": "bar"
        }
      },
      "relationships": {
        "shipping_category": {
          "links": {...}
        },
        "prices": {
          "links": {...}
        },
        "stock_items": {
          "links": {...}
        },
        "stock_reservations": {
          "links": {...}
        },
        "delivery_lead_times": {
          "links": {...}
        },
        "sku_options": {
          "links": {...}
        },
        { ... }
      },
      "meta": {
        "mode": "test",
        "organization_id": "JxYabZKcAw",
        "trace_id": "3b7f1d9a5c8e2b6f0d4a7c1f5b9d3e7a4c8f2b0d6e3a9c5f1b7d0e4a2c6f8b3d"
      }
    },
    {
      "other": "... 9 skus (first page)"
    }
  ],
  "meta": {
    "record_count": 140,
    "page_count": 14,
    "page_number": 1,
    "page_size": 10,
    "mode": "test",
    "organization_id": "JxYabZKcAw",
    "trace_id": "3b7f1d9a5c8e2b6f0d4a7c1f5b9d3e7a4c8f2b0d6e3a9c5f1b7d0e4a2c6f8b3d"
  },
  "links": {
    "first": "https://yourdomain.commercelayer.io/api/skus?sort=code&page[number]=1&page[size]=10",
    "next": "https://yourdomain.commercelayer.io/api/skus?sort=code&page[number]=2&page[size]=10",
    "last": "https://yourdomain.commercelayer.io/api/skus?sort=code&page[number]=14&page[size]=10"
  }
}
Pagination

The relationship must be one of the sortable relationships for the parent resource (in the example above the shipping category is one of the SKU's sortable relationships), the attribute must be one of the filterable ones for the related resource (in the example above the name is one of the shipping category's sortable attributes).

The following request fetches the list of orders associated with a certain customer, sorted by their creation date (descending):

On success, the API responds with a 200 OK status code, returning a paginated collection of the related resource objects, sorted in the requested order:

Pagination

You can get the full list of sortable attributes from the documentation of each resource.

Last updated