Skip to content

OpenRouter

OpenRouter multi-provider routing gateway using the OpenAI-compatible Chat Completions API. Access models from multiple providers through a single API key.

Setup

bash
go get github.com/zendev-sh/goai@latest
go
import "github.com/zendev-sh/goai/provider/openrouter"

Set the OPENROUTER_API_KEY environment variable, or pass WithAPIKey() directly.

Models

Models use a provider/model prefix format:

  • anthropic/claude-sonnet-4
  • openai/gpt-4o
  • google/gemini-2.5-pro
  • meta-llama/llama-3.3-70b-instruct

See openrouter.ai/models for the full catalog.

Tested Models

Unit tested (mock HTTP server, 2026-03-15): anthropic/claude-sonnet-4

Usage

go
model := openrouter.Chat("anthropic/claude-sonnet-4")

result, err := goai.GenerateText(ctx, model, goai.WithPrompt("Hello"))
if err != nil {
    log.Fatal(err)
}
fmt.Println(result.Text)

Options

OptionTypeDescription
WithAPIKey(key)stringSet a static API key
WithTokenSource(ts)provider.TokenSourceSet a dynamic token source
WithBaseURL(url)stringOverride the default https://openrouter.ai/api/v1 endpoint
WithHeaders(h)map[string]stringSet additional HTTP headers
WithHTTPClient(c)*http.ClientSet a custom *http.Client

Notes

  • Supports image inputs (depends on the underlying model).
  • Automatically sends HTTP-Referer and X-Title headers as recommended by OpenRouter's API.
  • Usage reporting is enabled by default (usage: {include: true} in request body).
  • Environment variable OPENROUTER_BASE_URL can override the default endpoint.

Released under the MIT License.