GuidesHosting choices

OpenCode API: opencode serve, endpoints and the SDK

OpenCode's HTTP API: opencode serve's flags, its password and Basic auth, the /api endpoints and OpenAPI spec, @opencode/client and @opencode/sdk, and v1's API.

November 25, 2026Updated November 27, 2026The Everpod team
The short answer

opencode serve starts OpenCode’s HTTP server in the foreground. On OpenCode 2 (2.4.1, November 23, 2026) it listens on 127.0.0.1, port 4096 or the next free one, and always requires a password: set OPENCODE_PASSWORD or it prints a generated one. Clients use Basic auth with the user name opencode:

curl -u "opencode:$OPENCODE_PASSWORD" http://127.0.0.1:4096/api/info
Copied

The endpoints live under /api (/api/session, /api/session/<id>/prompt, /api/event), described by an OpenAPI 3.1 document at opencode.ai/docs/openapi.json. From JavaScript, use @opencode/client for a running server or @opencode/sdk to run OpenCode inside your own process. OpenCode 1’s API is different and not compatible: no /api prefix, the spec at /doc, and the @opencode-ai/sdk package.

“OpenCode API” also gets used for the API keys of OpenCode’s paid model gateway, which you put into OpenCode or another tool to reach its models; that is OpenCode Zen. This page is about driving OpenCode itself from code.

Which version you have

opencode --version shows a 2.x version or a 1.x one. The two APIs share nothing a client can rely on: the migration page says “Integrations that call the V1 server API must migrate to the V2 API.” Version 2 first; version 1 is at the end.

Running the server: opencode serve

Every OpenCode 2 client already talks to a server: by default, the shared background service your account starts on first use, on port 49374 (28894 on Windows), which OpenCode’s background service covers. You can call that one, but for a program of your own the docs point at a dedicated process: “opencode serve runs the same server in the foreground instead of through the shared background service,” which suits systemd, Docker, or a script that owns its lifetime.

(umask 077; openssl rand -base64 32 > ~/.opencode-serve-password)
export OPENCODE_PASSWORD="$(cat ~/.opencode-serve-password)"
opencode serve --port 4096
Copied

It prints server listening on http://127.0.0.1:4096. Its flags, from the command’s definition at the v2.4.1 tag:

FlagWhat it doesDefault
--hostname
Address to listen on127.0.0.1
--port
Port to listen on4096, or the next free port above it
--cors
“Additional allowed CORS origin (repeat for multiple origins)”none extra
--stdio
Prints {"url": ...} as JSON, never prints the password, and exits when its standard input closesoff
--service
Runs as the shared background service (what OpenCode itself starts)off

Leave the port unset and the server takes 4096, or the next one up if 4096 is in use, so pass --port whenever a client has to find it. The password comes from OPENCODE_PASSWORD, or version 1’s OPENCODE_SERVER_PASSWORD if that is the one set. With neither, the server generates a random one and prints it as server password <password>, which lands in any log that captures its output; set the variable on a server. --stdio is the mode for a parent process that spawns the server and reads its address from standard output; it also removes both password variables from the environment the agent’s tools inherit.

Without --cors, a browser page may call the server only from http://localhost or http://127.0.0.1 on any port, from opencode.ai and its subdomains, from OpenCode’s desktop app, or from the server’s own address (cors.ts at 2.4.1). A web app on any other origin needs --cors https://app.example.com, one flag per origin.

Calling it: auth, endpoints, the spec

The web interface page states the scheme: “Other HTTP clients use Basic auth. The username is always opencode, and only the password can be changed.” A request without it gets 401.

curl -u "opencode:$OPENCODE_PASSWORD" http://127.0.0.1:4096/api/info
curl -u "opencode:$OPENCODE_PASSWORD" "http://127.0.0.1:4096/api/session?limit=5"
curl -N -u "opencode:$OPENCODE_PASSWORD" http://127.0.0.1:4096/api/event
Copied

The API reference calls the surface experimental and lists 188 operations, generated from an OpenAPI 3.1.0 document you can feed to a code generator; it is published on the docs site, where version 1 served its spec from the server at /doc. The endpoints most programs need:

Method and pathWhat it does
GET /api/info
Server info
GET /api/session
List sessions, newest 50 by default; filters include directory, search, archived
POST /api/session
Create a session
POST /api/session/{sessionID}/prompt
Send a message and start the agent
GET /api/session/{sessionID}/message
Read the session’s messages
POST /api/session/{sessionID}/interrupt
Stop what is running
GET /api/event
Subscribe to the event stream

