Cartesiandocs

React SDK

Installation

Add the Cartesian React SDK to a web app and connect it to your backend.

Requirements

Minimum
React18.2 or 19
BrowsersCurrent 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-react
bash

It has three entry points:

ImportContents
@cogitx/cartesian-reactThe UI: CartesianProvider, CartesianChat, CartesianPanel, CartesianAskButton, CartesianAskBar, useCartesian(). It also re-exports everything from core.
@cogitx/cartesian-react/coreThe headless client. No React.
@cogitx/cartesian-react/styles.cssOne 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.tgz
bash

Add the styles

Import the stylesheet once, for example in your entry file:

import "@cogitx/cartesian-react/styles.css";
tsx

Every 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> with http changed to ws, plus /ws
import { CartesianClient } from "@cogitx/cartesian-react";

export const cartesian = new CartesianClient({
  vertical: "grocery",                 // or "apparel"
  baseUrl: "https://assistant.freshmart.com",
});
tsx

How the session token is sent

VerticalRESTWebSocket
ApparelAuthorization: Bearer <token>Subprotocol bearer.<token>
GroceryAuthorization: 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 turn Authorization: Bearer <token> into Cookie: grocery_session=<token>.
  • WebSocket: accept the upgrade on /ws, read the token from the bearer.<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

vertical"grocery" | "apparel"required

Which backend protocol to speak.

baseUrlstringrequired

Your Cartesian backend, or your proxy in front of it.

apiUrlstringdefault: baseUrl + /api

Override the REST base, for example to route through a proxy path.

socketUrlstringdefault: ws(s)://… /ws

Override the WebSocket URL.

fetch(url, init) => Promise<Response>

Replace fetch, for example to add headers or route through your own transport.

webSocket({ url, token, vertical, protocols }) => WebSocketLike

Create the socket yourself. The default is new WebSocket(url, ["bearer." + token]).

credentialsRequestCredentials

Passed to fetch. Use "include" if your proxy uses its own cookies.

headersRecord<string, string>

Extra headers on every REST request.

licenseKeystring

Your white-label licence. See Customize.

appIdstringdefault: location.hostname

The name the licence is checked against.

sessionStoreSessionStoredefault: localStorage

Where the session token is kept between page loads. Use MemorySessionStore to keep it only in memory.

logger(message: string) => void

Receives internal diagnostics.

Next: Quickstart.