# Pagination

Paginated list endpoints use page numbers and a page size.

## Paginated Endpoints

| Endpoint | Sort order |
|----------|-----------|
| `GET /api/v1/orders` | Order date, newest first |
| `GET /api/v1/shipments` | Created, newest first |
| `GET /api/v1/products` | Last updated, newest first |
| `GET /api/v1/returns` | Requested, newest first (uses a different response shape; see [below](#returns-list)) |

These list endpoints are not paginated and return every item in one array:
- `GET /api/v1/warehouses`
- `GET /api/v1/webhooks`

## Query Parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `page` | integer | `1` | Page number. Values below 1 are treated as 1. |
| `limit` | integer | `50` | Items per page, from 1 to 100. Values above 100 are reduced to 100 and values below 1 are raised to 1. |

A missing or non-numeric value uses the default. A page past the last page returns an empty `data` array, not an error.

Pages are offsets into the current results, so items created or changed between requests can shift between pages.

## Response Meta

Paginated responses include a `meta` object alongside `data`:

```json
{
  "data": [ ... ],
  "meta": {
    "page": 1,
    "limit": 50,
    "total": 237,
    "totalPages": 5
  }
}
```

| Field | Description |
|-------|-------------|
| `page` | Current page number |
| `limit` | Items per page actually applied |
| `total` | Total number of matching items |
| `totalPages` | Total number of pages (`Math.ceil(total / limit)`); `0` when nothing matches |

### Returns List

`GET /api/v1/returns` accepts the same `page` and `limit` parameters, but its response is not wrapped in `data`/`meta`. It returns the items in `returns`, the paging values in `pagination` (same four fields as `meta`), and per-status totals in `counts`:

```json
{
  "returns": [ ... ],
  "pagination": {
    "page": 1,
    "limit": 50,
    "total": 12,
    "totalPages": 1
  },
  "counts": { "all": 12, "pending": 3, "approved": 2, ... }
}
```

## Examples

### Fetch the First Page

```bash
curl "https://shipwave.app/api/v1/orders?page=1&limit=25" \
  -H "Authorization: Bearer sw_live_abc123..."
```

### Iterate Through All Pages

```typescript
async function fetchAllOrders(apiKey: string) {
  const baseUrl = "https://shipwave.app/api/v1/orders";
  let page = 1;
  const limit = 100;
  const allOrders = [];

  while (true) {
    const res = await fetch(`${baseUrl}?page=${page}&limit=${limit}`, {
      headers: { Authorization: `Bearer ${apiKey}` },
    });
    const { data, meta } = await res.json();

    allOrders.push(...data);

    if (page >= meta.totalPages) break;
    page++;
  }

  return allOrders;
}
```

Each page is one request against your API key's [rate limit](rate-limiting.md).

### Combine with Filters

Pagination works alongside each endpoint's filter parameters:

```bash
curl "https://shipwave.app/api/v1/orders?status=pending&storeId=store_abc&page=2&limit=25" \
  -H "Authorization: Bearer sw_live_abc123..."
```
