Get started
Swift SDK
Fetch data

Manage an organization

Manage an organization

MountayaAdmin holds what a server that runs your organization needs: its members, its keys, its session tokens and its webhook endpoints. It is for servers and admin tools, never for an app.

Add the admin package

Link .product(name: "MountayaAdmin", package: "sdk-swift") beside MountayaClient. Importing it adds admin to the client you already hold:

import Foundation
import MountayaAdmin
import MountayaClient

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

// The key names its organization.
let organization = try await mountaya.studio.organizations.postOrganizationLookup(
  body: .init(publishable_key: publishableKey))
let organizationId = organization.data.id

let settings = try await mountaya.admin.organizations.getOrganizationSettings(
  organization_id: organizationId)
print(settings.data.etag)

Each sample below takes that client and organizationId.

Here MOUNTAYA_PUBLISHABLE_KEY is a key that carries the management scope, kept on your server. The session token names the member who administers: an owner or an admin.

Members and invitations

import MountayaAdmin
import MountayaClient

func manageMembers(on mountaya: Mountaya, organizationId: String) async throws {
  let members = try await mountaya.admin.members.getOrganizationMembers(
    organization_id: organizationId)
  for member in members.data {
    print(member.user_id, member.role.rawValue)
  }

  _ = try await mountaya.admin.members.patchOrganizationMember(
    organization_id: organizationId, user_id: "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1",
    body: .init(role: .viewer))

  _ = try await mountaya.admin.invitations.postOrganizationInvitation(
    organization_id: organizationId, body: .init(email: "camille@example.com", role: .contributor))
}

Each member has a user_id and a role. An invitation holds a seat until the person accepts it in the Mountaya app.

API keys

import MountayaAdmin
import MountayaClient

func createWebsiteKey(on mountaya: Mountaya, organizationId: String) async throws {
  let key = try await mountaya.admin.keys.postOrganizationPublishableKey(
    organization_id: organizationId,
    body: .init(name: "Website", scopes: [.publishing, .tiles]))
  print(key.data.key)  // the pk_… value your app or page sends
}

A key reaches only the scopes it carries. Secret keys are created in the Mountaya app, under Organization → API keys.

Mint session tokens

Minting takes the secret key, so it runs on your server:

import Foundation
import MountayaAdmin
import MountayaClient

// `userId` is the member your own sign-in vouches for, never one read from the request.
func mintSession(for userId: String) async throws -> RestSchema.Session {
  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
    })
  print(session.data.expires_at)  // the token lives minutes
  return session.data
}

Hand the session to the client that calls the API. Authentication in the SDK keeps it fresh in the app.

Webhook endpoints

import MountayaAdmin
import MountayaClient

func addEndpoint(on mountaya: Mountaya, organizationId: String) async throws {
  let endpoint = try await mountaya.admin.webhooks.postOrganizationWebhookEndpoint(
    organization_id: organizationId,
    body: .init(
      url: URI("https://example.com/mountaya/webhooks"),
      enabled_events: ["itinerary.published", "organization.member.*"]))
  print(endpoint.data.secret)  // whsk_…, shown once: store it now
}

The secret signs every delivery. Receive webhooks verifies them.

Next steps

Last updated