Get started
Get started

Authentication

Authentication

Every request carries your publishable key. Most also carry a short-lived session token, which your server mints with your secret key.

The three credentials

CredentialLooks likeHeaderLives
Publishable keypk_…X-API-KeyBrowser, app, or server
Secret keysk_…X-API-Key, on POST /v1/sessions onlyYour server only
Session tokensess_…X-Session-Token, beside the publishable keyMinted by your server, valid five minutes

The Data API needs a session token on every request. Create an account and keys shows where to create keys.

Scopes

A publishable key opens only the APIs its scopes name, such as data or tiles. Enable only what each integration needs: the scope table is on Create an account and keys.

Mint a session token on your server

Add one route to your backend. It calls POST /v1/sessions with your secret key, and returns the token to your page.

// app/api/mountaya-session/route.ts
import { Mountaya } from "@mountaya/client";

// A new token on every call: never cache this route.
export const dynamic = "force-dynamic";

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

export async function GET(): Promise<Response> {
  const { data } = await mountaya.admin.sessions.postSession({
    body: { publishable_key: process.env.MOUNTAYA_PUBLISHABLE_KEY ?? "" },
  });

  // { token, expires_at, user_id }: what the browser client reads.
  return Response.json(data, { headers: { "Cache-Control": "no-store" } });
}
Response
{
  "expires_at": "2026-11-09T10:55:49.466383Z",
  "token": "sess_your_session_token",
  "user_id": null
}

Use the token in the browser

Hand your route to the TypeScript client. It mints a token before the first request, and renews it before it expires.

import { Mountaya, SessionTokenEngine } from "@mountaya/client/browser";

const session = new SessionTokenEngine({
  mint: async () => {
    const response = await fetch("/api/mountaya-session");
    if (!response.ok) {
      throw new Error(`The session endpoint answered ${response.status}.`);
    }
    return response.json();
  },
});
// The engine renews the token while something listens to it.
session.subscribe(() => undefined);

const mountaya = new Mountaya({
  credential: { publishableKey: "pk_your_publishable_key", sessionToken: session },
});

In React, the useSessionToken hook does the same. Without the SDK, send X-API-Key and X-Session-Token on each request, and mint a new token about a minute before expires_at.

Act as a member

A publishable key identifies your organization, not a person. To act as one of your members, mint the token with their Mountaya user_id. Add surface: "studio-embedded" for an Embedded Studio and every itinerary, collection, or zone write.

curl -X POST https://api.mountaya.com/v1/sessions \
  -H "X-API-Key: $MOUNTAYA_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"publishable_key\": \"$MOUNTAYA_PUBLISHABLE_KEY\", \"user_id\": \"7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1\", \"surface\": \"studio-embedded\"}"
Response
{
  "data": {
    "expires_at": "2026-11-09T10:56:21.210479Z",
    "token": "sess_your_session_token",
    "user_id": "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1"
  }
}

Every request made with that token acts as that member, within their role. Mint it only for the person signed in to your own product.

Token audience

The surface field sets who a token is for, and the token keeps it for life. Leave it out for your own servers, pages, and SDK calls. Use studio-embedded for every Embedded Studio, the editor and its read-only viewer alike.

What a key alone reads

A publishable key without a token, or with a token minted without user_id, reads what you publish. It never reads drafts, and lists only public items. Only a token for a member reads your drafts and private items.

Serving anonymous viewers

A public page, such as a read-only studio, runs on a token minted without user_id. Mint it for a publishable key of its own, and hand every visitor the same token until shortly before it expires.

Allowed domains

List the sites that may use your publishable key from a browser under Organization → Security. Enter each host with an optional port, such as maps.example.com or localhost:3000, and list each subdomain on its own.

An empty list lets any page call the APIs with your key, but lets no site frame the hosted embed. So list every site you embed on.

You list one domain unless your agreement allows more. To list another, talk to us.

A share link opens one itinerary, or one collection with everything filed in it, read-only and as last published. Anyone holding the link can open it, so rotate it to cut off the old URL. Create one in the studio, or with POST /v1/itineraries/{itinerary_id}/share-link.

Credentials in the query string

A map tile or an <img> request cannot set headers. The Tile API and the Data API accept ?publishable_key= and ?session_token= instead, and a share link ?share_token=.

OAuth for MCP connectors

AI assistants connect to the MCP server over OAuth, and never see a secret key. How OAuth access works is the full model.

Next steps

Last updated