> ## Documentation Index
> Fetch the complete documentation index at: https://developer.maatstays.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Exchange your API key for a short-lived, workspace-scoped token, and send the token.

Authentication is one exchange: you hold a long-lived API key
(`maat_partner_…`), you swap it at [`POST /v1/token`](https://developer.maatstays.com/api-reference/authentication/exchange-your-api-key-for-a-token)
for a short-lived bearer token that is scoped to exactly one Maat workspace, and you send
that token in the `Authorization` header on every call. That is the whole model.

The exchange has a reference page of its own now, with the full response shape and every
error it can return. Its `401` and `503` are problem documents like everything else in this
API — see [Errors](https://developer.maatstays.com/guides/errors.md#there-is-no-exception).

```bash cURL theme={null}
# 1. Exchange the key for a token.
curl -X POST https://maatstays.com/v1/token \
  -H "Authorization: Bearer $MAAT_PARTNER_KEY"

# → 200
# {
#   "access_token": "eyJhbGciOiJIUzI1NiIs…",
#   "token_type": "Bearer",
#   "expires_in": 900,
#   "workspace_id": "3f6b0f2e-1c3a-4d5e-9a70-8b2c4d6e0f11",
#   "sandbox": true,
#   "scope": ["partner:token.issue", "catalog:write", "calendar:read",
#             "calendar:write", "webhooks:manage"]
# }

# 2. Call the API with the token.
curl https://maatstays.com/v1/blocks \
  -H "Authorization: Bearer $MAAT_PARTNER_TOKEN"
```

```javascript Node.js theme={null}
const keyExchange = await fetch('https://maatstays.com/v1/token', {
    method: 'POST',
    headers: { Authorization: `Bearer ${process.env.MAAT_PARTNER_KEY}` },
});

if (!keyExchange.ok) {
    // One indistinguishable 401 covers an unknown key, a revoked key
    // and a wrong secret.
    throw new Error(`token exchange failed: ${keyExchange.status}`);
}

const { access_token: token, expires_in: expiresIn } = await keyExchange.json();

// Use the token for a batch of work, then let it expire.
const res = await fetch('https://maatstays.com/v1/blocks', {
    headers: { Authorization: `Bearer ${token}` },
});
console.log(res.status, expiresIn);
```

```python Python theme={null}
import os

import requests

exchange = requests.post(
    "https://maatstays.com/v1/token",
    headers={"Authorization": f"Bearer {os.environ['MAAT_PARTNER_KEY']}"},
    timeout=30,
)
exchange.raise_for_status()
token = exchange.json()["access_token"]

# Use the token for a batch of work, then let it expire.
res = requests.get(
    "https://maatstays.com/v1/blocks",
    headers={"Authorization": f"Bearer {token}"},
    timeout=30,
)
print(res.status_code)
```

```go Go theme={null}
package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "os"
)

func main() {
    req, _ := http.NewRequest(http.MethodPost,
        "https://maatstays.com/v1/token", nil)
    req.Header.Set("Authorization", "Bearer "+os.Getenv("MAAT_PARTNER_KEY"))

    res, err := http.DefaultClient.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    if res.StatusCode != http.StatusOK {
        log.Fatalf("token exchange failed: %d", res.StatusCode)
    }

    var body struct {
        AccessToken string `json:"access_token"`
        ExpiresIn   int    `json:"expires_in"`
    }
    if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
        log.Fatal(err)
    }

    // Use body.AccessToken for a batch of work, then let it expire.
    fmt.Println("token expires in", body.ExpiresIn, "seconds")
}
```

All examples in these docs use `https://maatstays.com/v1` — the one host this contract is
published against. Whether your requests land on a sandbox or a production workspace is
decided by your key, never by the hostname.

## What the response tells you

```json theme={null}
{
    "access_token": "eyJhbGciOiJIUzI1NiIs…",
    "token_type": "Bearer",
    "expires_in": 900,
    "workspace_id": "3f6b0f2e-1c3a-4d5e-9a70-8b2c4d6e0f11",
    "sandbox": true,
    "scope": ["partner:token.issue", "catalog:write", "calendar:read", "calendar:write"]
}
```

| Field | What it is |
| :- | :- |
| `access_token` | The bearer token. Send it on every other call. |
| `expires_in` | Seconds. Fifteen minutes by default, never more than an hour. |
| `workspace_id` | The one workspace this token addresses. |
| `sandbox` | **Which kind of workspace that is.** `true` is a sandbox; `false` is production. This is the only place that answer is published — see [Sandbox](https://developer.maatstays.com/guides/sandbox.md). |
| `scope` | What the key held at exchange time, echoed for your own reading — never what authorizes a request. |

Read `sandbox` at least once, in whatever you build. It is how an integration proves it is
pointed where its author thought it was: every operation does real work on either kind, so a
key exchanged by mistake writes to the wrong workspace rather than failing.

## The key

* Your key looks like `maat_partner_a1b2c3d4e5f60718_…` and is **shown once**, when it
  is issued. Store it in a secret manager; we hold only a hash and cannot show it again.
* Every key is issued for **exactly one workspace** — the container a portfolio's
  properties and calendars live in — and that workspace is either a sandbox or a production
  one. Nothing you send can change that — see [Sandbox](https://developer.maatstays.com/guides/sandbox.md).
* The key opens exactly one door: the token exchange. It never authenticates a contract
  endpoint directly.
* The exchange takes an **empty body, on purpose**. The workspace, the environment and
  your identity all come from the key itself; anything you put in the body is ignored.

## The token

* Tokens live for **fifteen minutes by default and never more than an hour**
  (`expires_in` tells you exactly). Mint one per batch of work, not one per request —
  the exchange has its own, separate rate ceiling.
* Store the **key**, mint tokens from it. Never persist a token, and never send one as
  a cookie or in a query string — the API reads the `Authorization` header and nothing
  else.
* **Revocation does not wait for expiry.** We re-check the key behind a token on every
  request, so revoking a key kills its outstanding tokens on their next call.

## Scopes

A key is issued with a fixed list of **scopes**, and **every operation in the API reference
names the one scope it requires**, as `x-maat-scope` on the operation. It is checked in CI
against the route table the gateway enforces, so it cannot silently disagree with what your
key needs.

`x-maat-scope` is the **floor**. Four operations have a second requirement that depends on
the path rather than on the operation, and the whole vocabulary — all eighteen scopes, a
least-privilege table by job, and what an issuer must hold to grant each one — is
[Scopes](https://developer.maatstays.com/guides/scopes.md).

A token minted from a key that lacks the scope is refused with `403 insufficient_scope`, and
unlike the `401` this one **names the missing scope** in `detail`: it is your own credential,
and the fix is on your side — a key that carries it, which is a new key
([Get access](https://developer.maatstays.com/guides/developer-access.md)).

* Scopes are fixed **when the key is issued**. They cannot be widened later; a key that
  needs more is a new key.
* They are **re-read from the key on every request**, like revocation. A key narrowed
  today is narrowed on its next call, before any outstanding token expires.
* The token's `scope` is what the key held at exchange time, echoed for your own
  reading — decode it to see what a token was issued for, never to decide what to send.
* `partner:token.issue` opens the exchange itself and nothing else. It is on every
  key; on its own it reaches no operation.

Every key issued before scopes existed was given exactly the scopes that equal what
its tokens could already do, so nothing that worked yesterday is refused today.

## When it is refused

A refused exchange is one **indistinguishable `401`** whether the key is unknown,
revoked, or the secret is wrong — telling those apart would let anyone probe which
credentials exist. If a key that worked yesterday is refused today, the answer is on our
side: see [Get access](https://developer.maatstays.com/guides/developer-access.md).

> [!NOTE]
>
> The token endpoint answers its refusals the way every other operation does: as an
> `application/problem+json` document with a `code` to branch on — see [Errors](https://developer.maatstays.com/guides/errors.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.