---
title: "OpenCode"
description: "Use plaingateway, the free OpenRouter alternative, in OpenCode: paste one provider block, pay the provider's list price for tokens and nothing on top."
---

# OpenCode

OpenCode is a coding agent for the terminal. It talks to any OpenAI-compatible API through a custom provider, so plaingateway needs one block in its config file and one key. Model ids are OpenRouter's, for example `openai/gpt-5.6-luna`.

## Paste this

```json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "plaingateway": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "plaingateway",
      "options": {
        "baseURL": "https://api.plaingateway.com/api/v1",
        "apiKey": "{env:PLAINGATEWAY_API_KEY}"
      },
      "models": {
        "openai/gpt-5.6-luna": {
          "name": "GPT-5.6 Luna"
        },
        "deepseek/deepseek-v4.1-flash": {
          "name": "DeepSeek V4.1 Flash"
        }
      }
    }
  }
}
```

## Where the file lives

- For every project: `~/.config/opencode/opencode.json`.
- For one project: `opencode.json` at the project root. The key stays out of the file, so the file is safe to commit.

If the file already has a `provider` object, add the `plaingateway` entry inside it. Then set the key in your shell and start OpenCode:

```sh
export PLAINGATEWAY_API_KEY="sk-pg-v1-..."
opencode
```

Run `/models` and pick `plaingateway/openai/gpt-5.6-luna`. The part after `plaingateway/` is the model id the gateway receives.

## Get a key

1. Sign up at [plaingateway.com/signup](https://plaingateway.com/signup) and click the link in the confirmation email.
2. Open [Keys](https://plaingateway.com/settings/keys), press **New key** and copy it. It starts with `sk-pg-v1-` and is shown once.
3. Add credits by card on [Credits](https://plaingateway.com/settings/credits). Requests are paid from it.

## Check it works

Send one request to the same base URL with the same model:

```sh
curl -i https://api.plaingateway.com/api/v1/chat/completions \
  -H "Authorization: Bearer $PLAINGATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.6-luna",
    "max_tokens": 32,
    "messages": [
      {"role": "user", "content": "Say hello in five words."}
    ]
  }'
```

A 200 means the key and the base URL work. Three things in the answer tell you what happened: the `X-PG-Host` header names the provider that served it, `X-PG-Cost-USD` is the charge, and `usage.cost` in the body is the same charge. The request also shows on the [Activity](https://plaingateway.com/activity) page.

## What it costs

You pay the provider's official list price for tokens, and plaingateway adds nothing on top: 0% platform fee, no markup, no subscription, free with your own provider keys. You can earn up to 20% bonus credit, added to your balance when you add money and never taken off the token price: each person you invite gives you 1% bonus credit once they have paid $20 in total, up to 20%, so $100 paid gets up to $120 of credit. The person you invite gets 5% bonus credit on their first top-up, and you earn 10% of what they pay ([How referral works](../referral/how-it-works.md)). Each model's price is on [Models](https://plaingateway.com/models).

## Limits today

- **Only `openai/` and `deepseek/` models answer today.** [Models](https://plaingateway.com/models) lists more ids; any other id returns an error. Add more ids under `models` the same way, for example `deepseek/deepseek-v4-pro` or `openai/gpt-6.1-sol`.
- **Credit is prepaid.** Requests are paid from a balance you add by card. With no balance the gateway answers 402. Each request first holds its largest possible cost, the prompt plus the maximum output; on a low balance it holds an eighth of what is left and shortens the answer to fit, so a long session can get shorter answers, then 402, before the balance is spent.

## Gotchas

**The base URL ends in `/api/v1`.** OpenCode adds `/chat/completions` itself. A base URL with `/chat/completions` already on it gets 404.

**`/models` shows nothing new.** OpenCode reads its config at start. Quit and start it again after editing the file, and check the file is valid JSON.

**A 401 means the key.** `{env:PLAINGATEWAY_API_KEY}` reads the variable from the shell that started OpenCode. Export it in that shell, or in your shell profile.

## Moving from OpenRouter

Keep your model ids. Change the base URL from `https://openrouter.ai/api/v1` to `https://api.plaingateway.com/api/v1` and the key to a plaingateway key. The full guide: [Migrate from OpenRouter](../migrate/from-openrouter.md).

OpenCode's own reference for this setting: [opencode.ai/docs/providers](https://opencode.ai/docs/providers/).
