Get started
Examples

Assemble a full studio

Assemble a full studio

MountayaStudioHost builds the tool bus, the store and every handler, and MountayaStudio is the editor inside it. The preview arranges the same components in a customer's brand.

src/Studio.tsx
/// <reference types="vite/client" />
import { Mountaya } from "@mountaya/client/browser";
import { useSessionToken } from "@mountaya/client-react";
import { MountayaStudio, MountayaStudioHost } from "@mountaya/studio-react";
import "@mountaya/studio-react/styles.css";
import { useState } from "react";

// Your server route that mints a token with the secret key, for the signed-in member.
const mint = async () => {
  const response = await fetch("/api/mountaya-session");
  if (!response.ok) throw new Error(`The session route answered ${response.status}.`);
  return response.json();
};

// One of your itineraries.
const ITINERARY_ID = "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21";

export const Studio = () => {
  const { session } = useSessionToken({ mint });
  const [client] = useState(
    () =>
      new Mountaya({
        credential: {
          publishableKey: import.meta.env.VITE_MOUNTAYA_PUBLISHABLE_KEY,
          sessionToken: session,
        },
      }),
  );

  return (
    <MountayaStudioHost client={client} itineraryId={ITINERARY_ID}>
      <MountayaStudio />
    </MountayaStudioHost>
  );
};

Your server answers /api/mountaya-session for the signed-in member, with your secret key:

server/mountaya-session.ts
import { Mountaya } from "@mountaya/client";

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

// Answer GET /api/mountaya-session with this, behind your own sign-in.
// userId is the member your session vouches for, never a request field.
export async function mintStudioSession(userId: string): Promise<Response> {
  const { data } = await mountaya.admin.sessions.postSession({
    body: {
      publishable_key: process.env.MOUNTAYA_PUBLISHABLE_KEY ?? "",
      surface: "studio-embedded",
      user_id: userId,
    },
  });

  return Response.json(data, { headers: { "Cache-Control": "no-store" } });
}

Embedded Studio comes with an agreement.

Next steps

Last updated