SDK
The OpenCode SDK provides a type-safe client for programmatic interaction with the OpenCode server.
Installation
npm install @opencode-ai/sdkClient Setup
Full Instance
import { createOpencode } from "@opencode-ai/sdk"
const { client } = await createOpencode()Client-Only
import { createOpencodeClient } from "@opencode-ai/sdk"
const client = createOpencodeClient({
baseUrl: "http://localhost:4096"
})Configuration
const opencode = await createOpencode({
hostname: "127.0.0.1",
port: 4096,
config: {
model: "anthropic/claude-3-5-sonnet"
}
})TypeScript Support
import type { Session, Message, Part } from "@opencode-ai/sdk"API Reference
Health Check
const health = await client.global.health()Sessions
await client.session.list()
await client.session.create()
await client.session.prompt({
path: { id: sessionId },
body: {
model: { providerID: "anthropic", modelID: "claude-3-5-sonnet" },
parts: [{ type: "text", text: "Your query" }]
}
})Files
const content = await client.file.read({
query: { path: "src/index.ts" }
})Events
const events = await client.event.subscribe()
for await (const event of events.stream) {
console.log("Event:", event.type)
}