TypeScript SDK

Call your tasks from TypeScript with a generated, type-safe client.

The Rightbrain SDK adds your tasks to a TypeScript or JavaScript app as fully typed functions. The rightbrain CLI generates a client from the tasks in your project, so inputs and outputs are checked at compile time — no manual schema handling.

Use the SDK for TypeScript, Node, and Next.js apps. For other languages, or lightweight backend scripts, call the REST API directly.

Install

The published package is @rightbrain/sdk.

Terminal
$npm install @rightbrain/sdk
$# or: pnpm add @rightbrain/sdk
$# or: yarn add @rightbrain/sdk

Prerequisites: Node.js 18+, a package manager, and a Rightbrain project.

Generate the typed client

The rightbrain CLI handles credentials and code generation in one flow — no manual setup:

Terminal
$npx rightbrain@latest login
$npx rightbrain@latest init

login authenticates you, then init creates an API key for the project, writes your credentials to .env automatically, and generates typed task runners in src/generated/index.ts. Each task in your project becomes a typed method, with an interface for its inputs and its structured output.

The environment variables the CLI populates (and the transport reads):

.env (written by the CLI)
$RB_ORG_ID=...
$RB_PROJECT_ID=...
$RB_API_KEY=...

DirectTransport also accepts a config object for additional fetch options. If you’d rather manage credentials yourself, see Authentication.

Choose a transport

The SDK talks to Rightbrain through a transport:

  • DirectTransport — server-side (Node, route handlers, scripts). Holds your API key and calls Rightbrain directly.
  • PublicTransport — client-side (browser). Routes calls through your own backend so the API key never reaches the browser.

Never ship an API key to the browser. In client-side code use PublicTransport and proxy the request through a server route that holds the key.

Next.js example

A server route handler uses DirectTransport and the generated Client, then your UI calls that route.

app/api/tasks/product-listing/route.ts
1import type { Tasks } from "@/generated";
2import { Client, DirectTransport } from "@rightbrain/sdk";
3
4const client = new Client<Tasks>({
5 transport: new DirectTransport({
6 apiKey: process.env.RB_API_KEY!,
7 orgId: process.env.RB_ORG_ID!,
8 projectId: process.env.RB_PROJECT_ID!,
9 }),
10});
11
12export async function POST(request: Request) {
13 const body = await request.json();
14 const result = await client["<task-id>"].run({
15 inputs: { product_name: body.product_name },
16 });
17 return Response.json(result.response);
18}

Each task is addressed by its ID on the client, and run({ inputs }) returns the typed result — result.response holds the task’s structured output.

A minimal React hook that calls the route:

hooks/useGenerateProductListing.ts
1import { useState } from "react";
2
3export function useGenerateProductListing() {
4 const [loading, setLoading] = useState(false);
5 const [data, setData] = useState<unknown>(null);
6
7 async function execute(inputs: { product_name: string }) {
8 setLoading(true);
9 const res = await fetch("/api/tasks/product-listing", {
10 method: "POST",
11 headers: { "Content-Type": "application/json" },
12 body: JSON.stringify(inputs),
13 });
14 setData(await res.json());
15 setLoading(false);
16 }
17
18 return { execute, data, loading };
19}

This hook is deliberately minimal and tied to one route. For a production-grade generic hook — pass it any task function and get typed data/error/isPending state plus request-abort handling — use use-task.ts from the SDK demo, which works with every task the generated client exposes.

Use it in a component:

components/ProductListingForm.tsx
1function ProductListingForm() {
2 const { execute, data, loading } = useGenerateProductListing();
3
4 return (
5 <form
6 onSubmit={(e) => {
7 e.preventDefault();
8 execute({ product_name: "Headphones" });
9 }}
10 >
11 <button disabled={loading}>{loading ? "Generating..." : "Generate"}</button>
12 {data && <pre>{JSON.stringify(data, null, 2)}</pre>}
13 </form>
14 );
15}

Demo app

The Rightbrain SDK demo is a working Next.js app. It shows DirectTransport in a server route, PublicTransport in the browser, and a standalone Node script — each running real tasks that appear in your dashboard’s Runs view.

SDK or REST?

Use caseRecommended
TypeScript / Node / Next.js appsSDK — typed runners, ergonomic, transports
Other languages or frameworksREST API
Agents and SSE streamingRun agents via the API
Lightweight automation or scriptsREST API