Two things trip up a first script. The prompt call, in the reference’s words, “returns once the message is queued, not when the agent finishes”: wait with POST /api/experimental/session/{sessionID}/wait or the session.execution.* events, then read the reply from the messages endpoint. And the event stream does not buffer for you: it is “volatile by contract: a slow consumer overflows and fails the stream, and events during disconnection are missed,” so a client that reconnects should re-read the session rather than assume it saw everything. Calls about a project rather than one session (models, providers, files, MCP servers and others) take an optional working directory, sent as the query parameter location[directory]; without it the server uses its default location.

The JavaScript packages: @opencode/client and @opencode/sdk

Version 2 has two, both at 2.4.2 on npm as of November 27, 2026. @opencode/client talks to a running server over HTTP. Its docs install it with bun add @opencode/client and give this:

import { OpenCode } from "@opencode/client"

const client = OpenCode.make({
  baseUrl: "http://localhost:4096",
})

const session = await client.session.create({
  location: { directory: "/workspace" },
})

await client.session.prompt({
  sessionID: session.id,
  text: "Review the current changes",
})
Copied

That example sends no credentials. OpenCode.make takes a headers option, and opencode serve expects Basic auth, so the header goes there:

const auth = Buffer.from("opencode:" + process.env.OPENCODE_PASSWORD).toString("base64")
const client = OpenCode.make({
  baseUrl: "http://127.0.0.1:4096",
  headers: { authorization: "Basic " + auth },
})
Copied

To use your account’s background service instead, @opencode/client/service finds or starts it and supplies the headers: Service.ensure() returns the endpoint and Service.headers(endpoint) the auth. Events arrive as an async iterable: for await (const event of client.event.subscribe()).

@opencode/sdk is the other shape. Its docs say it runs OpenCode inside your application and “opens no HTTP listener”, so there is no port, password or CORS to think about:

import { OpenCode } from "@opencode/sdk"

await using opencode = await OpenCode.create()
const session = await opencode.sessions.create({
  location: { directory: "/workspace" },
})

await opencode.sessions.prompt({
  sessionID: session.id,
  text: "Review the current changes",
})
Copied

Installed with bun add @opencode/sdk. Its README notes that the default database is in memory, so sessions are gone when the process ends unless you give it a database path.

A terminal against a running server

Version 2 has no attach command and opencode run has no --attach. Both the terminal interface and run take --server <url>, “Connect to a server URL instead of the background service”, and read the password from the same variable:

opencode --server http://127.0.0.1:4096
opencode run --server http://127.0.0.1:4096 "summarise the README"
Copied

Without the password a client stops with “Server at <url> requires a password; set OPENCODE_PASSWORD”. What else changes for a scripted run, and which server it picks when you pass nothing, is in running opencode run on a schedule.

On a server: where it should listen

The server’s API runs commands and edits files in your account, so whoever reaches the port and has the password has your agent. The docs’ own example, opencode serve --hostname 0.0.0.0 --port 4096, listens on every address the machine has, which on a rented server includes its public one, over plain HTTP, with nothing in front but the password. Keep it on 127.0.0.1 and reach it over an SSH tunnel (ssh -L 4096:127.0.0.1:4096 your-server), or bind it to a private network address such as the one tailscale ip -4 prints. OpenCode on a remote server covers both routes for the same server’s web interface, and what 0.0.0.0 exposes. In a container, 0.0.0.0 inside is normal; the care moves to which host address the port is published on.

OpenCode 1: /doc, @opencode-ai/sdk, --attach

Version 1 (1.19.6, November 17, 2026) has a different API, and its server docs now live only in the source, since opencode.ai’s pages show version 2. opencode serve takes --port (default 4096), --hostname (default 127.0.0.1), --cors, --mdns and --mdns-domain. The difference that matters most: it runs with no password unless you set OPENCODE_SERVER_PASSWORD, which turns on Basic auth with the user opencode (or OPENCODE_SERVER_USERNAME), so set it before giving --hostname anything but 127.0.0.1.

Porting to version 2 means the new paths and the new packages, and always a password: OPENCODE_SERVER_PASSWORD still works, OPENCODE_SERVER_USERNAME does not. The rest of the move, sessions and config included, is in moving from OpenCode 1 to 2.

Run OpenCode on an always-on developer pod.

A developer pod is a permanent cloud computer (VPS), set up and secured for you, with your pick of Claude Code, Codex, OpenCode, Pi, Hermes and OpenClaw installed. Run more agents at once than your laptop can hold, and keep them working when it’s closed. From $24 a month, built in about ten minutes.