Swift SDK

# Outdoor maps as SwiftUI views

MountayaClient calls the Map Studio API, the Data API and the Tile API on every Apple platform and on Linux. On iPhone and iPad, MountayaElements draws the map, the elevation profile and the itinerary tables in SwiftUI.

[Read the docs](https://docs.mountaya.com/en/sdks/swift/)

[See the SDKs](/en/platform/sdks/)

Works with the tools you already use

Apple Health

Other MCP clients

Challenges we solve

## Call every Mountaya API from Swift, and draw the answer on iPhone and iPad.

### Link only the products you use

One package declares five products. The map sits behind its Elements trait, so an app that shows no map resolves no MapLibre.

### A secret key cannot ship in an app

The secret-key credential exists on macOS and Linux only. On iOS and the other device platforms it cannot be named, so the mistake does not compile.

### Turn by turn, said aloud

MountayaNavigation follows a route on the device's position, and speaks each maneuver in the system voice on iPhone and iPad.

Packages

## What you install for Swift

One package, which SwiftPM resolves from the public GitHub mirror. Link the client, and the map on iOS. The geo layer, navigation and the admin unit for your server come from the same package.

### MountayaClient

.package(url: "https://github.com/mountaya/sdk-swift", from: "0.1.0")

### MountayaElements

.package(url: "https://github.com/mountaya/sdk-swift", from: "0.1.0", traits: ["Elements"])

Integration

## From Package.swift to a map on screen

Each snippet carries its own imports. Your server mints the session token, and the app's engine keeps it fresh.

Swift

1

### Install

The map sits behind the package's Elements trait. Turn it on where you depend on the package, and SwiftPM resolves MapLibre Native with it.

```
dependencies: [
  .package(url: "https://github.com/mountaya/sdk-swift", from: "0.1.0", traits: ["Elements"])
],
targets: [
  .target(
    name: "App",
    dependencies: [
      .product(name: "MountayaClient", package: "sdk-swift"),
      .product(name: "MountayaElements", package: "sdk-swift"),
    ]
  )
]
```

2

### Authenticate

The Data API takes a session token beside the publishable key. Your server mints it through the admin unit, under a key no app can hold.

```
import MountayaAdmin
import MountayaClient

// On a server: a session token for the user your backend has signed in, for the app to use.
#if os(macOS) || os(Linux)
  func mintSession(forUser user: String) async throws -> RestSchema.Session {
    let server = try Mountaya(credential: .secret(key: "sk_your_secret_key"))
    let minted = try await server.admin.sessions.postSession(
      body: .init(publishable_key: "pk_your_publishable_key") { $0.user_id = user })
    return minted.data
  }
#endif
```

3

### Call

SessionTokenEngine fetches the token from your backend and refreshes it before it expires. Each operation is an async method named by its operation id.

```
import Foundation
import MountayaClient

// In an app: your backend mints the session token with its secret key.
func fetchSession() async throws -> RestSchema.Session {
  guard let url = URL(string: "https://example.com/api/mountaya-session") else { throw URLError(.badURL) }
  let (body, _) = try await URLSession.shared.data(from: url)
  return try JSONDecoder().decode(RestSchema.Session.self, from: body)
}

let engine = SessionTokenEngine { try await fetchSession() }
let client = try Mountaya(credential: .publishable(key: "pk_your_publishable_key", sessionToken: .source(engine)))

let itinerary = try await client.studio.itineraries.getItinerary(
  itinerary_id: "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
print(itinerary.data.name)
```

4

### Render

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

```
import MountayaClient
import MountayaElements
import SwiftUI

struct TrailMap: View {
  @State private var map: MapEngine

  init(mountaya: Mountaya) {
    _map = State(initialValue: MapEngine(client: mountaya))
  }

  var body: some View {
    MapView(map)
      .ignoresSafeArea()
      .task { try? await map.setTerrain() }
  }
}
```

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

[Read the docs](https://docs.mountaya.com/en/sdks/swift/)

[See the SDKs](/en/platform/sdks/)

Frequently asked questions

Which products do I link for Swift?

MountayaClient for the APIs, and MountayaElements for the map, with the package's Elements trait on. MountayaGeo holds the distances, the units and the elevation profile, and makes no network call. MountayaNavigation follows a route turn by turn.

Which platforms does it run on?

The client, the geo layer, navigation and the admin unit build with Swift 6.2.3 or newer. They run on iOS and iPadOS 17, Mac Catalyst 17, macOS 14, tvOS 17, visionOS 1, watchOS 10 and Linux. The map view is for iPhone and iPad, behind the Elements trait.

Does it run on a server?

Yes, on macOS and Linux. A server links MountayaAdmin beside the client, mints session tokens with its secret key and verifies webhooks with constructWebhookEvent.

Is the code the same as in the other SDKs?

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