Get started
Kotlin SDK

Fetch data

Client basics

Mountaya is the one type that calls the network. Every method suspends, and is named after the API's operation id.

Construct a client

Build one client per credential and keep it for the app's lifetime:

import com.mountaya.client.Credential
import com.mountaya.client.Mountaya
import com.mountaya.client.SessionToken

fun frenchClient(publishableKey: String, sessionToken: String): Mountaya =
    Mountaya(
        Credential.Publishable(publishableKey, SessionToken.Value(sessionToken)),
        // Sent as Accept-Language on every request.
        language = "fr",
    )

Call close() when you are done with it.

Call the three APIs

The Map Studio API hangs under studio and agent, the Data API under data, and the Tile API under tiles:

import com.mountaya.client.Mountaya
import com.mountaya.client.generated.DataSchema

suspend fun tour(mountaya: Mountaya) {
    // Map Studio API: path parameters by name.
    val itinerary = mountaya.studio.itineraries.getItinerary("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
    println(itinerary.data.name)

    // Data API: one input value per query.
    val places = mountaya.data.geocode(DataSchema.GeocodeInput(query = "Chamonix"))
    println(places.places?.firstOrNull()?.name)

    // Tile API: the layers your credential draws.
    val layers = mountaya.tiles.getTileLayers()
    println(layers.data.terrains.map { it.slug })
}

Write

A body is built by a factory named after its type. Required fields are arguments; optional ones are set in the trailing builder:

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

suspend fun rename(mountaya: Mountaya) {
    val renamed =
        mountaya.studio.itineraries.patchItinerary(
            "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21",
            body = RestSchema.ItineraryUpdateParams { name = "Tour du Mont-Blanc, summer" },
        )
    println("${renamed.status} ${renamed.data.name}")
}

A field left unassigned is not sent and keeps its value; one assigned null is cleared. A write acts as a member: see act as a member.

Ask for fewer fields

A Data API call can narrow its answer to the fields you select:

import com.mountaya.client.FieldSelection
import com.mountaya.client.Mountaya
import com.mountaya.client.generated.DataSchema

suspend fun placeNames(mountaya: Mountaya) {
    val places =
        mountaya.data.geocode(
            DataSchema.GeocodeInput(query = "Chamonix"),
            select =
                FieldSelection {
                    field("places") {
                        field("name")
                        field("point")
                    }
                },
        )
    println(places.places?.map { it.name })
}

Fields you did not select come back null.

Stream events

A stream method answers an EventStream, a cold Flow of frames. Collect it to open the itinerary's collaboration events:

import com.mountaya.client.Mountaya

suspend fun watchEditors(mountaya: Mountaya) {
    mountaya.studio.collab.getItineraryEvents("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21").collect { frame ->
        // Each event is a class of its type, such as CollabPresenceJoinEvent.
        println(frame.event::class.simpleName)
    }
}

The stream closes when its collector leaves.

Read the response

A Map Studio API or Tile API method answers a Response:

import com.mountaya.client.Mountaya

suspend fun readAnswer(mountaya: Mountaya) {
    val answer = mountaya.studio.itineraries.getItinerary("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
    println("${answer.status} ${answer.requestId} ${answer.data.etag}")
    println(answer.header("Content-Language"))
}

A Data API method answers its result type directly.

Next steps

Last updated