
Deploy OpenCode Server API
Drive OpenCode sessions over its HTTP API and SDK
opencode
Just deployed
/data
Deploy and Host self hosted OpenCode Server API (Open-Source AI Coding Agent) on Railway
This template runs opencode serve behind HTTP basic auth on port 4096, with a volume at /data. The same server that powers the web UI exposes an OpenAPI 3.1 API at /doc, so scripts, bots and your own UI can drive sessions and prompts.
About Hosting OpenCode Server API open-source software on Railway (self hosted OpenCode server API template)
This template runs one container: opencode serve on port 4096 behind HTTP basic auth, with a single volume at /data. Railway handles TLS, the public domain, health checks and restarts.
Why Deploy OpenCode Server API, the Claude Code alternative on Railway (Railway Free Trial)
Claude Code keeps you inside Anthropic's terminal and billing. The Railway $5 trial covers a first look before you commit.
Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.
By deploying OpenCode Server API on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.
Railway vs Other Hosting Providers and VPS for OpenCode Server API self hosting
Railway bakes in TLS, basic auth and a restart policy, while a plain VPS makes you wire all three yourself.
| Provider | What You Get with Railway | What You Get with the Other Provider |
|---|---|---|
| DigitalOcean | Domain, TLS and the generated 32-character password already wired | Droplet, then Caddy, a systemd unit and your own password policy |
| AWS | Managed container, volume and health checks | ECS or EC2 plus IAM, a load balancer and more setup |
| Hetzner | Same Docker image, no server upkeep | Cheap VPS, but you own firewall, TLS and uptime |
The API surface is identical either way; only the plumbing around it changes.
Common Use Cases for hosted OpenCode Server API
- Chat bot per request: your bot calls the session create endpoint, sends the user's prompt, and reads the reply back, so each conversation gets its own isolated session on the shared server.
- CI fix drafts: a failing pipeline job posts the log and a "draft a fix" prompt through the API, then opens a pull request with whatever diff OpenCode produces in /data/workspace.
- Custom web UI: build your own front end on the generated SDK, calling createOpencodeClient({ baseUrl }) with basic auth headers attached on every request.
- Live progress dashboards: subscribe to GET /global/event over server-sent events and render tool calls, message deltas and session state as they happen instead of polling.
- Scheduled maintenance: a cron job wakes up nightly, asks OpenCode to update dependencies or triage stale issues, and posts a summary to your team channel.
Dependencies for OpenCode Server API Docker hosted on Railway
The template runs one service, opencode, from ghcr.io/onepush-dev/opencode:1.19.3. One volume mounts at /data, and the entrypoint refuses to start if OPENCODE_SERVER_PASSWORD is empty.
Deployment Dependencies for Managed OpenCode Server API Service (AI Coding Agent)
- Auth:
OPENCODE_SERVER_PASSWORD(32 generated characters) plusOPENCODE_SERVER_USERNAME=opencode. - Runtime env:
OPENCODE_HOSTNAME=0.0.0.0,OPENCODE_WORKSPACE=/data/workspace,HOME=/data/home,PORT=4096. - Volume:
/data/workspaceholds projects,/data/homeholdsopencode.json,auth.jsonand sessions. - Health check:
/site.webmanifest, the only unauthenticated path. Everything else returns 401. - Keys, optional:
ANTHROPIC_API_KEY,OPENAI_API_KEY,OPENROUTER_API_KEY,GEMINI_API_KEY.
Implementation Details for OpenCode Server API (Using OpenCode official docker image)
Read the OpenAPI 3.1 spec at /doc before writing request bodies, because paths shift between versions. Upgrade by changing the image tag, not by letting the container self-update.
How does OpenCode Server API compare against other programmable coding agents
OpenCode vs Claude Code (Anthropic alternative)
- Server-first: the TUI is a client of an HTTP server with an OpenAPI 3.1 spec at /doc, so scripts drive the same sessions as the web UI.
- Model choice: 75+ providers via Models.dev, including local models; Claude Code only bills Anthropic models.
- Where Claude wins: Anthropic's permission UX and SDK docs are more polished and widely adopted.
OpenCode vs Codex CLI (OpenAI alternative)
- Many clients, one server: TUI, desktop app, IDE extension and your own SDK client can attach to one session; Codex CLI is mainly local.
- Provider-agnostic: Anthropic, OpenAI, Google, OpenRouter or local endpoints instead of OpenAI only.
- Where Codex wins: it ships from OpenAI with tight sandbox defaults and no server to run.
OpenCode vs Aider (git-native alternative)
- Headless API: Aider is a terminal pair programmer with no web server; OpenCode exposes sessions, SSE events and /tui over HTTP.
- Both git-native: undo and redo ride on git snapshots, though Aider's commit-per-change flow is lighter.
- Where Aider wins: a smaller footprint and a long record for plain terminal editing.
OpenCode vs OpenHands (agent platform alternative)
- Lighter deployment: one container, one volume, one password; OpenHands is a fuller platform with its own runtime stack.
- Same goal, narrower scope: both are driven programmatically, but OpenCode stays a coding agent server, not a sandbox platform.
- Where OpenHands wins: more built-in orchestration, browser tooling and multi-agent experiments.
How to use OpenCode Server API (the OSS AI coding agent server)?
- Copy OPENCODE_SERVER_PASSWORD from Railway Variables.
- Check health:
curl -u opencode:PASSWORD https://your-domain/global/health
- Read the spec at https://your-domain/doc and confirm every request body there.
- Add a provider key variable, or log in through the web UI.
- Create a session, then send a prompt through the session endpoints:
curl -u opencode:PASSWORD -X POST https://your-domain/session -H "Content-Type: application/json" -d '{}'
- Use the SDK:
import { createOpencodeClient } from "@opencode-ai/sdk";
const client = createOpencodeClient({ baseUrl: "https://your-domain" });
Send basic auth headers on every request. Session paths shift between versions, so trust /doc over any example.
How to self host OpenCode Server API on other VPS Services (OpenCode self hosting guide)
Clone the Repository
You don't clone OpenCode to run it. Install from the official script:
curl -fsSL https://opencode.ai/install | bash
Or pull the official image ghcr.io/anomalyco/opencode.
Install Dependencies
You need Node.js for the npm route and git, since sessions snapshot edits through git.
node --version
git --version
Configure Environment Variables
The server won't start without a password.
export OPENCODE_SERVER_PASSWORD="a long random string"
export OPENCODE_SERVER_USERNAME="opencode"
export OPENCODE_HOSTNAME="0.0.0.0"
export HOME="/data/home"
Keep HOME on persistent storage or you lose auth.json logins and sessions.
Start the OpenCode Server API Application
opencode serve --hostname 0.0.0.0 --port 4096
Check https://your-domain/global/health, read https://your-domain/doc for bodies, and expose port 4096 only through your HTTPS proxy.
Railway handles TLS, the volume, the generated password and restarts for you.
Official Pricing of OpenCode Server API (OpenCode pricing)
OpenCode itself is free. You can log in with your own Anthropic, OpenAI, OpenRouter or Gemini key, or use local models, and pay that provider directly at their rates. On Railway, expect about $5-15/mo for one light-use container.
OpenCode Server API cloud vs self hosted comparison (Pricing, features, costs, and more)
The cloud comparison here is mostly about where the process runs, not what it does.
Monthly cost of self hosting OpenCode Server API on Railway
Expect roughly $5-15 per month for one container at light use, more during heavy builds or test runs. The $5 trial covers the first stretch.
System Requirements for Hosting OpenCode Server API on a VPS
Any small Linux box with Docker, 2 GB of RAM and 10 GB of disk will run the server, though agent work gets cramped fast. Give it 4 GB and 20 GB if you plan to clone repos and run LSPs.
Frequently Asked Questions (FAQs)
Where is the OpenCode Server API documented?
The server publishes an OpenAPI 3.1 spec at /doc on your Railway domain. Open it after signing in and confirm every path and request body against the version you run.
How do I authenticate to the API?
Use HTTP basic auth with the username from OPENCODE_SERVER_USERNAME (opencode by default) and the generated OPENCODE_SERVER_PASSWORD. curl -u opencode:PASSWORD https://your-domain/global/health is the quickest check, since every path except /site.webmanifest wants credentials.
Can I stream progress from a session?
Yes. GET /global/event is a server-sent events stream, so a bot or dashboard can follow tool calls, message updates and session changes without polling.
Is there an SDK?
Yes, @opencode-ai/sdk on npm. createOpencodeClient({ baseUrl }) points at your domain, and its types are generated from the same spec.
Can two clients share one server?
Yes. The TUI is itself a client, so a terminal, the web UI and your script can hold sessions on one instance. Keep session IDs separate so prompts don't collide.
Is it safe to expose to the internet?
Treat it like an SSH box. The API reads and writes files and runs commands in /data/workspace, so keep the password long, never ship it to browser code, and leave authentication on.
Template Content
