SDK Integration

The Rightbrain SDK lets you add production-ready AI capabilities into your TypeScript or JavaScript apps direct from the Tasks you’ve created in Rightbrain.

No boilerplate, no manual schema handling. It pairs with the Rightbrain CLI to generate type-safe client code and simplify authentication.

When to use the SDK

Use the SDK when building apps or internal tools in TypeScript/Node/Next.js. Use the REST API directly when working from other languages or lightweight back-end scripts.

Quick Start

1. Prerequisites

  • Node.js 18+
  • pnpm / npm / yarn
  • A Rightbrain account with API credentials

2. Install the SDK

$pnpm add @rightbrain/sdk
># or
>npm install @rightbrain/sdk

3. Configure Environment

Copy .env.example.env.local and add your Rightbrain credentials:

$RB_ORG_ID=your_org_id
>RB_PROJECT_ID=your_project_id
>RB_OAUTH2_URL=your_oauth_url
>RB_API_URL=your_api_url
>RB_CLIENT_ID=your_client_id
>RB_CLIENT_SECRET=your_client_secret
>RB_OAUTH2_TOKEN_PATH=/oauth/token

In the Rightbrain Dashboard, create an OAuth 2.0 Client and click Client.env to copy the credentials.

Creating a new OAuth client in the Rightbrain dashboard Copying client credentials to environment variables

Setting Up the Demo App

Clone the Rightbrain SDK Demo:

$git clone https://github.com/RightbrainAI/rightbrain-sdk-demo
>cd rightbrain-sdk-demo
>pnpm install

Populate Tasks in Your Project

Run the setup script to auto-create demo Tasks in your dashboard:

$pnpm run populate-tasks

You’ll now see three new Tasks under your project in the dashboard.

Generate the Type-Safe SDK

$pnpm run generate-sdk

This uses your credentials from rightbrain.config.ts to output generated SDK code to src/generated.

Run the App

$pnpm run dev

Visit http://localhost:3000 - each demo shows real Task executions, visible in your dashboard’s Runs view.

How It Works

1

1. Authentication

Handled via access-token.ts using OAuth2 Client Credentials Flow.

1const client = new ClientCredentials({
2 auth: {
3 tokenHost: process.env.RB_OAUTH2_URL,
4 tokenPath: process.env.RB_OAUTH2_TOKEN_PATH,
5 },
6 client: {
7 id: process.env.RB_CLIENT_ID,
8 secret: process.env.RB_CLIENT_SECRET,
9 },
10});
11const accessToken = await client.getToken({});

In production, always:

  • Secure credentials via environment variables
  • Refresh and cache tokens
  • Add authentication to protect your endpoints
2

2. Next.js API Route Example

The SDK integrates cleanly with the Next.js App Router:

1// app/tasks/product-listing/route.ts
2export async function POST(request: Request) {
3 const token = await getAccessToken();
4 const body = await request.json();
5
6 const rightbrain = RightBrainClient.getInstance({
7 accessToken: token.token.access_token,
8 baseUrl: process.env.RB_API_URL!,
9 organizationId: process.env.RB_ORG_ID!,
10 projectId: process.env.RB_PROJECT_ID!,
11 });
12
13 const response = await rightbrain.runGenerateImageBasedProductListing({
14 inputs: { product_name: body.product_name },
15 });
16
17 return Response.json(response);
18}

3

3. React Hook + Component Example

Define a reusable hook:

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

Use it in your UI:

1function ProductListingForm() {
2 const { execute, data, loading } = useGenerateImageBasedProductListing("task-id");
3
4 return (
5 <form onSubmit={(e) => { e.preventDefault(); execute({ product_name: "Headphones" }); }}>
6 <button disabled={loading}>{loading ? "Generating..." : "Generate"}</button>
7 {data && <p>{data.description}</p>}
8 </form>
9 );
10}

SDK vs REST API - Which Should You Use?

Use CaseRecommended
TypeScript / Node / Next.js appsSDK - auto-typed, ergonomic, built-in auth helpers
Other languages / frameworksREST API - universal, low-level integration
No-code workflows or lightweight automationAPI
Enterprise apps or internal toolsSDK for type safety and simplified deployment