# List every rule in a guide

Source: https://watership.ca/api-docs/docs/tasks/list-every-rule

Page through GET /v1/rules with a cursor to export all the rules in one guide or tier.

`GET /v1/rules` responds with rules a page at a time. This guide shows how to follow the cursor to the last page, so you can export every rule in a guide, for example to build a checklist or to load rule names into your own tool.

## Ask for the first page [#ask-for-the-first-page]

Filter to the rules you want and set the page size. The largest page is 100 rules, so the style guide's 512 rules take two pages:

```sh
curl "http://127.0.0.1:4010/v1/rules?guide=style-guide&limit=100" \
  -H "Authorization: Bearer wsk_test_example"
```

The response has the rules in `data`, plus `has_more` and `next_cursor`:

```json
{
  "data": [ ... ],
  "has_more": true,
  "next_cursor": "cnVsZTpwcm9jLXZlcmlmeS1lbmQ"
}
```

## Follow the cursor [#follow-the-cursor]

While `has_more` is `true`, ask for the next page with `cursor` set to the `next_cursor` you got. Keep the same filters on every page.

```js title="list-rules.mjs"
const API_URL = process.env.WATERSHIP_API_URL ?? 'http://127.0.0.1:4010';
const API_KEY = process.env.WATERSHIP_API_KEY ?? 'wsk_test_example';

async function listRules(filters, { maxPages = 50 } = {}) {
  const rules = [];
  let cursor = null;
  for (let page = 0; page < maxPages; page++) {
    const params = new URLSearchParams({ ...filters, limit: '100' });
    if (cursor) params.set('cursor', cursor);
    const response = await fetch(`${API_URL}/v1/rules?${params}`, {
      headers: { Authorization: `Bearer ${API_KEY}` },
    });
    if (!response.ok) throw new Error(`List failed: ${response.status}`);
    const body = await response.json();
    rules.push(...body.data);
    if (!body.has_more) return rules;
    cursor = body.next_cursor;
  }
  throw new Error(`Stopped after ${maxPages} pages.`);
}

const neverRules = await listRules({ tier: 'never' });
console.log(neverRules.map((r) => `${r.id}\t${r.name}`).join('\n'));
```

A cursor is opaque. Don't build one yourself or read meaning into it, and don't reuse it with different filters.

<Callout type="warn" title="Against the mock">
  The mock responds with the same example page every time, with `has_more` set to `true`, so the loop would never end on its own. The `maxPages` limit stops it. Keep a limit like this in production code too, so a fault can't turn into an endless loop.
</Callout>

## Combine filters [#combine-filters]

Filters combine, and every one must match. To export the mechanical rules in the style guide that the automated checks don't cover yet:

```js
const gaps = await listRules({ guide: 'style-guide', check: 'mechanical', automated: 'false' });
```

## Related [#related]

- [`GET /v1/rules`](https://watership.ca/api-docs/docs/reference/api/rules/list-rules) in the API reference, with every filter
- [Pagination](https://watership.ca/api-docs/docs/reference/conventions#pagination) in the conventions reference
