Sorting

All list APIs support sorting to control the order of returned results.

How it works

When you send a request to a REST endpoint that supports sorting, you can include sorting parameters as query params. The API uses these parameters to determine the order in which results are returned.

By default, the response is sorted in ascending order using the id field. You can modify the sort order using sort_by and choose a different field using sort_key.

If no sorting parameters are provided, or if invalid values are passed, the API automatically applies the default sorting behavior.


Sorting parameters

ParameterDescription
sort_keyField to sort by. Accepted values: id, created_at.
sort_bySort direction. Accepted values: asc or desc.:
  • The default sort by is ASC.

Supported endpoints

You can use sorting on all REST Admin API GET endpoints that return a list of resources.

  • If an endpoint supports the limit parameter, it also supports sorting.
  • For example, the Plans resource supports sorting on the GET endpoint that retrieves a list of plans. plans.

Usage

You can pass sorting parameters as query params in any list API request.

The following example request fetches all subscriptions from the subscriptions endpoint, sorted by created_at in descending order.

GET /admin/api/2024-07/subscriptions?sort_by=desc&sort_key=created_at