React SDK
Installation
Add the Cartesian React SDK to a web app and connect it to your backend.
Requirements
| Minimum | |
|---|---|
| React | 18.2 or 19 |
| Browsers | Current Chrome, Edge, Firefox and Safari (ES2020, WebCrypto, WebSocket) |
| Node (tests, SSR) | 18 |
The package does not need Tailwind or any other CSS framework.
Add the package
npm install @cogitx/cartesian-reactbashIt has three entry points:
| Import | Contents |
|---|---|
@cogitx/cartesian-react | The UI: CartesianProvider, CartesianChat, CartesianPanel, CartesianAskButton, CartesianAskBar, useCartesian(). It also re-exports everything from core. |
@cogitx/cartesian-react/core | The headless client. No React. |
@cogitx/cartesian-react/styles.css | One stylesheet for the UI. |
Preview: the package is not on npm yet. Build it from the Cartesian repo and install it from a folder or a tarball:
cd sdk/react && npm ci && npm run build && npm pack
npm install ./cogitx-cartesian-react-0.1.0.tgzbashAdd the styles
Import the stylesheet once, for example in your entry file:
import "@cogitx/cartesian-react/styles.css";tsxEvery rule is scoped under the .cartesian-root class, so it does not change your own pages. The stylesheet also declares the Spline Sans and IBM Plex Serif fonts under their own names (Cartesian Spline Sans, Cartesian Plex Serif). If your bundler does not copy font files from node_modules, the UI falls back to the system font.
Connect to your backend
Each partner runs its own Cartesian backend. The SDK only talks to the baseUrl you give it:
- REST:
<baseUrl>/api - WebSocket:
<baseUrl>withhttpchanged tows, plus/ws
import { CartesianClient } from "@cogitx/cartesian-react";
export const cartesian = new CartesianClient({
vertical: "grocery", // or "apparel"
baseUrl: "https://assistant.freshmart.com",
});tsxHow the session token is sent
| Vertical | REST | WebSocket |
|---|---|---|
| Apparel | Authorization: Bearer <token> | Subprotocol bearer.<token> |
| Grocery | Authorization: Bearer <token> | Subprotocol bearer.<token> |
On Android the grocery token is sent as the grocery_session cookie. A browser cannot do that: scripts cannot set a Cookie header on a cross-origin request, and the backend's cookie is not shared with your site. The SDK also adds the Cookie header on grocery requests, but only non-browser runtimes such as Node send it.
The grocery backend reads the session from the grocery_session cookie only, and it does not send CORS headers. To use the grocery vertical from a browser, put a same-origin proxy in front of it:
- REST: forward
/api/*and turnAuthorization: Bearer <token>intoCookie: grocery_session=<token>. - WebSocket: accept the upgrade on
/ws, read the token from thebearer.<token>subprotocol, echo that subprotocol back, and open the upstream socket with the cookie.
The Cartesian web app does this in apps/web/server.mjs, and the SDK example has a dev version in sdk/react/example/vite.config.ts. Then set baseUrl to your own origin.
For the apparel vertical, the backend must allow your origin with CORS and accept the Authorization header.
Config options
Which backend protocol to speak.
Your Cartesian backend, or your proxy in front of it.
Override the REST base, for example to route through a proxy path.
Override the WebSocket URL.
Replace fetch, for example to add headers or route through your own transport.
Create the socket yourself. The default is new WebSocket(url, ["bearer." + token]).
Passed to fetch. Use "include" if your proxy uses its own cookies.
Extra headers on every REST request.
Your white-label licence. See Customize.
The name the licence is checked against.
Where the session token is kept between page loads. Use MemorySessionStore to keep it only in memory.
Receives internal diagnostics.
Next: Quickstart.