About this project
opencode2api is a Go gateway for OpenCode Zen and Zen Go. It exposes Chat Completions, Responses, and Anthropic Messages endpoints, translating between their native protocols while managing upstream keys and proxies. The executable includes a WebUI and requires no Node.js runtime or database.
Capabilities include JSON and SSE responses across all three inference protocols, support for text, images, reasoning, function tools, tool calls, and tool results, separate Zen and Go key pools with configurable tier preference, retries, and session affinity, optional anonymous Zen access, direct/HTTP/HTTPS/SOCKS5/SOCKS5H proxy support, dynamic model discovery with disk caches, a separate management port with configuration editing, a Playground, diagnostics, token statistics, and live logs, plus configuration hot reload with validation.
Quick start: download a binary from GitHub Releases or build with Go 1.24+. Edit config.json to set server_keys, Zen/Go keys (or enable anonymous), and the WebUI password. Run with `./opencode2api -config config.json`. Docker images are available via ghcr.io/jasonxu114514/opencode2api with docker compose support.
API endpoints include GET /v1/models, POST /v1/chat/completions, POST /v1/responses, POST /v1/messages, and GET /healthz. Authentication uses local server_keys via Bearer or x-api-key headers. Streaming is supported with `stream: true`. The gateway does not implement embeddings, file uploads, image generation, or Responses retrieval/cancellation, and does not store conversation history.
Routing uses model discovery from upstream /v1/models and OpenCode's capability catalog, with protocol overrides available. Anonymous access is enabled for models with 'free' in the ID or zero-cost non-deprecated models. Routing sequence tries anonymous proxies first, then authenticated tiers in preference order, with retries per tier. Session affinity uses a stable hash based on explicit session IDs or the first user message.
Configuration covers keys, listeners, routing preferences, reasoning effort (with per-model overrides), proxies (inline list and proxyfile), timeouts, connection pools, logging, and WebUI settings. The WebUI provides a Configuration Center, Playground, diagnostics, monitoring, and live logs. Management API routes handle authentication, config retrieval/update, reload, secret reveal, account updates, monitoring stats, model diagnostics, inference testing, and logs.
Monitoring tracks request outcomes, upstream attempts, token usage, and resource statistics. Data persistence includes configuration files, backups, and model caches; sessions and metrics are in-memory. Health checks report readiness based on catalog availability and proxy health. Development requires Go 1.24+ and uses standard Go tooling.
Comments
0 Rating appears after 10 ratings
Sign in to join the discussion.