Get started
Swift SDK

Fetch data

Client basics

Mountaya is the one type that calls the network. Every method is async throws and named after the API's operation.

Construct a client

import Foundation
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let mountaya = try Mountaya(
  credential: .publishable(
    key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "",
    sessionToken: .value(environment["MOUNTAYA_SESSION_TOKEN"] ?? "")),
  language: "fr")

Build one client and share it: it is Sendable. language asks the API to answer in French.

Call the three APIs

An operation hangs where the API files it: studio and agent for the Map Studio API, data for the Data API, tiles for the Tile API. Its method is the operation's id:

import Foundation
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let mountaya = try Mountaya(
  credential: .publishable(
    key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "",
    sessionToken: .value(environment["MOUNTAYA_SESSION_TOKEN"] ?? "")))

let itinerary = try await mountaya.studio.itineraries.getItinerary(
  itinerary_id: "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
let places = try await mountaya.data.geocode(input: .init(query: "Chamonix"))
let layers = try await mountaya.tiles.getTileLayers()
print(itinerary.data.name)
print(places.places?.first?.name ?? "")
print(layers.data.terrains.first?.slug ?? "")

A path parameter is an argument by its name, the query is one query: value, and a body is one body: value.

Write

A body takes its required fields as arguments and its optional ones in a trailing closure:

import Foundation
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let mountaya = try Mountaya(
  credential: .publishable(
    key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "",
    sessionToken: .value(environment["MOUNTAYA_SESSION_TOKEN"] ?? "")))

let renamed = try await mountaya.studio.itineraries.patchItinerary(
  itinerary_id: "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21",
  body: .init { $0.name = "Tour du Mont-Blanc, summer" })
print(renamed.data.name)

A field left nil is not sent. A nullable one set to .some(nil) sends null, which clears it: $0.description = .some(nil).

Ask for fewer fields

A Data API call can name the fields it wants, so the API sends less:

import Foundation
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let mountaya = try Mountaya(
  credential: .publishable(
    key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "",
    sessionToken: .value(environment["MOUNTAYA_SESSION_TOKEN"] ?? "")))

let places = try await mountaya.data.geocode(
  input: .init(query: "Chamonix"),
  select: ["places": ["name": true, "point": true]])
print(places.places?.first?.name ?? "")

Fields you did not select come back nil.

Stream events

A stream is an EventStream you read with for try await. It opens as you read its first frame:

import Foundation
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let mountaya = try Mountaya(
  credential: .publishable(
    key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "",
    sessionToken: .value(environment["MOUNTAYA_SESSION_TOKEN"] ?? "")))

for try await frame in try mountaya.studio.collab.getItineraryEvents(
  itinerary_id: "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
{
  switch frame.event {
  case .CollabPresenceJoinEvent(let join): print(join.type.rawValue, join.user_id)
  case .CollabItineraryChangedEvent(let change): print(change.type.rawValue)
  default: print(frame.event)
  }
}

The stream reconnects and resumes on its own until you stop reading it.

Read the response

A REST call answers a Response: the data, the status, and the answer's request id and tag.

import Foundation
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let mountaya = try Mountaya(
  credential: .publishable(
    key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "",
    sessionToken: .value(environment["MOUNTAYA_SESSION_TOKEN"] ?? "")))

let answer = try await mountaya.studio.itineraries.getItinerary(
  itinerary_id: "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
print(answer.status, answer.requestId ?? "", answer.data.etag)
print(answer.header("Content-Language") ?? "")

Quote the requestId to support.

Next steps

Last updated