> For the complete documentation index, see [llms.txt](https://www.opencodebook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opencodebook.xyz/en/chapter_13_plugin_system/13.4_opencode_sdk.md). Agents can also search and read this documentation through its [MCP server](https://www.opencodebook.xyz/~gitbook/mcp).

# 13.4 OpenCode SDK

> **Model**: claude-opus-4-6 (anthropic/claude-opus-4-6) **Generation Date**: 2026-02-17

***

Plugins obtain an OpenCode SDK client instance through `PluginInput.client`. This section briefly introduces the SDK's design and usage.

## 13.4.1 @opencode-ai/sdk -- The TypeScript Client SDK

`@opencode-ai/sdk` is OpenCode's official TypeScript SDK, located in the `packages/sdk/` directory. It provides a type-safe client that corresponds exactly to the OpenCode Server API.

The SDK is **automatically generated based on the OpenAPI specification** -- when the Server's API changes, the SDK can be regenerated using the `opencode generate` command, ensuring that the API and SDK always remain in sync.

## 13.4.2 Using createOpencodeClient()

```typescript
import { createOpencodeClient } from "@opencode-ai/sdk/v2"

const sdk = createOpencodeClient({
  baseUrl: "http://localhost:4096",
  directory: "/path/to/project",
  fetch: customFetch,  // Optional: custom fetch implementation
  signal: abortSignal,  // Optional: abort signal
})
```

The SDK client provides the following API namespaces:

```typescript
// Session management
sdk.session.create({})
sdk.session.list()
sdk.session.get({ sessionID: "..." })
sdk.session.chat({ sessionID, content })
sdk.session.abort({ sessionID })
sdk.session.fork({ sessionID })

// Message management
sdk.message.list({ sessionID })
sdk.message.get({ sessionID, messageID })

// Configuration management
sdk.config.get()

// Event subscription
sdk.event.subscribe({})

// Provider/Model
sdk.provider.list()
sdk.model.list()

// MCP
sdk.mcp.list()

// Permission
sdk.permission.list()
sdk.permission.reply({ requestID, action })

// Other
sdk.project.get()
sdk.file.read({ path })
sdk.pty.create({})
```

## 13.4.3 Auto-Generation Based on the OpenAPI Specification

The SDK generation flow:

1. **Server defines routes**: API routes are defined using the Hono framework, with route parameters described by Zod Schemas.
2. **Generate OpenAPI specification**: The `opencode generate` command traverses all routes, leveraging Zod Schemas and `BusEvent.payloads()` to produce an OpenAPI JSON document.
3. **Generate SDK code**: TypeScript client code is automatically generated from the OpenAPI document.

This "Schema -> OpenAPI -> SDK" pipeline ensures consistency across all three artifacts:

```
Zod Schema (Server side)
    | Auto-extract
OpenAPI Specification (API documentation)
    | Auto-generate
TypeScript SDK (Client side)
```

## 13.4.4 Using the SDK in Plugins

Plugins do not need to create their own SDK client -- `PluginInput.client` already provides a pre-configured instance. This instance uses an internal `fetch` implementation (directly calling `Server.App().fetch()`), bypassing the HTTP network layer for lower latency.

```typescript
const myPlugin: Plugin = async (ctx) => {
  // Use the SDK to list sessions
  const sessions = await ctx.client.session.list()

  // Use the SDK to get project information
  const project = await ctx.client.project.get()

  return {
    "chat.message": async (input, output) => {
      // Inject project statistics into the message
      const stats = await ctx.client.stats()
      output.parts.push({
        type: "text",
        text: `[Project Stats] ${stats.sessions} sessions, ${stats.messages} messages`,
      })
    },
  }
}
```

## 13.4.5 Summary

The OpenCode SDK follows the "single source of truth" principle:

1. **Zod Schema is the sole type definition source** -- Server routes, OpenAPI documents, and SDK types are all derived from Zod Schemas.
2. **OpenAPI is the standard expression of the API contract** -- both human-readable documentation and machine-readable client generators use it as input.
3. **The SDK is auto-generated** -- eliminating the consistency issues that come with manually maintaining client code.

This automated pipeline enables rapid API iteration in OpenCode -- after modifying a Zod Schema on the Server side, a single command updates both the OpenAPI documentation and the SDK.
