# Pagination and safe writes

> Canonical URL: https://docs.mountaya.com/en/sdks/kotlin/pagination/
> Last updated: 2026-11-27
> Rules for agents: https://docs.mountaya.com/llms.txt#rules-for-agents

Walk a whole listing as a cold Flow, repeat a write safely with an idempotency key, and write only if nothing changed with ifMatch.

A listing reads as a `Flow` of rows, a page at a time. A write lands once, and can refuse to overwrite a change it did not see.

## Read a whole listing

Every listing that pages has a walk beside it, `walk` before the operation's name. It takes the method's own filters and sort:

```kotlin
import com.mountaya.client.Mountaya
import com.mountaya.client.generated.RestSchema

suspend fun everyItinerary(mountaya: Mountaya) {
    mountaya.studio.itineraries
        .walkGetItineraries(pageSize = 50, maxPages = 10) { sort = RestSchema.GetItinerariesSort.UpdatedAtDescending }
        .collect { itinerary -> println(itinerary.name) }
}
```

The walk is cold: a page is asked for only as the rows before it are read, and a collector that stops early asks for no more. `PageWalk` walks any listing the same way, through its method.

- A printed walk (`walkGetItineraries`, `walkGetZones`, `walkGetCollections`, and the admin unit's `walkGetOrganizationMembers` and others) takes the method's own path parameters, arguments and builder, and asks for pages of the size its operation states, 50, unless you name `pageSize`. `maxPages` is the most pages it asks for before it stops without an error. It takes `options` too.
- `PageWalk(pageSize, maxPages) { paged -> method { …; paged() } }` applies `paged` last in your builder: it writes the page size and the cursor. `PageWalk.defaultPageSize` and `PageWalk.maxPageSize` hold the contract's figures.
- A walk returns a cold `Flow<T>`: nothing is sent until it is collected, and each collection walks from the first page again. Cancelling the collecting coroutine ends it.
- A page's error ends the flow with the error of the ladder that page raised, after its own retries.
- An operation this build has no method for walks through `client.walk(OperationDeclaration(…), path, serializer, query, pageSize, maxPages)`.

## Retry a write safely

Every write sends an `Idempotency-Key` on every attempt, so a repeated write lands once. Generate your own and keep it, so a retry of your own lands once too:

```kotlin
import com.mountaya.client.ConnectionException
import com.mountaya.client.Mountaya
import com.mountaya.client.generated.RestSchema
import kotlin.uuid.Uuid

suspend fun createTour(mountaya: Mountaya) {
    // Generated once for this intent, and kept: store it beside the pending write.
    val key = Uuid.random().toString()
    val body =
        RestSchema.ItineraryCreateParams(
            // The hiking-and-trail activity.
            activity_id = "a1c4e7b2-3d59-4f08-8b6a-2e5c9d0f7134",
            is_private = true,
            name = "Tour du Mont-Blanc",
        )
    val created =
        try {
            mountaya.studio.itineraries.postItinerary(body) { idempotencyKey = key }
        } catch (lost: ConnectionException) {
            // The same key again: the API answers the first write, never a second itinerary.
            mountaya.studio.itineraries.postItinerary(body) { idempotencyKey = key }
        }
    println("${created.status} ${created.location}")
}
```

- The client draws a key where you set none, from its `RandomSource`, and sends the same key on every attempt of that call. Set `idempotencyKey` yourself to make a repeat across calls or processes safe: the API answers a repeat of a key with the first answer (`ResponseReport.isReplayed` is `true`).
- A repeat whose first request is still running is a `409` coded `IDEMPOTENCY_KEY_IN_USE`, which the client asks again while its wait budget lasts, then throws `IdempotencyKeyInUseException`.
- A repeat whose first request made its write but whose answer cannot be given again (too large to keep, a `5xx` after the write, a credential shown once) is `IdempotentAnswerUnavailableException`, never retried. Its `answer` carries `status`, `location` and `etag`: read the resource; a new key would write twice.
- An operation by declaration states `declaresIdempotencyKey = true` to take one.

## Write only if nothing changed

Send the tag you read the itinerary under. The write is refused if someone changed it since:

```kotlin
import com.mountaya.client.Mountaya
import com.mountaya.client.PreconditionFailedException
import com.mountaya.client.generated.RestSchema

suspend fun renameIfUnchanged(mountaya: Mountaya) {
    val id = "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21"
    val read = mountaya.studio.itineraries.getItinerary(id)
    try {
        mountaya.studio.itineraries.patchItinerary(id, body = RestSchema.ItineraryUpdateParams { name = "Tour du Mont-Blanc, summer" }) {
            ifMatch = read.data.etag
        }
    } catch (stale: PreconditionFailedException) {
        // Changed since it was read: read it again, and let the person decide.
        val now = mountaya.studio.itineraries.getItinerary(id)
        println("Now named ${now.data.name}")
    }
}
```

- `ifMatch` sends `If-Match` with the tag a read answered, `Response.etag` or the resource's own `etag` field. A stale one is a `PreconditionFailedException` (`412`), never repeated; its `etag` is the tag the resource holds now where the answer carried one.
- Only an operation the contract declares conditional takes `ifMatch`; an operation by declaration states `isConditional = true`.
- A tag holding a character no header carries is refused with `ConfigurationException` before any request.

## Next steps

- [Manage an organization](https://docs.mountaya.com/en/sdks/kotlin/admin.md): Members, keys and webhook endpoints from a server.
- [Errors and retries](https://docs.mountaya.com/en/sdks/kotlin/errors.md): The exception ladder, codes and retries.
