camelAI Documentation

API Reference

camelRun v2 TypeScript SDK (@camelai/run)

Reference for the zero-dependency @camelai/run v2 TypeScript SDK: CamelRun, CamelRunAdmin, AgentHandle, defineTool, ToolSession, SSE helpers, and CamelRunError.

View as Markdown

The camelRun v2 TypeScript SDK (@camelai/run 2.0.0) has zero runtime dependencies and runs in Node 22+, Bun, Deno, and Cloudflare Workers.

bash
npm install @camelai/run

Other languages (Python, Go, Rust, Ruby) can call the /v2/* REST, SSE, and WebSocket API directly using standard HTTP and WebSocket libraries. See REST and WebSocket API.

Initialization

ts
import { CamelRun, CamelRunAdmin } from "@camelai/run";

const client = new CamelRun({
  baseUrl: "https://run.camelai.com",
  apiKey: process.env.CAMELRUN_API_KEY!,
  // Optional overrides for custom runtimes or tests:
  // fetch: customFetch,
  // WebSocket: customWebSocket,
});

// Inspect tenant profile and balance
const me = await client.me();

// Close all active local-tool WebSocket sessions before process exit
client.close();

For self-hosted or operator tenant creation (POST /v2/tenants):

ts
const tenant = await CamelRun.createTenant({
  baseUrl: "https://run.camelai.com",
  adminKey: process.env.ADMIN_SECRET!,
  name: "Acme Corp",
  initialCreditsUsd: 25,
});
// Or: new CamelRunAdmin({ baseUrl, adminKey }).createTenant({ name, initialCreditsUsd })

CamelRun namespaces

NamespaceMethods
client.me()GET /v2/me -> TenantInfo
client.tokenscreate(input?), list(), delete(id)
client.providersset(provider, { apiKey, baseUrl?, modelAliases? })
client.billingget(), addCredits({ amountUsd, refId?, note?, adminKey })
client.definitionsupsert(slug, config), list(), delete(slug)
client.reposcreate({ name? }), list(), get(repoId), delete(repoId), read(repoId, { path, at?, offset?, limit? }), write(repoId, input), restore(repoId, { targetCommit, ref?, message? }), ls(repoId, opts?), grep(repoId, input), history(repoId, opts?)
client.agentsupsert(key, config), get(idOrKey, { tools? }), list(), delete(idOrKey)
client.runscreate<TOutput>({ input, model?, instructions?, outputSchema?, tools?, maxSteps? })
client.decisionsdecide<TChoice>({ input, choices, instructions?, model? })
client.voicecreateSession({ agentIdOrKey, provider, voice? }), invokeTool(sessionId, { name, args?, callId? })
client.channelscreate(config), sendInbound(channelId, payload, headers?)
client.webhookscreate({ url, events?, secret? }), list(), delete(id)
client.mediatranscribe(input), speech(input), generateImage(input), getImage(id)
client.imagesrender(svg, { files? }) -> RenderedImage

AgentHandle

Returned by client.agents.upsert(), client.agents.get(), and agent.fork().

Properties: id, key, agentToken, repoId, branch, model, created, summary.

MethodDescription
agent.run<TOutput>(prompt, options?)Start a run and wait for completion (polls if still queued/running). Throws CamelRunError on status === "failed" unless throwOnError: false.
agent.submit<TOutput>(prompt, options?)Start a run with wait: false and return the queued RunRecord immediately.
agent.steer<TOutput>(prompt, options?)Send a mid-turn steer (whileRunning: "steer").
agent.stream<TOutput>(prompt, options?)Returns an AgentRunStream<TOutput> async generator yielding AgentEvents with a .result: Promise<RunRecord<TOutput>> property.
agent.getRun<TOutput>(runId)GET /v2/agents/:id/runs/:runId
agent.getRunByKey<TOutput>(idempotencyKey)GET /v2/agents/:id/runs/:key?by=idempotency-key
agent.abort({ keepQueued? })POST /v2/agents/:id/abort
agent.answerInput<TOutput>(inputId, { approved?, answer?, wait?, actor? })POST /v2/agents/:id/inputs/:inputId
agent.history({ after?, before?, limit? })GET /v2/agents/:id/history
agent.importV1History(input)PUT /v2/agents/:id/history/import (or /import/chunk when appendAfter is set)
agent.events({ after?, runId? })JSON event page (GET /v2/agents/:id/events)
agent.eventStream({ after?, runId?, signal? })Async generator over SSE (Accept: text/event-stream)
agent.compact({ summary?, keepLast? })POST /v2/agents/:id/compact
agent.resetContext({ summary?, keepLast? })POST /v2/agents/:id/reset-context
agent.fork({ key?, atSeq?, tools? })POST /v2/agents/:id/fork -> new AgentHandle
agent.schedulescreate(input), list(), delete(id)
agent.runWorkflow<TResult>(code, { idempotencyKey?, wait? })POST /v2/agents/:id/workflows
agent.dream()POST /v2/agents/:id/dream
agent.attachTools(tools)Register or update local WebSocket tools on this handle
agent.ensureToolSession()Explicitly open the attached-tool WebSocket (ToolSession)
agent.onEvent(listener)Subscribe to live events arriving over the tool WebSocket (returns unsubscribe function)
agent.delete()Close local tool session and DELETE /v2/agents/:id
agent.close()Close local tool WebSocket without deleting the remote agent

Local tools and SSE utilities

ts
import {
  defineTool,
  ToolSession,
  buildAgentWsUrl,
  parseSSE,
  streamAgentEvents,
  normalizeAgentEvent,
  isTerminalAgentEvent,
  CamelRunError,
} from "@camelai/run";
  • defineTool({ name, description, parameters, requiresApproval?, execute }): Typed helper for creating local tools executed over /v2/agents/:idOrKey/ws.
  • ToolSession: Low-level WebSocket session manager used by AgentHandle.
  • parseSSE / streamAgentEvents: Zero-dependency SSE parser for streaming Response.body in Node, Bun, Deno, Workers, or browsers.
  • CamelRunError: Error subclass exposing code, status, and optional run.