---
title: "TypeScript SDK"
description: "Use @shiba/sdk to call the public /api/v1 API from Node, Bun, Deno, browsers, and Workers."
---

> Documentation Index
> Fetch the complete documentation index at: https://tryshiba.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript SDK

The [`@shiba/sdk`](https://github.com/opentoolsorg/shiba/tree/main/packages/sdk) package is a **zero-runtime-dependency** fetch client for Shiba's public API. Types are generated from `GET /api/v1/openapi.json` on your Worker (`apps/backend/src/v1/openapi.json` in the repo).

## Install (monorepo)

```bash
pnpm add @shiba/sdk
```

When published, the package will ship as `@shiba/sdk` under the **AGPL-3.0-only** license (same as the rest of Shiba).

## Authentication

Mint an org-scoped API key in **Settings → API keys** (`shk_…`). Pass it as a Bearer token:

```ts
import { Shiba } from "@shiba/sdk";

const shiba = new Shiba({
  baseUrl: "https://your-worker.example.com",
  apiKey: process.env.SHIBA_API_KEY!,
});
```

`baseUrl` is the Worker origin **without** `/api/v1`; the client adds that prefix.

## Threads

```ts
// Idempotent create — requestId is generated when omitted
const receipt = await shiba.threads.create({
  repoUrl: "https://github.com/acme/repo",
  task: "Fix the flaky test",
});

const threads = await shiba.threads.list();
const thread = await shiba.threads.get("run-id");

await shiba.threads.send("run-id", {
  text: "Also update the README",
  delivery: "queue", // or "interrupt"
});
```

## Event stream (SSE)

```ts
for await (const event of shiba.threads.stream(threadId)) {
  console.log(event.type, event.data);
}
```

The iterator reconnects with `Last-Event-ID` after transient disconnects. Pass `{ reconnect: false }` to stop after the first connection.

## Pull requests

Read-only helpers wrap the thread-scoped PR routes:

```ts
const status = await shiba.pullRequests.status(threadId);
const comments = await shiba.pullRequests.comments(threadId);
const review = await shiba.pullRequests.review(threadId);
```

## Same-origin dashboard use

`apiKey` is optional. Omitting it sends no Bearer token, which lets the dashboard use its existing same-origin session cookie. The frontend wires `@shiba/sdk` to source during monorepo development; published consumers use the built `dist` package.

## Mobile-ready control-plane facade

Versioned v1 resources now expose a coherent mobile slice: approvals, employees, rooms, workspace connections, shared computers, and thread artifact metadata/content. These routes re-enter the owning authenticated domain handlers rather than copying authorization rules. Employee routes and the OpenAI-compatible inference lane accept scoped org API keys where the owning D1/org boundary supports them. Approvals, rooms, workspace connections, shared computers, and thread artifacts remain session-only because their owning routes require verified human, workspace-membership, or private runtime-store identity; the generated OpenAPI security metadata marks that distinction.

```ts
const approvals = await shiba.approvals.list();
const employees = await shiba.employees.list(workspaceId);
const artifacts = await shiba.artifacts.list(threadId);
```

## OpenAI-compatible employee chat

`shiba.openai.models()` lists employees as model IDs and `shiba.openai.chat()` calls `POST /api/v1/chat/completions`. The facade is an explicit inference-only employee runtime lane: no model-visible tools, connected-app actions, external MCP actions, delegation, repository, sandbox, credential, or external-effect authority is available. Runtime-owned transcript/checkpoint state may still persist for the selected conversation. `stream: true` is supported through `shiba.openai.chatStream()`; the Worker translates durable employee stream events into OpenAI chat completion chunks and `[DONE]`. Employee list/chat access follows workspace employee grants: model listing requires view access and chat requires chat access.

The operator CLI exposes the same surface as:

```bash
SHIBA_API_KEY=shk_... pnpm shiba chat --model <employee-id> "Summarize this"
```

## Errors and retries

Failed responses throw `ShibaApiError` with `status` and a **redacted** `body` (API keys and bearer tokens are stripped). Idempotent requests (`GET`, thread `create` with `requestId`, and `send` with `clientKey`) retry on `408`, `429`, and `5xx` with exponential backoff.

## Regenerating types

When `apps/backend/src/v1/openapi.json` changes:

```bash
pnpm -C packages/sdk generate:types
pnpm -C packages/sdk check:types   # CI sync check
```

## Examples

Runnable samples live under `packages/sdk/examples/` in the repository.

## Mobile-ready seams

Paired devices use `/api/v1/devices/pairing-codes` from a dashboard session, then redeem the single-use code at `/devices/pair` for a short-lived access JWT and rotating refresh token. `@shiba/app-core` provides DOM-free React hooks (`useThread`, `useApprovals`, `useEmployees`, `useRunStream`) over this SDK for web and future native shells. `streamTicket` mints a 60-second exact-thread stream URL for embedded run views; deep links are credential-free `shiba://` or `/links/...` identifiers. Notification endpoints can be registered for webpush, Slack, or email; APNs/FCM/Expo are reserved channel identifiers, not yet implemented adapters.

Source: https://tryshiba.dev/docs/sdk/index.mdx
