TypeScript SDK
The @rightbrain/sdk client turns your tasks into typed functions. This walkthrough connects a Next.js form to a task with a product_name input and JSON output.
Before you start: use Node.js 22+, an existing Next.js App Router project, and a Rightbrain task. The examples use the @/ import alias; adjust paths to match your app. For other languages, use the REST API.
Next.js example
Follow the steps beside the code. Each file is complete: replace <task-id> with an ID from your generated types and use that task’s input fields.
Install the SDK
Run the install command in your app directory. You can also use
pnpm add @rightbrain/sdkoryarn add @rightbrain/sdk.Sign in and generate task types
Sign in, then select your organization, project, API key and tasks in
init. The CLI writes credentials to.envand generates theTaskstype.The default output is
src/generated/index.tswhensrcexists, otherwisegenerated/index.ts. Point the route’s import at that directory.Next.js loads
.envfor server code.DirectTransportdoes not load it or read environment variables itself.Keep credentials on the server
Create a
Client<Tasks>withDirectTransport. Pass the API key asaccessToken, along with your organization, project and API base URL.This file runs on the server. Never put the key in a browser bundle or a
NEXT_PUBLIC_variable. Browser code calls your route below; applications usingPublicTransportalso need their own server proxy.Run the task from your route
The generated task ID selects a typed runner. Its
inputsmatch your task’s prompt variables, andresult.responsecontains the JSON output.This sample route assumes your application’s authentication and access checks already protect it. Apply those checks before exposing a paid task to users.
Handle loading and failures
The hook sends the form input to your route. A failed HTTP response becomes a visible error, and loading is cleared after either outcome.
Display the result
The client component disables the button during the request and displays the returned JSON or an error. It never receives your API key.
Import this component into a page in your app and submit the form to run the task.
Keep generated types current
Run npx rightbrain@latest generate after changing task schemas or the selected tasks. The CLI writes RB_ORG_ID, RB_PROJECT_ID and RB_API_KEY to .env; keep that file out of version control.
For a standalone Node script, load the environment with node --env-file=.env your-script.mjs or your runtime’s environment loader. DirectTransport also accepts a config object for additional fetch options. Credential options are covered in Authentication.
For a generic hook with inferred result types and request cancellation, use use-task.ts from the SDK demo.
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.