Skip to content

From the browser

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

Applies to EngineCloud
View as Markdown
On this page
  1. 1. Through your server
  2. 2. With a browser token
  3. What a browser token cannot do

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:

ask-box.tsx
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
const { token, expiresAt } = await ai.browserTokens.create({ endUser: user.id, tasks: ['allergen-answer'] });

What a browser token cannot do

Use a task without browser: truerefused, 403 task_not_allowed
Replace the route’s instructionsnever: its own messages come after them
Go past the person’s limita browser route must have limits.end_user; past it, 429 end_user_limit
Offer toolsrefused: tools run on your server
Be used from another siteonly the origins listed for the project are accepted

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

  1. 1. Through your server
  2. 2. With a browser token
  3. What a browser token cannot do

    Search pages, sections and terms