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@latestgo
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-4openai/gpt-4ogoogle/gemini-2.5-prometa-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
| Option | Type | Description |
|---|---|---|
WithAPIKey(key) | string | Set a static API key |
WithTokenSource(ts) | provider.TokenSource | Set a dynamic token source |
WithBaseURL(url) | string | Override the default https://openrouter.ai/api/v1 endpoint |
WithHeaders(h) | map[string]string | Set additional HTTP headers |
WithHTTPClient(c) | *http.Client | Set a custom *http.Client |
Notes
- Supports image inputs (depends on the underlying model).
- Automatically sends
HTTP-RefererandX-Titleheaders as recommended by OpenRouter's API. - Usage reporting is enabled by default (
usage: {include: true}in request body). - Environment variable
OPENROUTER_BASE_URLcan override the default endpoint.