Skip to content

Kotlin SDK

Outdoor maps in Jetpack Compose

mountaya-client calls the Map Studio API, the Data API and the Tile API from Kotlin Multiplatform code, on the JVM, Android and iOS. On Android, mountaya-elements draws the map, the elevation profile and the itinerary tables in Compose.

Works with the tools you already use

Challenges we solve

Call every Mountaya API from Kotlin, and draw the answer in Compose.

One coordinate for every target

The client, the geo layer and the elements are Kotlin Multiplatform. One Maven Central coordinate resolves the JVM, Android or iOS variant.

No secret key on a device

The secret-key credential is declared on the JVM alone. An Android or iOS build has no symbol to construct one, so the mistake does not compile.

A map that brings its renderer

mountaya-elements brings MapLibre and Compose's UI with it, so your Android app declares neither of them.

Packages

What you install for Kotlin

Modules on Maven Central, all at one version. The client calls every Mountaya API, and the elements draw the result. mountaya-geo, mountaya-navigation and mountaya-admin install the same way.

mountaya-client

implementation("com.mountaya:mountaya-client:0.1.0")

mountaya-elements

implementation("com.mountaya:mountaya-elements:0.1.0")

Integration

From Gradle to a map in Compose

Each snippet carries its own imports. The server step runs on the JVM, and the map composable on Android.

Kotlin

Install

Declare each module once, in your common source set. A Multiplatform build resolves each target's variant from that one coordinate.

kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("com.mountaya:mountaya-client:0.1.0")
            implementation("com.mountaya:mountaya-geo:0.1.0")
            implementation("com.mountaya:mountaya-elements:0.1.0")
        }
    }
}

Authenticate

The Data API takes a session token beside the publishable key. Your server mints it on the JVM, through the admin unit.

import com.mountaya.admin.admin
import com.mountaya.admin.generated.AdminSchema
import com.mountaya.client.Credential
import com.mountaya.client.Mountaya
import com.mountaya.client.generated.RestSchema

// On your backend, on the JVM: no Android or iOS build declares a secret-key credential.
val server = Mountaya(Credential(secretKey = "sk_your_secret_key"))

// What your app's session engine asks your backend for: a session for one user.
suspend fun mintFor(userId: String): RestSchema.Session {
    val minted =
        server.admin.sessions.postSession(
            AdminSchema.SessionCreateParams(publishable_key = "pk_your_publishable_key") {
                user_id = userId
            },
        )
    return minted.data
}

Call

SessionTokenEngine mints through your function, in the scope you give it, and refreshes before expiry. Each operation is a suspend function named by its operation id.

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mountaya.client.Credential
import com.mountaya.client.Mountaya
import com.mountaya.client.SessionToken
import com.mountaya.client.SessionTokenEngine
import com.mountaya.client.generated.RestSchema
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import java.net.URI

// In an app: your backend mints the session token with its secret key.
class TrailViewModel : ViewModel() {
    private val sessions = SessionTokenEngine(viewModelScope, mint = { fetchSession() })
    val client = Mountaya(Credential.Publishable("pk_your_publishable_key", SessionToken.Source(sessions)))

    suspend fun itineraryName(): String =
        client.studio.itineraries.getItinerary("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21").data.name

    override fun onCleared() = client.close()

    private suspend fun fetchSession(): RestSchema.Session =
        withContext(Dispatchers.IO) {
            val body = URI.create("https://example.com/api/mountaya-session").toURL().readText()
            Json { ignoreUnknownKeys = true }.decodeFromString(RestSchema.Session.serializer(), body)
        }
}

Render

MapEngine holds what the map shows, and MapView draws it in Compose. Every tile request carries your client's credential.

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import com.mountaya.client.Mountaya
import com.mountaya.elements.MapEngine
import com.mountaya.elements.MapView

@Composable
fun TrailMap(mountaya: Mountaya) {
    val map = remember { MapEngine(mountaya) }
    LaunchedEffect(map) { map.setTerrain() }
    MapView(map)
}

What will you build with the SDKs & Elements?

Tell us what your product needs to draw. We show you the SDKs & Elements that draw it.

Frequently asked questions

mountaya-client for the APIs, mountaya-geo for distances, units and the elevation profile, and mountaya-elements for the map. An Android app that navigates adds mountaya-navigation, which reads the device's location and speaks each maneuver.

The client, the admin unit, the geo layer and the elements build for the JVM, Android and iOS. The map view and navigation run on Android. Your build needs Kotlin 2.4, Java 17 and an Android minSdk of 24, or newer.

Yes, on the JVM. A server adds mountaya-admin, mints session tokens with its secret key and verifies webhooks with constructWebhookEvent.

Yes, in Kotlin's own idiom. Each method carries the schema's operation id, and SessionTokenEngine and constructWebhookEvent keep their names in the TypeScript, Swift and Dart SDKs. The layers match too: the client, the geo layer and the map. Embedded Studio stays React only.