Skip to content

muzfr7/opencode-api-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

opencode-api-examples

Two distinct ways to use DeepSeek from Python:

  1. model_api.py — call deepseek-v4-flash-free directly via OpenCode Zen (needs API key)
  2. agent_sdk.py — drive opencode's built-in agent (file editing, shell, etc.) (no API key needed)

Example 1: Direct model API call (model_api.py)

Call the underlying LLM directly via the OpenAI-compatible endpoint.

Prerequisites

Setup

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

cp .env.example .env
# Then edit .env and set OPENCODE_ZEN_API_KEY=<your-key>

python model_api.py

Code

Uses the OpenAI SDK pointed at https://opencode.ai/zen/v1 with model deepseek-v4-flash-free:

from openai import OpenAI

client = OpenAI(
    api_key="<your-key>",
    base_url="https://opencode.ai/zen/v1",
)

resp = client.chat.completions.create(
    model="deepseek-v4-flash-free",
    messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)

List available models

curl -H "Authorization: Bearer $OPENCODE_ZEN_API_KEY" \
  https://opencode.ai/zen/v1/models

Example 2: Programmatic agent access (agent_sdk.py)

Drive opencode's full agent (can read/write files, run shell commands, etc.) from your own scripts.

Prerequisites

  • opencode CLI installed and configured
  • opencode server running

Setup

pip install --pre opencode-ai

Run

Terminal 1 — start the opencode server:

opencode serve

Terminal 2 — run the agent script:

python agent_sdk.py

Code

Connects to the opencode server at http://localhost:4096, creates a session, and sends a message:

from opencode_ai import Opencode
from opencode_ai.types import TextPartInputParam

client = Opencode(base_url="http://localhost:4096")

session = client.session.create()

result = client.session.chat(
    session.id,
    model_id="opencode/deepseek-v4-flash-free",
    provider_id="opencode",
    mode="build",  # or "plan", "general", "explore", "coder"...
    parts=[TextPartInputParam(type="text", text="Write a hello world Python script to hello.py")],
)

Pre-select the model

By default the web UI or server may pick a different model. To use deepseek-v4-flash-free every time, create .opencode.json in the project root:

{
  "model": "opencode/deepseek-v4-flash-free"
}

Or set it globally in ~/.config/opencode/opencode.jsonc.

About

Two ways to use DeepSeek from Python: direct model API call via OpenCode Zen, and programmatic agent via opencode Python SDK.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages