Skip to main content

Overview

The serve command starts a headless HTTP server that provides API access to OpenCode functionality without the TUI interface. This is ideal for:
  • Programmatic API access
  • Running on remote servers
  • Integrating with other tools
  • Avoiding MCP server cold boot times

Usage

The server starts on 127.0.0.1:0 (random port) by default. Use flags to customize the network settings.

Options

number
default:"0"
Port to listen on. Default is 0 (random available port).
string
default:"127.0.0.1"
Hostname to listen on. Use 0.0.0.0 to allow external connections.
boolean
default:"false"
Enable mDNS service discovery. When enabled, defaults hostname to 0.0.0.0.
string
default:"opencode.local"
Custom domain name for mDNS service.
string[]
default:"[]"
Additional browser origins to allow for CORS. Can be specified multiple times.

Examples

Basic Server

Start a local server on a random port:

Fixed Port

Start server on port 4096:

Network-Accessible Server

Allow connections from other machines:

With mDNS Discovery

Enable service discovery on local network:

Custom CORS Origins

Allow specific origins for web access:

Authentication

By default, the server runs without authentication. Always set OPENCODE_SERVER_PASSWORD in production environments.
Enable HTTP basic authentication by setting environment variables:
Optionally customize the username (defaults to opencode):

Using with Run Command

You can attach to a running serve instance to avoid MCP server cold boot times:

API Access

The server exposes a full HTTP API. See the Server API documentation for details on available endpoints. Basic example using the JavaScript SDK:

Configuration

You can set default server options in your global configuration file (~/.config/opencode/config.json):
Command-line flags always override configuration file settings.

web

Start server with web interface

attach

Attach TUI to running server

run

Run commands non-interactively

Server API

View full HTTP API documentation