The OpenRouter Go SDK is a type-safe client for building AI applications with access to 400+ language models through a unified API.
Integrating AI models into applications involves handling different provider APIs, managing model-specific requirements, and avoiding common implementation mistakes. The OpenRouter SDK standardizes these integrations with idiomatic Go types, retries, and typed errors.
package main
import (
"context"
"log"
"os"
openrouter "github.com/OpenRouterTeam/go-sdk"
"github.com/OpenRouterTeam/go-sdk/models/components"
"github.com/OpenRouterTeam/go-sdk/optionalnullable"
)
func main() {
ctx := context.Background()
s := openrouter.New(
openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
)
res, err := s.Chat.Send(ctx, components.ChatRequest{
Model: openrouter.Pointer("openai/gpt-4o"),
Messages: []components.ChatMessages{
components.CreateChatMessagesUser(
components.ChatUserMessage{
Role: components.ChatUserMessageRoleUser,
Content: components.CreateChatUserMessageContentStr(
"Explain quantum computing",
),
},
),
},
Temperature: optionalnullable.From(openrouter.Pointer(0.7)),
}, nil)
if err != nil {
log.Fatal(err)
}
if res != nil && res.ChatResult != nil {
log.Println(res.ChatResult.Choices)
}
}The SDK provides three core benefits:
The SDK is automatically generated from OpenRouter's OpenAPI specs and updated with every API change. New models, parameters, and features appear in generated types and docs immediately.
res, err := s.Chat.Send(ctx, components.ChatRequest{
Model: openrouter.Pointer("openai/gpt-4o"),
// ...
}, nil)Request and response types live under models/components and models/operations. API errors are mapped to typed values under models/sdkerrors.
res, err := s.Chat.Send(ctx, components.ChatRequest{
Model: openrouter.Pointer("openai/gpt-4o"),
Messages: []components.ChatMessages{
components.CreateChatMessagesUser(
components.ChatUserMessage{
Role: components.ChatUserMessageRoleUser,
Content: components.CreateChatUserMessageContentStr("Hello"),
},
),
},
}, nil)Use the same client for streaming chat completions, embeddings, rerank, TTS, video generation, and platform APIs such as API keys, credits, models, guardrails, and workspaces.
res, err := s.Chat.Send(ctx, components.ChatRequest{
Model: openrouter.Pointer("openai/gpt-4o"),
Messages: []components.ChatMessages{ /* ... */ },
Stream: openrouter.Pointer(true),
}, nil)
if res != nil && res.EventStream != nil {
defer res.EventStream.Close()
for res.EventStream.Next() {
chunk := res.EventStream.Value()
if chunk == nil {
continue
}
for _, choice := range chunk.Data.Choices {
if text, ok := choice.Delta.Content.Get(); ok && text != nil {
_ = *text
}
}
}
}See examples/chat-stream for a runnable streaming chat example.
go get github.com/OpenRouterTeam/go-sdkFor beta releases, pin an explicit version:
go get github.com/OpenRouterTeam/go-sdk@v0.5.12Requirements: Go 1.25 or higher
Get your API key from openrouter.ai/settings/keys.
See examples/README.md for runnable examples, starting with examples/chat, or the API Reference for the full method list.