Server

Interact with opencode server over HTTP.

The opencode serve command runs a headless HTTP server that exposes an OpenAPI endpoint that an opencode client can use.


Usage

opencode serve [--port <number>] [--hostname <string>]

Options

FlagShortDescriptionDefault
--port-pPort to listen on4096
--hostname-hHostname to listen on127.0.0.1

How it works

When you run opencode it starts a TUI and a server. Where the TUI is the client that talks to the server. The server exposes an OpenAPI 3.1 spec endpoint. This endpoint is also used to generate an SDK.

:::tip Use the opencode server to interact with opencode programmatically. :::

This architecture lets opencode support multiple clients and allows you to interact with opencode programmatically.

You can run opencode serve to start a standalone server. If you have the opencode TUI running, opencode serve will start a new server.


Connect to an existing server

When you start the TUI it randomly assigns a port and hostname. You can instead pass in the --hostname and --port flags. Then use this to connect to its server.

The /tui endpoint can be used to drive the TUI through the server. For example, you can prefill or run a prompt. This setup is used by the opencode IDE plugins.


Spec

The server publishes an OpenAPI 3.1 spec that can be viewed at:

http://<hostname>:<port>/doc

For example, http://localhost:4096/doc. Use the spec to generate clients or inspect request and response types. Or view it in a Swagger explorer.


APIs

The opencode server exposes the following APIs.


App

MethodPathDescriptionResponse
GET/appGet app infoApp
POST/app/initInitialize the appboolean

Config

MethodPathDescriptionResponse
GET/configGet config infoConfig
GET/config/providersList providers and default models{ providers: Provider[], default: { [key: string]: string } }

Sessions

MethodPathDescriptionNotes
GET/sessionList sessionsReturns Session[]
GET/session/:idGet sessionReturns Session
GET/session/:id/childrenList child sessionsReturns Session[]
POST/sessionCreate sessionbody: { parentID?, title? }, returns Session
DELETE/session/:idDelete session
PATCH/session/:idUpdate session propertiesbody: { title? }, returns Session
POST/session/:id/initAnalyze app and create AGENTS.mdbody: { messageID, providerID, modelID }
POST/session/:id/abortAbort a running session
POST/session/:id/shareShare sessionReturns Session
DELETE/session/:id/shareUnshare sessionReturns Session
POST/session/:id/summarizeSummarize session
GET/session/:id/messageList messages in a sessionReturns { info: Message, parts: Part[]}[]
GET/session/:id/message/:messageIDGet message detailsReturns { info: Message, parts: Part[]}
POST/session/:id/messageSend chat messagebody matches ChatInput, returns Message
POST/session/:id/shellRun a shell commandbody matches CommandInput, returns Message
POST/session/:id/revertRevert a messagebody: { messageID }
POST/session/:id/unrevertRestore reverted messages
POST/session/:id/permissions/:permissionIDRespond to a permission requestbody: { response }

Files

MethodPathDescriptionResponse
GET/find?pattern=<pat>Search for text in filesArray of match objects with path, lines, line_number, absolute_offset, submatches
GET/find/file?query=<q>Find files by namestring[] (file paths)
GET/find/symbol?query=<q>Find workspace symbolsSymbol[]
GET/file?path=<path>Read a file{ type: "raw" | "patch", content: string }
GET/file/statusGet status for tracked filesFile[]

Logging

MethodPathDescriptionResponse
POST/logWrite log entry. Body: { service, level, message, extra? }boolean

Agents

MethodPathDescriptionResponse
GET/agentList all available agentsAgent[]

TUI

MethodPathDescriptionResponse
POST/tui/append-promptAppend text to the promptboolean
POST/tui/open-helpOpen the help dialogboolean
POST/tui/open-sessionsOpen the session selectorboolean
POST/tui/open-themesOpen the theme selectorboolean
POST/tui/open-modelsOpen the model selectorboolean
POST/tui/submit-promptSubmit the current promptboolean
POST/tui/clear-promptClear the promptboolean
POST/tui/execute-commandExecute a command ({ command })boolean
POST/tui/show-toastShow toast ({ title?, message, variant })boolean
GET/tui/control/nextWait for the next control requestControl request object
POST/tui/control/responseRespond to a control request ({ body })boolean

Auth

MethodPathDescriptionResponse
PUT/auth/:idSet authentication credentials. Body must match provider schemaboolean

Events

MethodPathDescriptionResponse
GET/eventServer-sent events stream. First event is server.connected, then bus eventsServer-sent events stream

Docs

MethodPathDescriptionResponse
GET/docOpenAPI 3.1 specificationHTML page with OpenAPI spec

How is this guide?

Last updated on

Powered by Holocron

Documentation