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

# Identity Verification

> Prove who a logged-in visitor is, so your AI agent can trust their user ID, email and phone

Anyone can open the browser console on your website and tell the chat widget they are someone else. Identity verification closes that gap: your server signs a short-lived token for the logged-in customer, the widget passes it along, and botBrains marks the user ID, email and phone from that token as verified. [Audiences](https://docs.botbrains.io/concepts/audiences.md) can then require verified fields, so a [procedure](https://docs.botbrains.io/concepts/procedures.md) that changes an order or reads account data only runs for verified customers.

## How it works

1. You create an identity secret for your project in the platform. Only your server knows it.
2. When a customer is logged in, your server signs a token (a JSON Web Token) with that secret.
3. Your page hands the token to the widget. botBrains checks the signature and the expiry, then identifies the customer from it.

## Create the identity secret

Open [Identity verification](https://platform.botbrains.io/~/settings/identity-verification) in your project settings and click **Generate secret**. The platform shows the secret once, together with its **Key ID (kid)**, so store both in your server's secret store right away. Generating, rotating and deleting the secret needs the permission to manage API keys.

| Action | What happens |
| - | - |
| Generate secret | Creates the secret and its key ID. botBrains accepts tokens signed with it right away. |
| Rotate secret | Replaces the secret and key ID. Tokens signed with the old secret stop working immediately, so update your server right after rotating. |
| Delete | Removes the secret. botBrains rejects every token until you generate a new one. |

> [!WARNING]
>
> Keep the identity secret on your server. Anyone who has it can sign in to the widget as any of your customers. Never put it in your website's code or send it to the browser.

## Sign a token on your server

Sign the token with HS256, put the key ID in the token header as `kid`, and keep its lifetime short: a few minutes is enough.

```javascript Node.js theme={null}
import jwt from "jsonwebtoken";

const token = jwt.sign(
  {
    sub: String(user.id),
    email: user.email,
    first_name: user.firstName,
    aud: "PROJECT_ID",
  },
  process.env.BOTBRAINS_IDENTITY_SECRET,
  { algorithm: "HS256", keyid: process.env.BOTBRAINS_IDENTITY_KID, expiresIn: "10m" },
);
```

```python Python theme={null}
import os
from datetime import datetime, timedelta, timezone

import jwt

token = jwt.encode(
    {
        "sub": str(user.id),
        "email": user.email,
        "first_name": user.first_name,
        "aud": "PROJECT_ID",
        "exp": datetime.now(timezone.utc) + timedelta(minutes=10),
    },
    os.environ["BOTBRAINS_IDENTITY_SECRET"],
    algorithm="HS256",
    headers={"kid": os.environ["BOTBRAINS_IDENTITY_KID"]},
)
```

The Node.js example uses [jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken), the Python example [PyJWT](https://pypi.org/project/PyJWT/). Replace `PROJECT_ID` with your project's ID, the number in your platform URL.

| Claim | Required | Description |
| - | - | - |
| `sub` | Yes | Your ID for the customer, 16 to 256 characters. botBrains uses it as the user's external ID. |
| `exp` | Yes | When the token expires. botBrains rejects expired tokens. |
| `email` | No | The customer's email address. |
| `phone` | No | The customer's phone number. |
| `first_name` | No | The customer's first name. |
| `last_name` | No | The customer's last name. |
| `aud` | Recommended | Your project ID as a string. With it, a token for one project is never accepted by another. |

If your user IDs are shorter than 16 characters, derive a longer one that stays stable, for example `customer-000000042` for the user with ID 42. Never change it afterwards: botBrains recognizes the customer by it across visits and devices.

Each claim in the token replaces the matching value you set in the browser, and the user ID always comes from `sub`. Fields the token doesn't carry keep the value you set in the browser and stay unverified, just like custom attributes you pass with `user.data`.

## Pass the token to the widget

Send the token from your server to the page and pass it to the [Web SDK](https://docs.botbrains.io/concepts/web-sdk.md) with `user.data`:

```javascript theme={null}
$botbrains.push(["set", "user.data", { userJwt: token }]);
```

The widget takes this command right away, so its very first request already carries the token. botBrains identifies the customer by the token's `sub`. Other fields you set, such as `external_attributes`, stay unverified.

`user.identify` also accepts `userJwt`, but it only runs once the widget has loaded, so the widget's first request goes out without the token.

Tokens expire after a few minutes, and the widget doesn't fetch new ones itself. Replace the token before it expires, for example on a timer slightly shorter than the token's lifetime. In this example, `/api/botbrains-token` stands for your own endpoint that signs a new token for the logged-in customer:

```javascript theme={null}
// Tokens live 10 minutes; replace them every 9.
setInterval(async () => {
  const response = await fetch("/api/botbrains-token");
  $botbrains.push(["set", "user.data", { userJwt: await response.text() }]);
}, 9 * 60 * 1000);
```

If your page already receives a fresh session token on its own, pass a new `userJwt` at the same moment instead.

When a different customer logs in on the same page, call `user.logout` before you pass their token. This clears the previous customer's chat and data from the widget:

```javascript theme={null}
$botbrains.push(["do", "user.logout"]);
$botbrains.push(["set", "user.data", { userJwt: otherToken }]);
```

The widget keeps the token in memory only, never in cookies or local storage, and sends it with every request. `user.logout` and `reset` forget it.

On frames that ask visitors to sign in, botBrains identifies the customer from that sign-in and ignores the token.

## Use verified fields

Each identified user records which fields botBrains verified. Use them in an [audience](https://docs.botbrains.io/concepts/audiences.md) to limit sensitive procedures to verified customers:

| Field | Verified when |
| - | - |
| `user.external_id_signed` | The token carried the user ID (`sub`), which every token does. |
| `user.email_signed` | The token carried the email address. |
| `user.phone_signed` | The token carried the phone number. |

> [!WARNING]
>
> A verified user ID proves who the customer is, not that the order or account they ask about is theirs. When an action reads or changes a customer's data, look it up through the verified user ID or email, or check in your system that the requested order belongs to that customer. Otherwise a verified customer can ask about someone else's order number.

## Troubleshooting

When a token is invalid, botBrains returns an error and the widget doesn't identify the customer.

| Error | Cause | Fix |
| - | - | - |
| `user_jwt_expired` | The token's `exp` lies in the past, so the widget's requests fail. | Replace the token from your page before it expires, as shown in [Pass the token to the widget](#pass-the-token-to-the-widget). |
| `invalid_user_jwt` | The signature, key ID, algorithm, audience or a claim doesn't match. The error message names the problem. | Check that the token uses HS256, the current secret and its `kid`, that `aud` is your project ID, and that `sub` has at least 16 characters. After a rotation, deploy the new secret to every server that signs tokens. |

## Next steps

> [card] [Identify users](https://docs.botbrains.io/guides/users.md)
>
> Decide how to identify customers and what your AI agent can do for them

> [card] [Audiences](https://docs.botbrains.io/concepts/audiences.md)
>
> Limit procedures to verified customers


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