# From the browser

> Let your app's pages call AI without ever holding a secret key, with a limit for each person.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://ai.docs.sypnia.com/using/from-the-browser/

For agents: Secret keys (syp_sk_…) are server-only; a request with an Origin header and a secret key is refused (403 secret_key_in_browser). Two ways for browsers: (1) through your server (recommended for anything sensitive); (2) a browser token syp_ait_… valid 10 minutes, for one project, one end user and only routes with browser: true: minted by exchanging the end user's Sypnia Data session (POST /v1/browser-tokens with the publishable key syp_pk_… + the Data access token) or by your server for apps without Data (POST /v1/browser-tokens with the secret key and end_user). Browser routes must set limits.end_user; instructions locked; tools only those the route allows and run in the browser are not offered (tools need your server). SDK @sypnia/ai/browser createAI({ project, getToken }) refreshes the token. CORS: allowed origins per project.

A secret key gives full use of your project's AI. It never goes in a web page: anyone can read a page's code. The gateway refuses a secret key sent from a browser with `403 secret_key_in_browser`.

There are two safe ways.

## 1. Through your server

The page calls your server; your server calls Sypnia AI with the secret key. This is the way for anything that uses tools, private data or long instructions.

## 2. With a browser token

A **browser token** is a short pass for one person: it lasts **10 minutes**, belongs to one project and one person of your app, and works only for tasks whose route says `browser: true`.

If your app signs people in with [Sypnia Data](https://data.docs.sypnia.com/), the browser gets one by showing the person's session:

`ask-box.tsx`

```ts
import { createAI } from '@sypnia/ai/browser';
import { data } from './sypnia';

const ai = createAI({
  project: 'la-espiga',
  getToken: () => data.auth.session(),   // the SDK exchanges it for a browser token and refreshes it
});

const stream = await ai.chat({ task: 'allergen-answer', case: 'ask-about-a-product', messages, stream: true });
```

Without Sypnia Data, your server makes the token for the person it signed in:

`server: /api/ai-token`

```ts
const { token, expiresAt } = await ai.browserTokens.create({ endUser: user.id, tasks: ['allergen-answer'] });
```

## What a browser token cannot do

|                                    |                                                                                |
| ---------------------------------- | ------------------------------------------------------------------------------ |
| Use a task without `browser: true` | refused, `403 task_not_allowed`                                                |
| Replace the route's instructions   | never: its own messages come after them                                        |
| Go past the person's limit         | a browser route **must** have `limits.end_user`; past it, `429 end_user_limit` |
| Offer tools                        | refused: tools run on your server                                              |
| Be used from another site          | only the origins listed for the project are accepted                           |

Allowed origins are set per project: `npx @sypnia/ai origins add https://laespiga.co`.
