A plugin for OpenCode that automatically syncs available models from OpenRouter's public API to your global configuration.
Contributions are welcome! Please feel free to submit a Pull Request.
See CHANGELOG.md for version history.
- Automatic model sync on startup (once per 24 hours)
- Manual sync via
/openrouter-synccommand - File-based caching for resilience
- No API key required (uses public endpoint)
- Only adds new models, never overwrites existing configurations
npm install -g opencode-openrouter-syncThen add the plugin to your OpenCode config at ~/.config/opencode/opencode.json:
{
"plugin": ["opencode-openrouter-sync"]
}Clone this repository and build:
git clone https://github.com/tbui17/opencode-openrouter-sync.git
cd opencode-openrouter-sync
npm install
npm run buildCopy the built plugin to your OpenCode plugins directory:
mkdir -p ~/.config/opencode/plugins
cp dist/index.js ~/.config/opencode/plugins/openrouter-sync.jsAdd to your config:
{
"plugin": ["openrouter-sync"]
}-
Install the plugin:
npm install -g opencode-openrouter-sync
-
Add to your OpenCode config (
~/.config/opencode/opencode.json):{ "plugin": ["opencode-openrouter-sync"] } -
Restart OpenCode — models will sync automatically on first run.
The plugin automatically syncs models on first startup. After that, it checks if 24 hours have passed since the last successful sync and runs automatically when you start OpenCode.
Run the manual sync command anytime:
/openrouter-sync
This forces an immediate sync regardless of when the last sync occurred.
You can also use the sync functionality programmatically in your own code:
import { syncModels, fetchModels } from 'opencode-openrouter-sync/sync';
// Full sync with config
const result = await syncModels();
console.log(`Added ${result.added} models, skipped ${result.skipped}`);
// Fetch models without updating config
const models = await fetchModels();
console.log(`Fetched ${models?.length ?? 0} models from OpenRouter`);Note: Programmatic functions are available via the
/syncsubpath to avoid conflicts with OpenCode's plugin loading mechanism.
After a sync, you can verify the models were added:
cat ~/.config/opencode/opencode.json | jq '.provider.openrouter.models | keys | length'This should show a number greater than 400, representing all available OpenRouter models.
List all synced OpenRouter models:
cat ~/.config/opencode/opencode.json | jq '.provider.openrouter.models | to_entries | .[].key'Check a specific model:
cat ~/.config/opencode/opencode.json | jq '.provider.openrouter.models["openai/gpt-4o"]'The plugin works out of the box without any configuration. However, you can customize behavior in your config file.
- Global config:
~/.config/opencode/opencode.json - Cache directory:
~/.local/share/opencode/openrouter-sync/cache.json
Add options under provider.openrouter in your config:
{
"plugin": ["opencode-openrouter-sync"],
"provider": {
"openrouter": {
"options": {
"cacheTtl": 86400000,
"enabled": true
}
}
}
}| Option | Type | Default | Description |
|---|---|---|---|
cacheTtl |
number | 86400000 | Cache TTL in milliseconds (24 hours) |
enabled |
boolean | true | Whether to run sync on startup |
The plugin adds models under provider.openrouter.models in your global config. Each model includes:
id- Model identifiername- Human-readable namecontextWindow- Maximum context lengthpricing- Input/output pricing informationmax_completion_tokens- Maximum completion lengthsupported_parameters- Parameters the model supportsdefault_parameters- Default parameter valuesis_moderated- Whether the model has content moderation enabled
Example model entry:
{
"id": "openai/gpt-4o",
"name": "GPT-4o",
"contextWindow": 128000,
"pricing": { "prompt": "2.50", "completion": "10.00" },
"max_completion_tokens": 16384,
"supported_parameters": ["temperature", "max_tokens", "top_p"],
"default_parameters": { "temperature": 0.7 },
"is_moderated": true
}- Only adds models that don't already exist
- Never removes or overwrites existing models
- Preserves any custom model configurations you have
- Check that the plugin file exists at
~/.config/opencode/plugins/openrouter-sync.js - Verify the plugin is listed in your config under
"plugin" - Restart OpenCode after making config changes
- Check OpenCode logs for any error messages
- Verify you have internet access to reach
openrouter.ai - Try running
/openrouter-syncmanually to see error details
- Run
/openrouter-syncmanually - Check that
~/.config/opencode/opencode.jsonis writable - Look for errors in the OpenCode console
If you need to force a fresh sync:
rm ~/.local/share/openrouter-sync/cache.jsonThen restart OpenCode or run /openrouter-sync.
# Install dependencies
npm install
# Build the plugin
npm run build
# Run tests
npm test
# Clean build artifacts
npm run cleanMIT