# Signed visitor tokens

Sign a reader's claims on your server so adaptive content can protect pages, not just personalize them.

[Adaptive content](../adaptive-content) reads **claims** about the reader — their plan, role, region. Claims
set in a cookie or URL personalize pages, but anyone can change them. A **signed visitor token** is a JWT your
backend signs with the site's key: Docsheaf trusts it, and with **Trust only signed claims** on, conditions
become access rules.

## \1. Create a signing key

In the site's **Customize → AI & features → Adaptive content**, create a signing key. It's shown once —
store it as a secret on your server. Creating a new key, or deleting it, invalidates every token signed
with the old one.

## \2. Sign the reader's claims

A token is a compact JWT:

- header `{ "alg": "HS256", "typ": "JWT" }` — no other algorithm is accepted;
- payload: the reader's claims as top-level fields, plus `exp` (required) and optionally `nbf`;
- signature: HMAC-SHA256 with the key, used as the text it is.

Up to 30 claims. Names are letters, digits and `_` (up to 40, not starting with a digit); values are
strings, numbers, booleans, `null`, or lists of those.

{% tabs %}
{% tab title="Node.js" %}
```js
import { createHmac } from 'node:crypto';

const b64url = (value) => Buffer.from(JSON.stringify(value)).toString('base64url');

export function visitorToken(claims, key, ttlSeconds = 3600) {
  const body = `${b64url({ alg: 'HS256', typ: 'JWT' })}.${b64url({ ...claims, exp: Math.floor(Date.now() / 1000) + ttlSeconds })}`;
  return `${body}.${createHmac('sha256', key).update(body).digest('base64url')}`;
}

// visitorToken({ plan: 'enterprise', roles: ['admin'] }, process.env.DOCSHEAF_CLAIMS_KEY)
```
{% endtab %}

{% tab title="Python" %}
```python
import jwt, time, os  # PyJWT

def visitor_token(claims, ttl=3600):
    return jwt.encode({**claims, "exp": int(time.time()) + ttl},
                      os.environ["DOCSHEAF_CLAIMS_KEY"], algorithm="HS256")
```
{% endtab %}
{% endtabs %}

## \3. Hand the token to the docs

{% tabs %}
{% tab title="Link" %}
Link to the docs with the token:

```
https://docs.example.com/billing?visitor_token=<jwt>
```

Docsheaf moves it into a secure, httpOnly cookie for that site (until the token expires, at most 30 days)
and redirects to the same address without it, so it never stays in browser history or referrers.
{% endtab %}

{% tab title="Cookie" %}
If your app and your docs share a parent domain, set a `docsheaf-visitor-token` cookie on the docs domain
with the JWT as its value.
{% endtab %}
{% endtabs %}

## What changes with signed claims only

A token's claims always win over unsigned ones. When the site trusts **only** signed claims:

- a page whose condition — or an ancestor's — fails for the reader answers "not found" on the site, as
  `.md`, and through the MCP server;
- search returns only pages the reader may see, and matches and quotes only text they can see;
- the AI assistant leaves out what the reader can't see.

{% hint style="info" %}
`llms.txt`, `llms-full.txt`, the sitemap and the MCP server always show what a reader without claims sees.
{% endhint %}
