Skip to main content

openrouter-go

Zero-dependency Go client for the OpenRouter API

go get github.com/hra42/openrouter-go

Zero dependencies

Pure Go standard library. No transitive supply chain, no version churn — go get and you're done.

Full API coverage

Chat, legacy completions, streaming, tool calling, structured outputs, multimodal inputs (image / audio / PDF / text), embeddings, and the Anthropic-compatible Messages endpoint.

Streaming done right

SSE streams with channel-based iteration, context cancellation, and automatic retry with exponential backoff.

Functional options

One pattern, everywhere. WithModel, WithTools, WithResponseFormat — discoverable, composable, extensible.

Typed errors

*RequestError with IsRateLimitError, IsAuthenticationError, IsContextLengthError, and more.

Agent-friendly

llms.txt, AGENTS.md, and a machine-readable api-surface.json so Claude Code and friends can build on the SDK without crawling.

Quick start

package main

import (
"context"
"fmt"
"log"
"os"

"github.com/hra42/openrouter-go"
)

func main() {
client := openrouter.NewClient(
openrouter.WithAPIKey(os.Getenv("OPENROUTER_API_KEY")),
)

resp, err := client.ChatComplete(context.Background(),
[]openrouter.Message{openrouter.CreateUserMessage("Hello")},
openrouter.WithModel("openai/gpt-4o-mini"),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}