Skip to navigation

Use OpenCode with Fabric AI Gateway

You can send OpenCode traffic through Fabric AI Gateway by adding the gateway as a custom provider in OpenCode’s opencode.json file and passing a gateway key as a custom header. The gateway then applies your plugins to OpenCode requests, and you can review them in the console. This topic shows you how to configure OpenCode and confirm that requests arrive.

Prerequisites

Before you begin, make sure you have the following:

  • OpenCode installed on your computer.
  • A route for an OpenAI connection. For the steps, see Connect an LLM provider.
  • A gateway key for the route. The gateway reads it from the fg-api-key header. For more information, see Authenticate requests with Key Auth.
  • The ID of a model that the connection allows.

Configure OpenCode to use the gateway

OpenCode reads its settings from opencode.json. To apply the settings to every project, use ~/.config/opencode/opencode.json. To limit them to one project, use opencode.json in the project root. To configure OpenCode, do the following:

  1. In the Fabric console, select LLMs > Routes, find your OpenAI route, and then select Copy LLM base URL.

  2. Set your gateway key in an environment variable, for example in your shell profile:

    export FABRIC_GATEWAY_KEY="GATEWAY_KEY"
  3. Open opencode.json, and add the gateway as a provider:

    {
    "$schema": "https://opencode.ai/config.json",
    "provider": {
    "fabric": {
    "npm": "@ai-sdk/openai-compatible",
    "name": "Fabric AI Gateway",
    "options": {
    "baseURL": "BASE_URL",
    "headers": {
    "fg-api-key": "{env:FABRIC_GATEWAY_KEY}"
    }
    },
    "models": {
    "MODEL_ID": {
    "name": "MODEL_NAME"
    }
    }
    }
    }
    }
  4. Replace GATEWAY_KEY with your gateway key, BASE_URL with the URL that you copied, ending in /v1, MODEL_ID with the ID of a model that the connection allows, and MODEL_NAME with the name that you want OpenCode to show.

  5. Restart OpenCode, and then select the model with the /models command.

The settings do the following:

  • npm makes OpenCode talk to the gateway in the OpenAI-compatible chat completions format.
  • baseURL sends requests to the gateway route instead of the OpenAI API.
  • headers adds the gateway key to each request. The {env:FABRIC_GATEWAY_KEY} value reads the key from the environment variable, which keeps the key out of the file.
  • models lists the models that appear in OpenCode. Each ID must match a model that the connection allows.

Treat the gateway key as a secret. Don’t put it in a config file that you commit.

The gateway supplies the OpenAI credential. It adds the API key saved on the connection when it forwards requests to OpenAI, so OpenCode doesn’t need an OpenAI key.

Confirm that requests pass through the gateway

To confirm that OpenCode uses the gateway, do the following:

  1. In OpenCode, send a message, for example Hello, is this working?.
  2. In the Fabric console, select Admin, and then select Traces.
  3. Find the requests from your user on the route.

Next steps