Language integration

Twin Browser + Node.js & TypeScript

Node is the other half of the agent ecosystem — the Vercel AI SDK, LangChain.js, and the OpenAI and Anthropic TypeScript SDKs all run here, and it is where most production API routes and background workers live.

Language4-step setupBearer key auth

How Twin plugs into Node.js & TypeScript

Global `fetch` and a Bearer key are the whole integration; there is no `@twin-browser/*` package to install and none is published. The function below is what the Vercel AI SDK entry wraps in `tool()`, what a Next.js route handler calls, and what a queue worker runs on a schedule — the same `/api/v1/run` endpoint every other path on this page reaches.

Twin is the browser execution layer your stack calls. The first run cold-compiles a skill via skill compilation; every similar request after that is matched from the cache and replayed deterministically, so your marginal cost per run trends toward zero rather than climbing with usage.

app.example.com
  1. Receive goal from Node.js & TypeScriptdone
  2. Compile DOM → token-efficient indexed statedone
  3. Match the semantic dispatch cacherunning
  4. Replay compiled skill — 0 LLM callsqueued

Wire it up

Drop Twin into Node.js & TypeScript.

Copy, paste, and swap in your Bearer key. The first run compiles a skill; repeats match the semantic dispatch cache and replay deterministically.

twin.ts — the whole integrationtypescript
const TWIN = 'https://twin-browser.com/api/v1';

type Success =
  | { kind: 'statusText'; match: string }
  | { kind: 'urlIncludes'; value: string }
  | { kind: 'textVisible'; value: string }
  | { kind: 'extracted' };

export async function twinRun(url: string, prompt: string, success: Success) {
  const res = await fetch(`${TWIN}/run`, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.TWIN_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ url, prompt, success }),
  });
  if (!res.ok) throw new Error(`Twin ${res.status}: ${await res.text()}`);
  return res.json();
}

Base URL https://twin-browser.com/api/v1 · auth Authorization: Bearer ab_live_… · over MCP the same engine is run_goal, compile_skill and run_skill the full tool table.

  1. Set your key

    Put TWIN_API_KEY (an ab_live_ key from the dashboard) in your environment — never in client-side code.

  2. Copy the helper

    Paste the twinRun function below. Node 18+ has global fetch, so there is nothing to install.

  3. Call it server-side

    Use it from a route handler, a worker or a script. The key is a server secret; a browser must never see it.

  4. Compile the repeats

    Once a goal succeeds reliably, compile it and call /api/v1/skills/{name}/run for a deterministic replay.

FAQ

Node.js & TypeScript on Twin — common questions

Is there an @twin-browser/sdk package?
No. The only package Twin publishes to npm is twin-browser-mcp, the MCP server your client launches over stdio. For Node, global fetch against /api/v1 is the supported path.
Can I call Twin from the browser?
No — a Twin key is a server secret. Call it from a route handler, an edge function or a worker, and pass only the result to the client.

Wire up Node.js & TypeScript in minutes.

Free to start. Usage-based credits from $29/mo — each call billed the higher of its flat action price or its metered cost.