Vite
Use Baseera in a Vite React app, with a small server that signs user tokens.
A Vite app is a browser app, so it has no server of its own to sign tokens. Run a small server next to it (Express, Hono, a serverless function) and point getUserToken at it.
The provider
import "@baseera/ui/baseera.css";
import { createRoot } from "react-dom/client";
import { BaseeraProvider } from "@baseera/react";
import { BaseeraSurface } from "@baseera/ui/connected";
import { App } from "./App";
async function getUserToken(): Promise<string> {
const res = await fetch("/api/baseera-token", { method: "POST" });
if (!res.ok) throw new Error(`The token route answered ${res.status}.`);
return ((await res.json()) as { token: string }).token;
}
createRoot(document.getElementById("root")!).render(
<BaseeraProvider
publishableKey={import.meta.env.VITE_BASEERA_KEY}
baseUrl={import.meta.env.VITE_BASEERA_URL}
getUserToken={getUserToken}
theme="system"
>
<App />
<BaseeraSurface locale="ar" />
</BaseeraProvider>,
);VITE_BASEERA_KEY=pk_your_publishable_key
VITE_BASEERA_URL=https://runtime.example.comOnly the publishable key belongs in a VITE_ variable. Vite inlines every VITE_ variable into the bundle, so a secret key there would be public.
The token server
This example uses Express. Keep the secret key in the server's environment.
import express from "express";
import { signUserToken } from "@baseera/node";
const app = express();
app.use(express.json());
app.post("/api/baseera-token", async (req, res) => {
const user = await getCurrentUser(req); // your own session check
if (!user) return res.status(401).json({ error: "unauthorized" });
const token = await signUserToken(
{ userId: user.id, tenantId: user.orgId, name: user.name, locale: "ar" },
process.env.BASEERA_SECRET_KEY!,
);
res.set("cache-control", "no-store").json({ token });
});
app.listen(3001);Proxy it in development
Forward /api to the token server so the browser calls the same origin:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
server: { proxy: { "/api": "http://localhost:3001" } },
});In production, serve both from the same origin, or allow the origin of your app in the token server's CORS settings. Add your app's domain to the agent's allowed domains in the console, or the runtime refuses the requests.
Tools and context
Hooks work as in any React app. Mount a component that registers tools anywhere under the provider:
import { useBaseeraTool } from "@baseera/react";
import { z } from "zod";
const input = z.object({ number: z.string() });
export function InvoiceTools() {
useBaseeraTool({
name: "getInvoice",
description: "Get an invoice by its number",
kind: "read",
input,
run: ({ number }) => fetch(`/api/invoices/${encodeURIComponent(number)}`).then((r) => r.json()),
});
return null;
}