OpenCode SDK
The OpenCode JS/TS SDK provides a type-safe client for interacting with the server programmatically.
Installation
npm install @opencode-ai/sdk
Creating Clients
Full Instance (Server + Client)
import { createOpencode } from "@opencode-ai/sdk"
const { client } = await createOpencode()
Options:
- •
hostname: Server hostname (default:127.0.0.1) - •
port: Server port (default:4096) - •
signal: AbortSignal for cancellation - •
timeout: Server start timeout in ms (default:5000) - •
config: Configuration object
Client Only
import { createOpencodeClient } from "@opencode-ai/sdk"
const client = createOpencodeClient({
baseUrl: "http://localhost:4096",
})
Options:
- •
baseUrl: Server URL (default:http://localhost:4096) - •
fetch: Custom fetch implementation - •
parseAs: Response parsing method (auto) - •
responseStyle:dataorfields(default:fields) - •
throwOnError: Throw errors instead of return (default:false)
API Modules
Global Module
Health check:
const health = await client.global.health() console.log(health.data.version)
Response: { healthy: true, version: string }
App Module
Write log entry:
await client.app.log({
body: {
service: "my-app",
level: "info",
message: "Operation completed",
},
})
List available agents:
const agents = await client.app.agents()
Response: Agent[]
Project Module
List all projects:
const projects = await client.project.list()
Response: Project[]
Get current project:
const currentProject = await client.project.current()
Response: Project
Path Module
Get current path information:
const pathInfo = await client.path.get()
Response: Path
Config Module
Get config:
const config = await client.config.get()
Response: Config
List providers and defaults:
const { providers, default: defaults } = await client.config.providers()
Response: { providers: Provider[], default: { [key: string]: string } }
Session Module
List sessions:
const sessions = await client.session.list()
Get session:
const session = await client.session.get({
path: { id: "session-id" },
})
List child sessions:
const children = await client.session.children({
path: { id: "session-id" },
})
Create session:
const session = await client.session.create({
body: { title: "My session" },
})
Delete session:
await client.session.delete({
path: { id: "session-id" },
})
Update session:
const updated = await client.session.update({
path: { id: "session-id" },
body: { title: "New title" },
})
Initialize session (analyze app and create AGENTS.md):
await client.session.init({
path: { id: "session-id" },
body: {},
})
Abort running session:
await client.session.abort({
path: { id: "session-id" },
})
Share session:
const shared = await client.session.share({
path: { id: "session-id" },
})
Unshare session:
const unshared = await client.session.unshare({
path: { id: "session-id" },
})
Summarize session:
await client.session.summarize({
path: { id: "session-id" },
body: {},
})
List messages:
const messages = await client.session.messages({
path: { id: "session-id" },
})
Response: { info: Message, parts: Part[] }[]
Get message details:
const message = await client.session.message({
path: { id: "session-id", messageId: "msg-id" },
})
Response: { info: Message, parts: Part[] }
Send prompt:
const result = await client.session.prompt({
path: { id: session.id },
body: {
model: { providerID: "anthropic", modelID: "claude-3-5-sonnet-20241022" },
parts: [{ type: "text", text: "Hello!" }],
},
})
Inject context without AI response (for plugins):
await client.session.prompt({
path: { id: session.id },
body: {
noReply: true,
parts: [{ type: "text", text: "Context injection..." }],
},
})
Send command:
const result = await client.session.command({
path: { id: "session-id" },
body: { command: "/list_files" },
})
Run shell command:
const result = await client.session.shell({
path: { id: "session-id" },
body: { command: "ls -la" },
})
Revert message:
const reverted = await client.session.revert({
path: { id: "session-id" },
body: { messageId: "msg-id" },
})
Restore reverted messages:
const restored = await client.session.unrevert({
path: { id: "session-id" },
})
Respond to permission request:
await client.postSessionByIdPermissionsByPermissionId({
path: { id: "session-id", permissionId: "perm-id" },
body: { approved: true },
})
Files Module
Search text in files:
const textResults = await client.find.text({
query: { pattern: "function.*opencode" },
})
Response: Array with path, lines, line_number, absolute_offset, submatches
Find files by name:
const files = await client.find.files({
query: { query: "*.ts", type: "file" },
})
Find directories:
const directories = await client.find.files({
query: { query: "packages", type: "directory", limit: 20 },
})
Optional query fields for find.files:
- •
type:"file"or"directory" - •
directory: override project root - •
limit: max results (1-200)
Find workspace symbols:
const symbols = await client.find.symbols({
query: { query: "MyFunction" },
})
Response: Symbol[]
Read file:
const content = await client.file.read({
query: { path: "src/index.ts" },
})
Response: { type: "raw" | "patch", content: string }
Get file status:
const status = await client.file.status()
Response: File[]
TUI Module
Append text to prompt:
await client.tui.appendPrompt({
body: { text: "Add this to prompt" },
})
Open dialogs:
await client.tui.openHelp() await client.tui.openSessions() await client.tui.openThemes() await client.tui.openModels()
Submit/clear prompt:
await client.tui.submitPrompt() await client.tui.clearPrompt()
Execute command:
await client.tui.executeCommand({
body: { command: "/list_files" },
})
Show toast:
await client.tui.showToast({
body: { message: "Task completed", variant: "success" },
})
Auth Module
Set authentication credentials:
await client.auth.set({
path: { id: "anthropic" },
body: { type: "api", key: "your-api-key" },
})
Response: boolean
Events Module
Subscribe to real-time events:
const events = await client.event.subscribe()
for await (const event of events.stream) {
console.log("Event:", event.type, event.properties)
}
Response: Server-sent events stream
Error Handling
try {
await client.session.get({ path: { id: "invalid-id" } })
} catch (error) {
console.error("Failed to get session:", (error as Error).message)
}
Types
Import TypeScript types directly:
import type { Session, Message, Part, Project, Agent } from "@opencode-ai/sdk"
Constraints
- •Always handle errors with try-catch when calling SDK methods
- •Use type annotations for better code safety
- •Check response structure before accessing nested properties
- •Use
noReply: truefor context injection without triggering AI responses - •Respect rate limits when calling multiple methods in succession
- •Validate user input before passing to SDK methods
Best Practices
- •Prefer client-only mode when connecting to existing OpenCode instance
- •Use AbortSignal for cancellation where appropriate
- •Validate all paths and IDs before passing to SDK methods
- •Log errors for debugging but avoid exposing sensitive data
- •Use TypeScript types for compile-time safety
- •Mock SDK client in tests for unit testing