# Manage an organization

> Canonical URL: https://docs.mountaya.com/en/sdks/swift/admin/
> Last updated: 2026-11-27
> Rules for agents: https://docs.mountaya.com/llms.txt#rules-for-agents

Run an organization's members, invitations, API keys, session tokens and webhook endpoints from a Swift server with MountayaAdmin.

`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:

```swift
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.

- `import MountayaAdmin` declares `admin` on the core's `Mountaya` in an extension: there is no second client type, and the credential, the retries, the idempotency keys and the error cases are the client's. Its groups: `billing`, `invitations`, `keys` (publishable keys), `members`, `organizations` (details, settings, usage, AI provider configuration, `postOrganization`), `sessions` (`postSession`), `users` (the acting user's memberships, preferences, notification preferences and invitations) and `webhooks`. Every method is the contract's `operationId`, and answers a `Response` with the payload at `data`.
- The types only these operations and the webhook events reach are `AdminSchema`'s (`AdminSchema.OrganizationMember`, `AdminSchema.OrganizationInvitation`, `AdminSchema.PublishableKey`, `AdminSchema.WebhookEndpointWithSecret`, `AdminSchema.Membership`), and `WebhookEvent`.
- Credentials: the organization's operations take a publishable key with the `management` scope and a session token minted for it. `management` is carried alone: a key with it carries no other scope, and it never ships in an app. Every read takes a token; any member's token reads members, invitations and settings, while keys, usage, billing and webhooks are read by an owner or an admin. Every write takes a token naming an owner or an admin. The `users` operations take the `studio-embedded` scope and a token naming the user. `postSession` takes the secret key alone.
- A token minted without `user_id` reads the `management` reads and writes nothing: a write answers `401`.
- Under a key, a token governs members with at most an admin's say: granting or taking `admin`, removing an admin, and inviting one answer `403`, whoever the token names. Those are the owner's acts in the Mountaya app. See [members and keys](https://docs.mountaya.com/en/apis/map-studio/members-and-keys.md).
- `mountaya.studio.organizations.postOrganizationLookup(body: .init(publishable_key:))` answers the key's organization: it takes the key in its body and sends no credential. The id is also in the Mountaya app's address, after `/organizations/`.
- `MOUNTAYA_SESSION_TOKEN` is a token your server minted for that member with the secret key ([mint session tokens](https://docs.mountaya.com/en/sdks/swift/admin.md#sessions)); a long-running server hands the client a `SessionTokenEngine` instead.
- `MountayaAdmin` builds wherever `MountayaClient` does; receiving webhooks (`constructWebhookEvent`) exists on macOS and Linux only.

## Members and invitations

```swift
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.

- `getOrganizationMembers(organization_id:query:)` answers one page of `AdminSchema.OrganizationMember` (`user_id`, `email`, `name`, `role`, `joined_at`), 50 by default; `walkGetOrganizationMembers(organization_id:)` walks every page ([pagination](https://docs.mountaya.com/en/sdks/swift/pagination.md#paginate)).
- `patchOrganizationMember(organization_id:user_id:body:)` with `.init(role:)` changes a role; `deleteOrganizationMember(organization_id:user_id:)` removes a member, or yourself to leave. `role` is an `AdminSchema.AssignableOrganizationRole`: the type names `.admin`, `.contributor` and `.viewer`, and under a key only `.contributor` and `.viewer` are taken. A key never assigns `.admin`: the owner does, in the Mountaya app.
- `postOrganizationInvitation(organization_id:body:)` with `.init(email:role:)` invites one address per request, answered alike for every well-formed address. The invitation expires after 30 days. `getOrganizationInvitations`, `getOrganizationInvitation` and `deleteOrganizationInvitation` list, read and withdraw them. Invitations held at once and seats are ceilings of the agreement: a `429` coded `limitReached`.
- A role the token's user lacks answers `403`, never `404`.

## API keys

```swift
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**.

- `keys` holds `getOrganizationPublishableKeys` (and its walk), `getOrganizationPublishableKey`, `postOrganizationPublishableKey`, `patchOrganizationPublishableKey` (name and scopes, conditional on `ifMatch:` with the key's `etag`) and `deleteOrganizationPublishableKey` (revokes it). An `AdminSchema.PublishableKey` carries `id`, `key`, `name`, `scopes`, `etag`, `created_at`, `last_used_at` and `revoked_at`.
- `scopes` are `AdminSchema.PublishableKeyScope`: `.publishing`, `.studioEmbedded`, `.tiles`, `.data`, `.mcp`, `.management`. At least one; `.management` alone on its key; `.studioEmbedded` with `.tiles` and `.data`. A scope whose product the organization does not hold is a `402`.
- No operation lists, creates or reveals a secret key. See [account and keys](https://docs.mountaya.com/en/account.md).

## Mint session tokens

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

```swift
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](https://docs.mountaya.com/en/sdks/swift/authentication.md) keeps it fresh in the app.

- `postSession(body:options:)` with `AdminSchema.SessionCreateParams(publishable_key:)` and, in its closure, `user_id` and `surface` (`.studioEmbedded` mints for writes acting as a member). It answers `RestSchema.Session`: `token` (prints `<redacted>`), `expires_at`, `user_id`. A token lives minutes; `SessionTokenEngine` mints again before it expires.
- A secret key authenticates `postSession` and nothing else: keep one client with `.secret(key:)` for minting, and make every other call through a client built with `.publishable(key:sessionToken:)`.
- `user_id` is the member every request made with the token acts as. Leave it out for a token that reads and never writes.
- `.secret(key:)` exists on macOS and Linux only; an app build cannot name it.

## Webhook endpoints

```swift
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](https://docs.mountaya.com/en/sdks/swift/webhooks.md) verifies them.

- `enabled_events` lists event types, families followed by `.*`, or `*` for every public type, later ones included. A type the contract does not publish is a `400` naming its position. `url` is a `URI` (`URI(_:)` from a string or a `URL`): `https`, printable ASCII, at most 2048 characters, with no credentials and no fragment, on a host that resolves to a public address.
- The answer is an `AdminSchema.WebhookEndpointWithSecret`: the endpoint and its `secret`, `whsk_` and 64 hexadecimal characters, shown once. No read returns it again; an owner or an admin reveals it in the Mountaya app, where it is rotated too.
- `webhooks` also holds `getOrganizationWebhookEndpoints`, `getOrganizationWebhookEndpoint`, `patchOrganizationWebhookEndpoint` (its URL, description, events and status), `deleteOrganizationWebhookEndpoint`, the events (`getOrganizationWebhookEvents`, `getOrganizationWebhookEvent`) and deliveries (`getOrganizationWebhookDeliveries`, `getOrganizationWebhookDelivery`), `getOrganizationWebhookHealth` and `getWebhookEventTypes`.
- `postOrganizationWebhookDelivery(organization_id:body:)` with `.init(webhook_endpoint_id:)` takes exactly one of `event_id` (resend one event of the last 30 days), `test` (a test event, delivered once without retries) or `since` (redeliver every event from an instant on). A disabled endpoint is a `409`.
- Webhook endpoints are a ceiling of the agreement: one past it is a `429` coded `limitReached`. The wire contract is on [webhook endpoints](https://docs.mountaya.com/en/webhooks/endpoints.md).

## Next steps

- [Receive webhooks](https://docs.mountaya.com/en/sdks/swift/webhooks.md): Verify a delivery and handle its event.
- [Authentication in the SDK](https://docs.mountaya.com/en/sdks/swift/authentication.md): Session tokens, and acting as a member.
