# Sessions

> Canonical URL: https://docs.mountaya.com/en/apis/map-studio/reference/sessions/
> Last updated: 2026-11-26
> Rules for agents: https://docs.mountaya.com/llms.txt#rules-for-agents

Exchange a secret key for a short-lived session token, naming the publishable key the session is bound to and, for a page that acts as a member, the user it acts as.

 A session token is what a browser surface authenticates with, and what decides whether a read is served the draft or the published snapshot; the secret key that minted it never reaches the page. There is no refresh operation: a session lives at most five minutes and a client mints a fresh one.

## The Session object

A short-lived session, minted by exchanging a secret key for one and bound to the publishable key named in the request, to the surface named there, and to the user named there, if any. Its `sess_` token is what a browser surface authenticates with, what decides whether a read is served the draft or the published snapshot, and, when it names a user, what lets a request act as that member.

This is the session, not the credential. `SessionToken` is the name of the security scheme a request authenticates with; this schema is what `postSession` answers, and it carries that token at `token` alongside the expiry to read instead of assuming one.

- `expires_at` (string (date-time), required): When the token stops being accepted: at most five minutes after the request, and sooner when `postSession` returned a token it minted moments earlier for the same publishable key, surface and user. There is no refresh operation: a new one is minted from the caller's own backend before this passes. Read this field rather than assuming the five minutes, so a client never holds a token past its end.

- `user_id` (string (uuid) | null, required): The user the token names, the `user_id` the request asked for, or null for a token minted without one. A token naming a user is what lets a request act as that user: every operation whose `x-acting-user` is `required` refuses a token that names none with a `401`. A token naming none writes nothing and reads what the publishable key alone does, the published snapshot and public-only listings; one minted for `api` also makes the `management` reads, and one minted for `studio-embedded` does not.

- `token` (string, required): The session token. Send it back verbatim in `X-Session-Token`. It is OPAQUE to a client: the `sess_` prefix is the only part of it this contract fixes for one. Do not parse it, do not derive an expiry from it, and read `expires_at` beside it instead. Mountaya's own SDKs read the surface it was minted for from it, so an Embedded Studio refuses a token minted for another surface before it sends anything.

### An example Session

```json
{
  "token": "sess_3b81f6a04d27c95e18470fb2e6d3a95c7e02b184fd639a05",
  "expires_at": "2026-07-04T12:05:00Z",
  "user_id": "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1"
}
```

## Create a session token using a secret key

### POST /v1/sessions

- Operation: `postSession`
- Behavior: idempotent, safe to retry · answers within 30000 ms or times out

Creates a short-lived session token by exchanging a secret key and a publishable key. The `X-API-Key` header with an `sk_` value is required to authenticate this request. The token accompanies the publishable key it is minted against: every Data API request, every `management` operation and every `studio-embedded` operation require one beside the key.

Give it `surface` to say what the token is for. Omitted, it is `api`, the surface of your own clients. Every Embedded Studio you mount, the editor and its read-only viewer alike, needs a token minted for `studio-embedded`: it is the only token a `studio-embedded` operation accepts, a token minted for any other surface being a `401` there, and what the studio reaches with it on the Data API and the Tile API is recorded as `studio-embedded` usage and never counted toward the organization's limits, allowances or invoice. Such a token is minted only for a publishable key that reaches the `studio-embedded` scope, and a key that does not is a `403`, and only for an organization that holds Embedded Studio, one that does not being the `402` a `studio-embedded` operation answers. It names a member for the editor, or is minted without `user_id` for a viewer, which acts as nobody and, like every token minted without `user_id`, reads what the publishable key alone does. A viewer that must show drafts takes a token naming the member who looks. A token already minted stays valid for its whole life whatever the organization holds, but what it reaches on the Data API and the Tile API while the organization does not hold Embedded Studio is recorded and counted as `api` usage.

