01. HTTP API¶
🎯 Objective¶
Talk directly to the OpenCode server's HTTP API — the same one the TUI uses.
📋 Overview¶
The server exposes an HTTP API described by an OpenAPI document at /openapi.json. Use the built-in api command, which reuses the TUI's discovery and authentication (and can start the background service):
opencode api get /api/info
Calls using method + path:
opencode api post /api/example --data '{"key":"value"}'
opencode api get /api/example --header 'X-Example:value'
Or use the operation ID with --param:
opencode api session.list --param limit=10
opencode api session.create --data '{"title": "New session"}'
- Request bodies assume
Content-Type: application/json --data/-dsends the body;--header/-Hadds headers
🧪 Health check¶
opencode api get /api/info
A successful response confirms the client↔service connection and is the standard health check in deployments.
🔌 Server¶
opencode serve
opencode serve --hostname 0.0.0.0 --port 4096 --cors https://app.example.com
When connected to an explicit server, use the same configured authentication context instead of assembling an unauthenticated request.
🧰 Supporting tools¶
opencode service status # is the background service healthy?
opencode debug paths db # database path (sessions/credentials)
❓ Hands-on challenge¶
- Run
opencode api get /api/infoand confirm it is healthy - List sessions by path and by operation ID
- Create a session with
--dataand retrieve it - Start
opencode servewith--corsand call it from a script
📚 Official source¶
Next step: Lesson 02 — JavaScript Client · Back to module