---
title: "Deploy 9Router OpenCode"
description: "Point OpenCode CLI at your self-hosted AI router"
category: "AI/ML"
url: https://railway.com/deploy/deploy-9router-for-opencode-cli
---

# Deploy 9Router OpenCode

Point OpenCode CLI at your self-hosted AI router

**[Deploy 9Router OpenCode on Railway](https://railway.com/template/deploy-9router-for-opencode-cli)**

Machine-readable deploy manifest (JSON, validated by TemplateCI): https://railway.com/deploy/deploy-9router-for-opencode-cli/manifest.json

- **Creator:** onepush
- **Category:** AI/ML

## Template content

### 9router

- **Image:** decolua/9router:0.5.95@sha256:4316fefb95ea642d57db885d906b1227b1768b15ac5def314621fd781da7b3f1
- **Health check:** /api/health
- **Public domain:** Yes

## Documentation

# Deploy and Host self hosted 9Router OpenCode (Open-Source AI Router) on Railway

Deploy self-hosted 9Router on Railway, then point OpenCode's terminal agent at one endpoint. Add a custom provider to opencode.json with baseURL https://your-domain/v1 and an env key. Models keyed by combo names route long sessions with fallback.

## About Hosting 9Router OpenCode open-source software on Railway (self hosted 9Router template)

This template runs one pinned decolua/9router:0.5.95 container on port 20128, with SQLite state and automatic backups on the /app/data volume. Point OpenCode at the Railway-assigned domain's /v1 and use /dashboard to manage it.

## Why Deploy 9Router OpenCode, the OpenRouter alternative on Railway (Railway Free Trial)

You skip OpenRouter's credit-purchase fee and keep provider keys, quota state, and usage history on a box you control. The Railway $5 trial is enough to try it, and after that the service runs roughly $3-8/mo while providers bill you directly.

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 9Router OpenCode 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 9Router OpenCode self hosting

Railway handles the container, volume, TLS, and health checks, while classic VPS options hand you a bare VM to configure.

| Provider | What You Get with Railway | What You Get with the Other Provider |
| --- | --- | --- |
| DigitalOcean | One-click deploy from template, public HTTPS, persistent /app/data volume, $5 trial credit | Droplet plus manual Docker install, reverse proxy, firewall, and volume setup |
| AWS | Same managed container and health endpoint, no IAM or VPC work | EC2 or ECS with security groups, load balancer, TLS certs, and storage config |
| Hetzner | Public endpoint and backups without touching a server | Cheap bare metal or cloud VM requiring full self-host setup and monitoring |

## Common Use Cases for hosted 9Router OpenCode

- **One opencode.json for laptop and CI:** Point both at the same 9router custom provider block with baseURL https://your-domain/v1 and {env:NINE_ROUTER_KEY}; no per-machine edits.
- **Fallback combos for long refactors:** Build a combo that fails over from a paid provider key to a cheap tier, then a free one. Pick it with /models and keep a long session alive when a quota dies mid-refactor.
- **One router shared by a team's OpenCode installs:** Give each developer their own API key on the same Railway-hosted 9Router, share combos, and let everyone use /models without touching provider credentials.
- **Usage history per key:** Check the dashboard to see tokens and cost per key, so you can spot which OpenCode session ate the budget before the next sprint.
- **Mixing paid and free tiers:** Map combos to paid keys, cheap subscription accounts, and free models, then switch in /models instead of editing env vars or restarting OpenCode.

## Dependencies for 9Router OpenCode Docker hosted on Railway

The whole stack is one pinned Docker image and one SQLite file. There is no Postgres, Redis, or queue worker to babysit. Railway gives you the volume, the public HTTPS endpoint, and the health check; 9Router brings the rest.

### Deployment Dependencies for Managed 9Router OpenCode Service (AI Router)

- Image: decolua/9router:0.5.95, pinned by digest, listening on port 20128 with /api/health.
- Volume: /app/data, holding SQLite at /app/data/db/data.sqlite plus automatic backups in /app/data/db/backups/.
- Secrets: INITIAL_PASSWORD for first dashboard login, JWT_SECRET, API_KEY_SECRET, and MACHINE_ID_SALT.
- Fixed environment: PORT=20128, HOSTNAME=0.0.0.0, DATA_DIR=/app/data, BASE_URL and NEXT_PUBLIC_BASE_URL set to the Railway domain, CLOUD_URL for 9Router cloud sync, AUTH_COOKIE_SECURE=true, REQUIRE_API_KEY=true. ENABLE_REQUEST_LOGS is off by default. Optional HEADROOM_URL and SEARXNG_URL stay empty.

### Implementation Details for 9Router OpenCode (Using 9Router official docker image)

OpenCode hits the same /v1 endpoint as every other client. SQLite stores providers, combos, API keys, settings, and usage history. Backups are automatic.

## How does 9Router OpenCode compare against other AI gateway platforms

### 9Router vs OpenRouter (OpenRouter Alternative)

- **Self-hosted endpoint:** OpenCode hits your Railway /v1 URL, so keys and usage stay yours. OpenRouter is hosted and adds a fee on credits.
- **Fallback combos:** 9Router fails a session over across your accounts. OpenRouter has fallback but you don't control those accounts.
- **Where OpenRouter wins:** No deploy work and a broad catalog from day one.

### 9Router vs LiteLLM (LiteLLM Alternative)

- **OpenCode fit:** The 9Router /v1 endpoint works directly with @ai-sdk/openai-compatible. LiteLLM needs more mapping and routing config.
- **Account routing:** 9Router handles consumer subscriptions plus API keys. LiteLLM is mostly enterprise key traffic.
- **Where LiteLLM wins:** More mature at scale and deeper provider integrations.

### 9Router vs OmniRoute (OmniRoute Alternative)

- **Terminal focus:** 9Router combos map cleanly into OpenCode's model list. OmniRoute also exposes /v1, so either works; pick the dashboard you prefer.
- **Small footprint:** One SQLite-backed Railway container includes dashboard, keys, and usage history.
- **Where OmniRoute wins:** If you already run it and like its rules, switching adds little.

### 9Router vs OpenCode Zen (OpenCode Zen Alternative)

- **No extra subscription:** 9Router is MIT-licensed and uses accounts you own. Zen is a paid curated model list.
- **Model control:** Your opencode.json models come from 9Router combos. Zen gives tested models but no combo control.
- **Where Zen wins:** Zero-config, officially tested for OpenCode, and no consumer subscription terms risk.

## How to use 9Router OpenCode (the OSS AI router)?

1. Open /dashboard and log in with INITIAL_PASSWORD from Railway variables.
2. Connect providers, then build a combo with fallback.
3. Create an API key and export it as NINE_ROUTER_KEY.
4. Add this block to opencode.json, matching combo names to GET /v1/models:

```json
{"provider":{"9router":{"npm":"@ai-sdk/openai-compatible","name":"9Router","options":{"baseURL":"https://your-domain/v1","apiKey":"{env:NINE_ROUTER_KEY}"},"models":{"combo":{"name":"My combo","limit":{"context":200000,"output":65536}}}}}}
```

5. Save the file, restart OpenCode, pick the model with /models, run a session, then check usage in the dashboard.

## How to self host 9Router OpenCode on other VPS Services (9Router self hosting guide)

### Clone the Repository

Clone the source from the 9Router GitHub repository and enter the directory.

```bash
git clone https://github.com/decolua/9router.git
cd 9router
```

### Install Dependencies

Install Node.js and npm; the source build needs about 2 GB of memory. Or skip the build and run the decolua/9router:0.5.95 image with /app/data mounted.

```bash
npm install
```

### Configure Environment Variables

Copy the example env file and set required values: DATA_DIR=/app/data, PORT=20128, INITIAL_PASSWORD, JWT_SECRET, API_KEY_SECRET, and MACHINE_ID_SALT. Also set BASE_URL to https://your-domain.

### Start the 9Router OpenCode Application

Build and start the server.

```bash
npm run build
npm start
```

The dashboard stays at /dashboard on port 20128. Railway handles the container, volume, health check, and public HTTPS for you, so you skip these steps entirely.

## Official Pricing of 9Router OpenCode (9Router pricing)

9Router is free and MIT licensed; there is no paid plan or subscription for the router itself, including its optional cloud sync feature. You only pay your AI providers and whatever infrastructure you run it on.

## 9Router OpenCode cloud vs self hosted comparison (Pricing, features, costs, and more)

Self hosting puts the same routing logic on your own Railway domain, but the tradeoff is your time and a small monthly bill instead of just pointing OpenCode at a managed endpoint.

### Monthly cost of self hosting 9Router OpenCode on Railway

Expect roughly $3-8/mo on Railway for one container and its volume. There is no paid 9Router plan, and the dashboard's cost tracking is informational only. You still pay whatever your connected providers charge for tokens.

### System Requirements for Hosting 9Router OpenCode on a VPS

About 512 MB of RAM is fine for a single OpenCode user, and SQLite means no Postgres or Redis to run alongside it. The container listens on port 20128, persists state under /app/data, and exposes /api/health for checks.

## Frequently Asked Questions (FAQs)

### Where does opencode.json live?

In your project root, or global at ~/.config/opencode/opencode.json. Put the 9router block in either; project config wins.

### Why does /models not show my combo?

Model IDs must match what GET /v1/models returns. Check the provider id is 9router, baseURL ends in /v1, then restart OpenCode.

### Should I use the chat completions or responses package?

Use @ai-sdk/openai-compatible for /v1/chat/completions. For /v1/responses models, use @ai-sdk/openai instead.

### Can CI use the same key?

Yes. With REQUIRE_API_KEY=true, any laptop, dev box, or CI runner shares the public baseURL. Use separate keys to split usage history.

### Does the router see my code?

Yes, prompts and responses pass through 9Router. SQLite stores config and usage history, not code; logs are off unless you enable them.

### What happens if the router is down?

OpenCode shows connection errors. Your config and key stay valid, so sessions resume when Railway restarts; keep a direct provider block as backup.


## Similar templates

- [Chat Chat](https://railway.com/deploy/-WWW5r) — Chat Chat, your own unified chat and search to AI platform.
- [stella](https://railway.com/deploy/stella) — Self-host stella with web, API, Postgres, Redis, and object storage.
- [Hermes Agent | OpenClaw Alternative with Dashboard](https://railway.com/deploy/hermes-agent-or-openclaw-alternative-wit) — Self-Hosted Hermes AI Agent for Telegram, Discord & Slack

Open this page in a browser: https://railway.com/deploy/deploy-9router-for-opencode-cli
