Get started
Swift SDK
Get started

Server quickstart

Server quickstart

One Swift program, on macOS or Linux: mint a session token with your secret key, list your itineraries, then route a hike from Chamonix to Argentière.

Before you start

You need Swift 6.2.3 or newer, and two keys from Organization → API keys in the Mountaya app: a secret key, and a publishable key with the publishing and data scopes.

export MOUNTAYA_SECRET_KEY="sk_your_secret_key"
export MOUNTAYA_PUBLISHABLE_KEY="pk_your_publishable_key"

Install

mkdir trail-server && cd trail-server
swift package init --type executable

In Package.swift, add the package and link two products:

// swift-tools-version: 6.2
import PackageDescription

let package = Package(
  name: "trail-server",
  platforms: [.macOS(.v14)],
  dependencies: [
    .package(url: "https://github.com/mountaya/sdk-swift", from: "0.1.0")
  ],
  targets: [
    .executableTarget(
      name: "trail-server",
      dependencies: [
        .product(name: "MountayaClient", package: "sdk-swift"),
        .product(name: "MountayaAdmin", package: "sdk-swift"),
      ]
    )
  ]
)

Mint a session token

Your secret key mints a short-lived session token for the publishable key. Put this in Sources/trail-server/main.swift and run swift run:

import Foundation
import MountayaAdmin
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let server = try Mountaya(credential: .secret(key: environment["MOUNTAYA_SECRET_KEY"] ?? ""))
let session = try await server.admin.sessions.postSession(
  body: .init(publishable_key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "")
).data
print("The session expires at", session.expires_at)

Call the Data API

Every other call sends the publishable key with that token. Replace main.swift with this:

import Foundation
import MountayaAdmin
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let publishableKey = environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? ""
let server = try Mountaya(credential: .secret(key: environment["MOUNTAYA_SECRET_KEY"] ?? ""))
let session = try await server.admin.sessions.postSession(
  body: .init(publishable_key: publishableKey)
).data
let mountaya = try Mountaya(
  credential: .publishable(key: publishableKey, sessionToken: .minted(session)))

// Map Studio API: your five most recently updated itineraries.
let itineraries = try await mountaya.studio.itineraries.getItineraries(
  query: .init {
    $0.sort = .updatedAtDescending
    $0.limit = 5
  })
for itinerary in itineraries.data {
  print(itinerary.name, "updated", itinerary.updated_at)
}

// Data API: a hike from Chamonix to Argentière.
let found = try await mountaya.data.directions(
  input: .init(activity: .hikingAndTrail, waypoints: [[6.8712, 45.9231], [6.9271, 45.9822]]),
  select: ["routes": ["distance": true, "duration": true, "summary": true]])
print(found.routes?.first?.summary ?? "")
Docs Tour du Mont-Blanc updated 2026-11-09 10:53:23 +0000
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. It posts to your server's /session route, and your server answers the session it minted for the user your own sign-in names. Swift has no web server of its own, so your framework's route on 127.0.0.1:8787 calls this function:

import Foundation
import MountayaAdmin
import MountayaClient

// What the route sends back: a status, its headers and its body.
struct SessionAnswer {
  var status: Int
  var headers: [String: String] = [:]
  var body = Data()
}

func answerSession(method: String, path: String, signedInUser userId: String) async throws
  -> SessionAnswer
{
  guard path == "/session" else { return SessionAnswer(status: 404) }
  guard method == "POST" else { return SessionAnswer(status: 405, headers: ["Allow": "POST"]) }
  let environment = ProcessInfo.processInfo.environment
  let server = try Mountaya(credential: .secret(key: environment["MOUNTAYA_SECRET_KEY"] ?? ""))
  let session = try await server.admin.sessions.postSession(
    body: .init(publishable_key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "") {
      $0.user_id = userId
    })
  return SessionAnswer(
    status: 200,
    headers: ["Content-Type": "application/json", "Cache-Control": "no-store"],
    body: try JSONEncoder().encode(session.data))
}

The app decodes that body as a RestSchema.Session and hands it to a SessionTokenEngine.

Next steps

Last updated