# Pagination

> Every list method of @morevoice/sdk returns a PagedList: await it for the first page, or iterate it with for await to read every item.

List methods return a `PagedList`. `await` it for the first page (the `{ object: "list", data, has_more, next_cursor }` envelope), or iterate it with `for await` to read every item, page by page, as you go. `.toArray({ limit })` collects items into an array. Nothing is requested until you use the list.

Methods that return one: [`mv.agentSessions.list()`](https://docs.morevoice.ai/sdk/typescript/agent-sessions/#list), [`mv.assistants.list()`](https://docs.morevoice.ai/sdk/typescript/assistants/#list), [`mv.audioAssets.list()`](https://docs.morevoice.ai/sdk/typescript/audio-assets/#list), [`mv.callbacks.list()`](https://docs.morevoice.ai/sdk/typescript/callbacks/#list), [`mv.callerIds.list()`](https://docs.morevoice.ai/sdk/typescript/caller-ids/#list), [`mv.calls.list()`](https://docs.morevoice.ai/sdk/typescript/calls/#list), [`mv.calls.listCopilotEvents()`](https://docs.morevoice.ai/sdk/typescript/calls/#listcopilotevents), [`mv.campaigns.contacts.list()`](https://docs.morevoice.ai/sdk/typescript/campaigns/#contactslist), [`mv.campaigns.contacts.listAttempts()`](https://docs.morevoice.ai/sdk/typescript/campaigns/#contactslistattempts), [`mv.campaigns.list()`](https://docs.morevoice.ai/sdk/typescript/campaigns/#list), [`mv.conferenceRooms.list()`](https://docs.morevoice.ai/sdk/typescript/conference-rooms/#list), [`mv.conferenceRooms.listSessions()`](https://docs.morevoice.ai/sdk/typescript/conference-rooms/#listsessions), [`mv.connections.list()`](https://docs.morevoice.ai/sdk/typescript/connections/#list), [`mv.consents.list()`](https://docs.morevoice.ai/sdk/typescript/consents/#list), [`mv.copilotProfiles.list()`](https://docs.morevoice.ai/sdk/typescript/copilot-profiles/#list), [`mv.copilotProfiles.listTemplates()`](https://docs.morevoice.ai/sdk/typescript/copilot-profiles/#listtemplates), [`mv.dnc.list()`](https://docs.morevoice.ai/sdk/typescript/dnc/#list), [`mv.events.list()`](https://docs.morevoice.ai/sdk/typescript/events/#list), [`mv.faxes.list()`](https://docs.morevoice.ai/sdk/typescript/faxes/#list), [`mv.flowTemplates.list()`](https://docs.morevoice.ai/sdk/typescript/flow-templates/#list), [`mv.flows.list()`](https://docs.morevoice.ai/sdk/typescript/flows/#list), [`mv.flows.listVersions()`](https://docs.morevoice.ai/sdk/typescript/flows/#listversions), [`mv.inboundRoutes.list()`](https://docs.morevoice.ai/sdk/typescript/inbound-routes/#list), [`mv.invitations.list()`](https://docs.morevoice.ai/sdk/typescript/invitations/#list), [`mv.kbDocuments.list()`](https://docs.morevoice.ai/sdk/typescript/kb-documents/#list), [`mv.missedCalls.list()`](https://docs.morevoice.ai/sdk/typescript/missed-calls/#list), [`mv.organizations.list()`](https://docs.morevoice.ai/sdk/typescript/organizations/#list), [`mv.parkedCalls.list()`](https://docs.morevoice.ai/sdk/typescript/parked-calls/#list), [`mv.partnerBilling.invoices.list()`](https://docs.morevoice.ai/sdk/typescript/partner-billing/#invoiceslist), [`mv.phoneNumbers.list()`](https://docs.morevoice.ai/sdk/typescript/phone-numbers/#list), [`mv.phoneNumbers.listAvailable()`](https://docs.morevoice.ai/sdk/typescript/phone-numbers/#listavailable), [`mv.ports.list()`](https://docs.morevoice.ai/sdk/typescript/ports/#list), [`mv.queues.list()`](https://docs.morevoice.ai/sdk/typescript/queues/#list), [`mv.reports.list()`](https://docs.morevoice.ai/sdk/typescript/reports/#list), [`mv.reports.listRuns()`](https://docs.morevoice.ai/sdk/typescript/reports/#listruns), [`mv.requestLogs.list()`](https://docs.morevoice.ai/sdk/typescript/request-logs/#list), [`mv.secrets.list()`](https://docs.morevoice.ai/sdk/typescript/secrets/#list), [`mv.teams.list()`](https://docs.morevoice.ai/sdk/typescript/teams/#list), [`mv.users.list()`](https://docs.morevoice.ai/sdk/typescript/users/#list), [`mv.voices.list()`](https://docs.morevoice.ai/sdk/typescript/voices/#list), [`mv.webhookDeliveries.list()`](https://docs.morevoice.ai/sdk/typescript/webhook-deliveries/#list), [`mv.webhookEndpoints.list()`](https://docs.morevoice.ai/sdk/typescript/webhook-endpoints/#list), [`mv.whatsappNumbers.list()`](https://docs.morevoice.ai/sdk/typescript/whatsapp-numbers/#list).

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

for await (const agentSession of mv.agentSessions.list()) {
	console.log(agentSession);
}
```

## API

Exported from `@morevoice/sdk` (source: `packages/sdk-ts/src/lib/pagination.ts`).

### `ListPage`

*interface*

One page of a /v1 list.

```ts
interface ListPage<T> {
	object: "list";
	data: T[];
	has_more: boolean;
	/** Pass back as `starting_after` (or `ending_before` when paging backwards) for the next page. */
	next_cursor: string | null;
	url?: string;
}
```

### `CursorParams`

*interface*

The cursor parameters every /v1 list accepts (plus its own filters).

```ts
interface CursorParams {
	/** Page size, 1–100 (server default 20). */
	limit?: number;
	starting_after?: string;
	ending_before?: string;
}
```

### `ListFn`

*type*

```ts
type ListFn<T, P> = (params: P) => Promise<ListPage<T>>;
```

### `PagedList`

*class*

A lazily paged list: `await` it for the first page, `for await` it for every item, `.pages()` for every page,
`.toArray({ limit })` to collect. Nothing is requested until one of these is used; the first page is fetched once
and shared between them.

```ts
class PagedList<T, P extends CursorParams = CursorParams> implements PromiseLike<ListPage<T>>, AsyncIterable<T> {
	constructor(listFn: ListFn<T, P>, params: P);
	/** The first page. */
	firstPage(): Promise<ListPage<T>>;
	then<R1 = ListPage<T>, R2 = never>(onfulfilled?: ((page: ListPage<T>) => R1 | PromiseLike<R1>) | null, onrejected?: ((reason: unknown) => R2 | PromiseLike<R2>) | null): Promise<R1 | R2>;
	/** Every page, in order, fetching the next only when the previous one has been consumed. */
	async *pages(): AsyncGenerator<ListPage<T>, void, undefined>;
	async *[Symbol.asyncIterator](): AsyncGenerator<T, void, undefined>;
	/** Collect up to `limit` items (required, so an unbounded list never fills memory by accident). */
	async toArray(options: { limit: number }): Promise<T[]>;
}
```

### `autoPaginate()`

*function*

Wrap a single-page list call (the generated one, unwrapped) into an auto-paginating list.

```ts
function autoPaginate<T, P extends CursorParams = CursorParams>(listFn: ListFn<T, P>, params: P = {} as P): PagedList<T, P>;
```
