Get started
Kotlin SDK
Fetch data

Manage an organization

Manage an organization

mountaya-admin 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.

Add the admin package

dependencies {
    implementation("com.mountaya:mountaya-admin:0.1.0")
}

Import admin, and the operations appear under mountaya.admin on the client you already hold:

import com.mountaya.admin.admin
import com.mountaya.client.Credential
import com.mountaya.client.Mountaya
import com.mountaya.client.SessionToken
import com.mountaya.client.generated.RestSchema

suspend fun settings() {
    val publishableKey = System.getenv("MOUNTAYA_PUBLISHABLE_KEY").orEmpty()
    val mountaya =
        Mountaya(
            Credential.Publishable(publishableKey, SessionToken.Value(System.getenv("MOUNTAYA_SESSION_TOKEN").orEmpty())),
        )
    // The key names its organization.
    val organization = mountaya.studio.organizations.postOrganizationLookup(RestSchema.OrganizationLookupParams(publishableKey))
    val organizationId = organization.data.id

    val settings = mountaya.admin.organizations.getOrganizationSettings(organizationId)
    println(settings.data.etag)
    mountaya.close()
}

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, minted as mint session tokens shows.

Each sample below takes that client and the organization's id.

Members and invitations

import com.mountaya.admin.admin
import com.mountaya.admin.generated.AdminSchema
import com.mountaya.client.Mountaya

suspend fun team(mountaya: Mountaya, organizationId: String) {
    val members = mountaya.admin.members.getOrganizationMembers(organizationId)
    for (member in members.data) println("${member.user_id} ${member.role}")

    mountaya.admin.members.patchOrganizationMember(
        organizationId,
        "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1",
        AdminSchema.OrganizationMemberUpdateParams(AdminSchema.AssignableOrganizationRole.Viewer),
    )

    mountaya.admin.invitations.postOrganizationInvitation(
        organizationId,
        AdminSchema.OrganizationInvitationCreateParams("camille@example.com", AdminSchema.AssignableOrganizationRole.Contributor),
    )
}

Each member has a user_id and a role. A key never grants admin: the owner does that in the Mountaya app. An invitation holds a seat until the person accepts it.

API keys

import com.mountaya.admin.admin
import com.mountaya.admin.generated.AdminSchema
import com.mountaya.client.Mountaya

suspend fun websiteKey(mountaya: Mountaya, organizationId: String) {
    val created =
        mountaya.admin.keys.postOrganizationPublishableKey(
            organizationId,
            AdminSchema.PublishableKeyCreateParams(
                "Website",
                listOf(AdminSchema.PublishableKeyScope.Publishing, AdminSchema.PublishableKeyScope.Tiles),
            ),
        )
    // The pk_… value your app sends.
    println(created.data.key)
}

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 com.mountaya.admin.admin
import com.mountaya.admin.generated.AdminSchema
import com.mountaya.client.Credential
import com.mountaya.client.Mountaya
import com.mountaya.client.generated.RestSchema
import com.mountaya.client.invoke

// `userId` is the member your own sign-in vouches for, never one read from the request.
suspend fun mint(userId: String): RestSchema.Session {
    val server = Mountaya(Credential.Secret(System.getenv("MOUNTAYA_SECRET_KEY").orEmpty()))
    val session =
        server.admin.sessions.postSession(
            AdminSchema.SessionCreateParams(publishable_key = System.getenv("MOUNTAYA_PUBLISHABLE_KEY").orEmpty()) {
                user_id = userId
            },
        )
    server.close()
    // The token lives minutes.
    println(session.data.expires_at)
    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 com.mountaya.admin.admin
import com.mountaya.admin.generated.AdminSchema
import com.mountaya.client.Mountaya
import com.mountaya.client.Url

suspend fun subscribe(mountaya: Mountaya, organizationId: String) {
    val endpoint =
        mountaya.admin.webhooks.postOrganizationWebhookEndpoint(
            organizationId,
            AdminSchema.WebhookEndpointCreateParams(
                Url("https://example.com/mountaya/webhooks"),
                listOf("itinerary.published", "organization.member.*"),
            ),
        )
    // whsk_…, shown once: store it now.
    println(endpoint.data.secret)
}

The secret signs every delivery. Receive webhooks verifies them.

Next steps

Last updated