From the browser
Let your app's pages call AI without ever holding a secret key, with a limit for each person.
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, the browser gets one by showing the person’s session:
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:
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.