Give it `user_id` to mint a token naming one user, as your backend asserts after signing that user in to your own product: a member of the key's organization, or a user whose verified email address has a pending invitation to it. The token names that user for its whole life and no header can name anybody else, so a page holding it acts as that user and nobody else. The answer's `user_id` is the user the token names, or null for a token minted without one.

- A token naming a user is what acting as a user requires. Every operation whose `x-acting-user` is `required`, which is every `studio-embedded` operation, every `management` write and every operation under `/v1/me`, refuses with a `401` a token minted without `user_id`, and acts as the user the token names, answered only while that user holds the role it asks. A user whose invitation is still pending holds no role, and reaches only the operations under `/v1/me` marked `x-pending-invitee`.
- The session acts as that member with at most an admin's say over the organization's members. Any admin can create the secret key that mints a session, and the session can name any member, so the owner's own acts, granting, taking away or removing `admin`, inviting an admin, an admin leaving and handing the organization on, are refused under any session with a `403`, a session naming the owner included: the owner makes them signed in to Mountaya's web application.
- A token minted without `user_id` reads what the publishable key alone does, whatever surface it was minted for: the last published snapshot and never a draft, and public-only listings, and a backend that must read drafts mints for a member. It reaches no operation that acts as a user. Minted for `api` against a key that carries `management`, it reaches every `management` read with no member role asked, so such a token stays on your own backend; a token you hand to an anonymous visitor is minted for a key without `management`. Minted for `studio-embedded`, the `management` reads answer it the `401` a key alone gets.
- On a `management` read, a token naming a user is answered only while that user holds the role the read asks of a member of the organization, and is otherwise the `403` a member without that role gets.

A token is valid for at most five minutes. A token minted moments earlier for the same publishable key, the same surface and the same user, or for the same key and surface and no user, is returned again while it has more than two minutes to run, so read `expires_at` rather than assuming the full lifetime. Sending the same request twice is harmless for that reason: the second is answered the first one's token, or another for the same key and user, and every token stays valid until its own expiry.

An unknown, revoked or malformed secret key is a `401`, the same answer as sending none, and so is a key of an organization that has been canceled, so this operation cannot be used to learn which secret keys exist.

Five limits hold this operation, and each is answered `429` with `Retry-After`, the seconds left in the minute:

- Each client IP address, an IPv6 address counted by its `/64` prefix, may fail 60 times a minute. Only a request that fails counts toward it, so a backend minting tokens for many users from one address is never held to it while its requests succeed. A request Mountaya's MCP server forwards is counted against the address of the person it acts for, which the server forwards, rather than against the server's own, and that address may fail 6,000 times a minute there: an assistant reaches the MCP server from a few addresses its provider shares among all its users, so one organization's failing key would otherwise lock out every other organization's sessions behind the same address.
- Each secret key may fail 10 times a minute with a `401`, `403` or `404`. Past that it is refused for the rest of the minute, even on a request that is otherwise correct: a deployment pairing its secret key with a wrong or revoked publishable key locks that secret key out until the minute ends.
- A `403` for `user_id` counts toward a limit of its own instead, 10 a minute for that secret key and that `user_id`: a token asked for somebody who has left the organization never locks out the tokens asked for everybody else.
- A refusal for `surface` counts toward a limit of its own instead, 10 a minute for that secret key and that surface: the `403` of `mcp`, the `402` of `mcp` that Mountaya's MCP server is answered for an organization that does not hold the Data API, the `402` of `studio-embedded` for an organization that does not hold Embedded Studio and its `403` for a key that does not reach the `studio-embedded` scope. Pages asking for a `studio-embedded` token for an organization whose agreement does not include Embedded Studio never lock out the tokens the same secret key asks for any other surface.
- Each secret key mints at most 6000 tokens a minute. Only a token actually minted counts toward it, and a token returned again is free. This ceiling, not the failure limits, is what bounds a secret key that has leaked: its requests succeed, so no failure is counted, and it mints at most 6000 tokens a minute until it is revoked.

