Get started
TypeScript SDK
Fetch data

Manage an organization

Manage an organization

The admin unit holds what a server that runs your organization needs: its members, its keys, its session tokens and its webhook endpoints. It is @mountaya/client/admin, a subpath of @mountaya/client.

Add the admin package

There is nothing more to install. Build MountayaAdmin over the client you already hold:

import { Mountaya } from "@mountaya/client";
import { MountayaAdmin } from "@mountaya/client/admin";

const publishableKey = process.env.MOUNTAYA_PUBLISHABLE_KEY ?? "";
const mountaya = new Mountaya({
  credential: { publishableKey, sessionToken: process.env.MOUNTAYA_SESSION_TOKEN ?? "" },
});
// The key names its organization.
const { data: organization } = await mountaya.studio.organizations.postOrganizationLookup({
  body: { publishable_key: publishableKey },
});
const organizationId = organization.id;

const admin = new MountayaAdmin(mountaya);
const { data: settings } = await admin.organizations.getOrganizationSettings({
  organization_id: organizationId,
});
console.log(settings.etag);

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 } from "@mountaya/client/admin";

const admin = new MountayaAdmin(mountaya);

const { data: members } = await admin.members.getOrganizationMembers({
  organization_id: organizationId,
});

await admin.members.patchOrganizationMember({
  body: { role: "viewer" },
  organization_id: organizationId,
  user_id: "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1",
});

await admin.invitations.postOrganizationInvitation({
  body: { email: "camille@example.com", role: "contributor" },
  organization_id: organizationId,
});

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 { MountayaAdmin } from "@mountaya/client/admin";

const { data: key } = await new MountayaAdmin(mountaya).keys.postOrganizationPublishableKey({
  body: { name: "Website", scopes: ["publishing", "tiles"] },
  organization_id: organizationId,
});

console.log(key.key); // the pk_… value your pages send

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 { Mountaya } from "@mountaya/client";
import { MountayaAdmin } from "@mountaya/client/admin";

const server = new Mountaya({
  credential: { secretKey: process.env.MOUNTAYA_SECRET_KEY ?? "" },
});

// userId is the member your own sign-in vouches for, never one read from the request.
const { data: session } = await new MountayaAdmin(server).sessions.postSession({
  body: { publishable_key: process.env.MOUNTAYA_PUBLISHABLE_KEY ?? "", user_id: userId },
});

console.log(session.expires_at); // the token lives minutes

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

Webhook endpoints

import { MountayaAdmin } from "@mountaya/client/admin";

const { data: endpoint } = await new MountayaAdmin(mountaya).webhooks.postOrganizationWebhookEndpoint({
  body: {
    enabled_events: ["itinerary.published", "organization.member.*"],
    url: "https://example.com/mountaya/webhooks",
  },
  organization_id: organizationId,
});

console.log(endpoint.secret); // whsk_…, shown once: store it now

The secret signs every delivery. Receive webhooks verifies them.

Next steps

Last updated