# Pagination and sorting

> Page through lists with limit and starting_after.

Lists come back a page at a time. Each response is a list object: the results are in `data`, and `has_more` says whether there are more after them.

```json
{
  "object": "list",
  "data": [ ... ],
  "has_more": true
}
```

## Parameters

`limit`

The most results to return, from 1 to 100. For example, `limit=100`. The default is 25.

`starting_after`

The ID of the last result on the previous page, to get the page after it. For example, `starting_after=inv_8f3a2c1d9b7e4f60a1b2c3d4e5f60718`. Leave it out to get the first page.

`sort`

The field to sort results by. For example, `sort=due_date`. Put `-` in front for descending order, for example `sort=-due_date`. The default is `-created_at`, newest first.

## Fields you can sort by

| List         | Fields                                                                 |
| ------------ | ---------------------------------------------------------------------- |
| Creators     | `created_at`, `updated_at`, `name`                                     |
| Invoices     | `created_at`, `updated_at`, `due_date`, `invoice_date`, `total_amount` |
| Payment runs | `created_at`, `updated_at`, `total_amount`                             |

Purchase orders and payments can't be sorted. They come back newest first.

## Example

Say you want your approved invoices, the ones due soonest first, 50 at a time.

The request with the default pagination looks like this:

```text
GET https://api.stridehq.ai/v1/invoices?status=approved
```

Add the pagination and sorting parameters. Here we limit each page to 50 invoices and sort them by due date, earliest first:

```text
&limit=50&sort=due_date
```

The full request looks like this:

```bash
curl "https://api.stridehq.ai/v1/invoices?status=approved&limit=50&sort=due_date" \
  -H "Authorization: Bearer $STRIDEHQ_KEY"
```

If the response has `"has_more": true`, take the ID of the last invoice in `data` and ask for the next page, with the same parameters:

```bash
curl "https://api.stridehq.ai/v1/invoices?status=approved&limit=50&sort=due_date&starting_after=inv_8f3a2c1d9b7e4f60a1b2c3d4e5f60718" \
  -H "Authorization: Bearer $STRIDEHQ_KEY"
```

Keep going until `has_more` is `false`. Pages stay in order while new records are added, so you won't get the same result twice.

A `limit` outside 1 to 100, or a `sort` field the list doesn't have, gets a `400` with the code `invalid_parameter`.