Requires no publishable-key scope of its own: the secret key is the credential, and the publishable key named in the body is the subject of the session rather than the caller. The calling-page refusal in the [order of checks](https://docs.mountaya.com/en/apis/conventions/errors.md#which-refusal-you-get-first) does not apply here.

Records no webhook event. Runs the task `session.create`.

Also takes `Accept-Language`, as every operation of this API does: the [overview](https://docs.mountaya.com/en/apis/map-studio/reference.md) describes it.

**Body**

The publishable key the session token is minted against, and the user it names, where it names one.

- `publishable_key` (string, in body, required): The publishable key value to bind the session to. Always the `pk_` prefix followed by 32 lowercase hexadecimal characters, 35 in all, the shape every key is minted with.

- `user_id` (string (uuid), in body, optional): The user the session acts as, which your backend asserts: a member of the publishable key's organization, or a user whose invitation to it is still pending, matched by an address the user's account has verified. The token is signed with it, and every operation whose `x-acting-user` is `required` acts as the user the token names, so a page holding the token acts as this user and nobody else. A `management` read under the token is answered only while this user holds the role that read asks of a member of the organization. The session governs the members with at most an admin's say, whoever it names: a session naming the owner is answered as an admin's on the owner's own acts, which are made only signed in to Mountaya's web application. A user who is neither a member nor invited answers `403`, the same answer whether or not the user exists, and nothing is minted.

  Omitted, the session is minted without `user_id`, and no operation whose `x-acting-user` is `required` accepts it. Whatever its surface, it reads what the publishable key alone does: the last published snapshot and never a draft, and public-only listings. Minted for `api` it also makes every `management` read its key's `management` scope allows, so a session minted without `user_id` for a key carrying `management` stays on your own backend, and one you hand to a page is minted for a key without `management`; minted for `studio-embedded`, for an Embedded Studio's read-only viewer, it makes no `management` read, which answers it `401`. Name a member when a backend or a viewer must read drafts. A malformed value, or the nil UUID, is a `400` naming the field.

- `surface` (string, in body, optional, one of: `api`, `mcp`, `studio-embedded`): The surface the session is minted for, which it keeps for its whole life. Omitted, it is `api`, the surface of your own clients.

  Ask for `studio-embedded` for every Embedded Studio you mount, the editor and its read-only viewer alike: it is the only session a `studio-embedded` operation accepts, and what the studio then reaches on the Data API and the Tile API is recorded as `studio-embedded` usage and never counted toward the organization's limits, allowances or invoice. It is minted only for a publishable key that reaches the `studio-embedded` scope, and a key that does not is a `403`, and only for an organization that holds Embedded Studio, one that does not being the `402` a `studio-embedded` operation answers. It may name a member, for the editor, or be minted without `user_id`, for a viewer that only reads, which is then served what the publishable key alone is: published content and never a draft, and the organization's private resources only by their id, never in a listing.

  `mcp` is minted only for Mountaya's MCP server, and asked for here it is a `403`.

**Response** `200`

The session token, the user it names, and its expiry. Send the token in `X-Session-Token` beside the publishable key it was minted against. It may be a token minted moments earlier for the same key, surface and user, returned again with less of its lifetime left.

- `extensions` (object): Top-level extensions map for 2xx responses. Mirrors the GraphQL-spec response extensions: a free-form map for response-scoped metadata.
  - `metadata` (object): Response-scoped metadata. Its shape is per-operation: the operation that emits it names the schema, and an operation that documents none never emits this field.

- `data` (Session, required): A short-lived session, minted by exchanging a secret key for one and bound to the publishable key named in the request, to the surface named there, and to the user named there, if any. Its `sess_` token is what a browser surface authenticates with, what decides whether a read is served the draft or the published snapshot, and, when it names a user, what lets a request act as that member.

  This is the session, not the credential. `SessionToken` is the name of the security scheme a request authenticates with; this schema is what `postSession` answers, and it carries that token at `token` alongside the expiry to read instead of assuming one.

  See [the Session object](https://docs.mountaya.com/en/apis/map-studio/reference/sessions.md#session-object).

**Errors** `400`, `401`, `402`, `403`, `404`, `413`, `429`, `500`, `504`. See [Errors](https://docs.mountaya.com/en/apis/conventions/errors.md).

- `400`: Bad Request
- `401`: Unauthorized
- `402`: A plain `PAYMENT_REQUIRED`: `surface` is `studio-embedded` and the organization does not hold Embedded Studio, the product a `studio-embedded` session runs, which its agreement with Mountaya grants. It is the `402` every `studio-embedded` operation answers for the same organization, so a session is not minted for a studio that could not use it. The product is asked before the key's scope, so a key that does not reach the `studio-embedded` scope either is answered this rather than a `403`. Nothing is minted, and the refusal counts toward the limit of that secret key and that surface rather than the secret key's own. It clears when the agreement changes, not when the request does. A session for `api` is never refused this way, and one for `mcp` only when Mountaya's MCP server asks for it, for an organization that does not hold the Data API, the product `mcp` belongs to, by its agreement or by its card; any other caller asking for `mcp` is the `403`.
- `403`: The credential is valid and this operation is not open to it: the publishable key named in the body is not one this secret key's organization issued, `surface` is `studio-embedded` and that key does not reach the `studio-embedded` scope, `surface` is `mcp`, which is minted for Mountaya's MCP server alone, or `user_id` matches no member of that organization and no user whose verified email address has a pending invitation to it. A `user_id` that matches no user and one naming somebody outside the organization answer alike, so this cannot be used to learn which user ids exist. Nothing is minted. A refusal for `user_id` counts toward the limit of that secret key and that `user_id`, one for `surface` toward the limit of that secret key and that surface, and one for the publishable key toward the secret key's own.
- `404`: The publishable key named in the body does not exist, or is revoked. A key that exists but belongs to another organization is a 403 rather than this, so this status says the key is unknown and nothing about who owns it.
- `413`: Payload Too Large
- `429`: `TooManyRequests`, given in full at the end of this page.
- `500`: Internal Server Error
- `504`: Gateway Timeout

**cURL**

```bash
curl -s https://api.mountaya.com/v1/sessions \
  -H "X-API-Key: sk_your_secret_key" \
  -H "Content-Type: application/json" \
  -d '{"publishable_key": "pk_your_publishable_key", "user_id": "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1"}'
```

**TypeScript**

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

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

const { data } = await admin.sessions.postSession({
  body: {
    publishable_key: "pk_your_publishable_key",
    user_id: "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1",
  },
});

console.log(data);
```

**Python**

```python
import requests

response = requests.post(
    "https://api.mountaya.com/v1/sessions",
    headers={
        "X-API-Key": "sk_your_secret_key",
    },
    json={
        "publishable_key": "pk_your_publishable_key",
        "user_id": "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1",
    },
    timeout=30,
)
response.raise_for_status()
print(response.json()["data"])
```

**cURL: Mint a session token for an Embedded Studio's read-only viewer**

```bash
curl -s https://api.mountaya.com/v1/sessions \
  -H "X-API-Key: sk_your_secret_key" \
  -H "Content-Type: application/json" \
  -d '{"publishable_key": "pk_your_publishable_key", "surface": "studio-embedded"}'
```

Response 200:

```json
{
  "data": {
    "token": "sess_3b81f6a04d27c95e18470fb2e6d3a95c7e02b184fd639a05",
    "expires_at": "2026-07-04T12:05:00Z",
    "user_id": "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1"
  }
}
```

**Error answers the operations of this page share**

- `TooManyRequests` (`429`): A limit on this operation's requests is spent: the [rate limits](https://docs.mountaya.com/en/apis/conventions/rate-limits.md) page lists every one, the unknown publishable keys one address may present among them, and the operation's description names any of its own. The request was counted and nothing was done. `Retry-After` says how long until the limit lifts; wait that long, with a little jitter of your own, and send the request again. Where the operation takes an `Idempotency-Key`, a write sent again with the same key runs once.
