Get started
Kotlin SDK
Get started

Server quickstart

Server quickstart

A Kotlin program on the JVM that mints a session token, calls the Data API with it, and hands tokens to your app.

Before you start

You need JDK 17 or newer and Gradle. You also need two keys from Organization → API keys in the Mountaya app: a secret key, and a publishable key with the publishing and data scopes. Export both:

export MOUNTAYA_PUBLISHABLE_KEY=pk_your_publishable_key
export MOUNTAYA_SECRET_KEY=sk_your_secret_key

Install

Create a Kotlin JVM application and add the admin package, which brings the client with it:

plugins {
    kotlin("jvm") version "2.4.21"
    application
}

dependencies {
    implementation("com.mountaya:mountaya-admin:0.1.0")
}

application { mainClass.set("MainKt") }

Each program below is a whole src/main/kotlin/Main.kt. Run it with ./gradlew run.

Mint a session token

A client built over the secret key mints the token through the admin package:

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.invoke

suspend fun main() {
    val server = Mountaya(Credential.Secret(System.getenv("MOUNTAYA_SECRET_KEY").orEmpty()))
    val publishableKey = System.getenv("MOUNTAYA_PUBLISHABLE_KEY").orEmpty()
    val session = server.admin.sessions.postSession(AdminSchema.SessionCreateParams(publishable_key = publishableKey)).data
    // Never print the token itself.
    println("Minted a session token that expires at ${session.expires_at}")
    server.close()
}

Call the Data API

Every other call sends the publishable key with the minted token:

import com.mountaya.admin.admin
import com.mountaya.admin.generated.AdminSchema
import com.mountaya.client.Credential
import com.mountaya.client.FieldSelection
import com.mountaya.client.Mountaya
import com.mountaya.client.SessionToken
import com.mountaya.client.generated.DataSchema
import com.mountaya.client.generated.RestSchema
import com.mountaya.client.invoke

suspend fun main() {
    val publishableKey = System.getenv("MOUNTAYA_PUBLISHABLE_KEY").orEmpty()
    val server = Mountaya(Credential.Secret(System.getenv("MOUNTAYA_SECRET_KEY").orEmpty()))
    val session = server.admin.sessions.postSession(AdminSchema.SessionCreateParams(publishable_key = publishableKey)).data
    val mountaya = Mountaya(Credential.Publishable(publishableKey, SessionToken.Minted(session)))

    // Map Studio API: your five most recently updated itineraries.
    val itineraries =
        mountaya.studio.itineraries.getItineraries {
            sort = RestSchema.GetItinerariesSort.UpdatedAtDescending
            limit = 5
        }
    for (itinerary in itineraries.data) println("${itinerary.name}, updated ${itinerary.updated_at}")

    // Data API: a hike from Chamonix to Argentière.
    val found =
        mountaya.data.directions(
            DataSchema.DirectionsInput(
                activity = DataSchema.ActivitySlug.HikingAndTrail,
                waypoints = listOf(listOf(6.8712, 45.9231), listOf(6.9271, 45.9822)),
            ),
            select =
                FieldSelection {
                    field("routes") {
                        field("distance")
                        field("duration")
                        field("summary")
                    }
                },
        )
    println(found.routes?.firstOrNull()?.summary)

    mountaya.close()
    server.close()
}
Docs Tour du Mont-Blanc, updated 2026-11-09T10:53:23.814Z
9.0 km, ~2h40m, +277m / -56m elevation, 67% path, 12% track, 8% road.

Hand tokens to your app

An app never holds the secret key. Your server answers POST /session with a token minted for the member your own sign-in vouches for:

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
import com.mountaya.client.invoke
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpServer
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import java.net.InetSocketAddress
import java.util.concurrent.ConcurrentHashMap

// Your own sign-in fills this: each of its session cookies, to the member it vouches for.
private val signedIn = ConcurrentHashMap<String, String>()

fun main() {
    val publishableKey = System.getenv("MOUNTAYA_PUBLISHABLE_KEY").orEmpty()
    val server = Mountaya(Credential.Secret(System.getenv("MOUNTAYA_SECRET_KEY").orEmpty()))
    val http = HttpServer.create(InetSocketAddress("127.0.0.1", 8787), 0)
    http.createContext("/") { exchange ->
        when {
            exchange.requestURI.path != "/session" -> answer(exchange, 404, "")
            exchange.requestMethod != "POST" -> answer(exchange, 405, "")
            else -> {
                val member = signedIn[exchange.requestHeaders.getFirst("Cookie").orEmpty()]
                val params = AdminSchema.SessionCreateParams(publishable_key = publishableKey) { member?.let { user_id = it } }
                val session = runBlocking { server.admin.sessions.postSession(params).data }
                answer(exchange, 200, Json.encodeToString(RestSchema.Session.serializer(), session))
            }
        }
    }
    http.start()
    println("POST http://127.0.0.1:8787/session")
}

private fun answer(exchange: HttpExchange, status: Int, body: String) {
    val bytes = body.encodeToByteArray()
    exchange.responseHeaders.add("Content-Type", "application/json")
    exchange.responseHeaders.add("Cache-Control", "no-store")
    if (status == 405) exchange.responseHeaders.add("Allow", "POST")
    exchange.sendResponseHeaders(status, if (bytes.isEmpty()) -1 else bytes.size.toLong())
    exchange.responseBody.use { it.write(bytes) }
}

The app's SessionTokenEngine posts to that address and reads the Session it answers. Authentication in the SDK wires it.

Next steps

Last updated