Access OpenCode using the TS SDK

2025-12-09
Cloudflare

Access OpenCode using the TS SDK

Full TypeScript SDK for building on top of OpenCode

index.ts
1import { getSandbox } from '@cloudflare/sandbox';
2import { createOpencode } from '@cloudflare/sandbox/opencode';
3
4// Get a sandbox container and start OpenCode server inside it
5const sandbox = getSandbox(env.Sandbox, "opencode");
6const { client } = await createOpencode(sandbox, {
7 directory: '/home/user/project',
8 config: { provider: { anthropic: { options: { apiKey: env.ANTHROPIC_API_KEY } } } },
9});
10
11// Create a session and send a prompt
12const session = await client.session.create({ body: { title: "My Session" } });
13const result = await client.session.prompt({
14 path: { id: session.data.id },
15 body: {
16 model: { providerID: "anthropic", modelID: "claude-sonnet-4" },
17 parts: [{ type: "text", text: "Summarize the README." }],
18 },
19});
20
21console.log(result.data?.parts?.find((p) => p.type === "text")?.text);