Go SDK

Official OpenRouter Go SDK documentation

The Go SDK and docs are currently in beta. Report issues on GitHub.

The OpenRouter Go SDK is a type-safe client for building AI applications with access to 400+ language models through a unified API.

Why use the OpenRouter SDK?

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.

1package main
2
3import (
4 "context"
5 "log"
6 "os"
7
8 openrouter "github.com/OpenRouterTeam/go-sdk"
9 "github.com/OpenRouterTeam/go-sdk/models/components"
10 "github.com/OpenRouterTeam/go-sdk/optionalnullable"
11)
12
13func main() {
14 ctx := context.Background()
15
16 s := openrouter.New(
17 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
18 )
19
20 res, err := s.Chat.Send(ctx, components.ChatRequest{
21 Model: openrouter.Pointer("openai/gpt-4o"),
22 Messages: []components.ChatMessages{
23 components.CreateChatMessagesUser(
24 components.ChatUserMessage{
25 Role: components.ChatUserMessageRoleUser,
26 Content: components.CreateChatUserMessageContentStr(
27 "Explain quantum computing",
28 ),
29 },
30 ),
31 },
32 Temperature: optionalnullable.From(openrouter.Pointer(0.7)),
33 }, nil)
34 if err != nil {
35 log.Fatal(err)
36 }
37 if res != nil && res.ChatResult != nil {
38 log.Println(res.ChatResult.Choices)
39 }
40}

The SDK provides three core benefits:

Auto-generated from API specifications

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.

1res, err := s.Chat.Send(ctx, components.ChatRequest{
2 Model: openrouter.Pointer("openai/gpt-4o"),
3 // ...
4}, nil)

Type-safe by default

Request and response types live under models/components and models/operations. API errors are mapped to typed values under models/sdkerrors.

1res, err := s.Chat.Send(ctx, components.ChatRequest{
2 Model: openrouter.Pointer("openai/gpt-4o"),
3 Messages: []components.ChatMessages{
4 components.CreateChatMessagesUser(
5 components.ChatUserMessage{
6 Role: components.ChatUserMessageRoleUser,
7 Content: components.CreateChatUserMessageContentStr("Hello"),
8 },
9 ),
10 },
11}, nil)

Streaming and platform APIs

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.

1res, err := s.Chat.Send(ctx, components.ChatRequest{
2 Model: openrouter.Pointer("openai/gpt-4o"),
3 Messages: []components.ChatMessages{ /* ... */ },
4 Stream: openrouter.Pointer(true),
5}, nil)
6if res != nil && res.EventStream != nil {
7 defer res.EventStream.Close()
8 for res.EventStream.Next() {
9 chunk := res.EventStream.Value()
10 if chunk == nil {
11 continue
12 }
13 for _, choice := range chunk.Data.Choices {
14 if text, ok := choice.Delta.Content.Get(); ok && text != nil {
15 _ = *text
16 }
17 }
18 }
19}

See examples/chat-stream for a runnable streaming chat example.

Installation

$go get github.com/OpenRouterTeam/go-sdk

For beta releases, pin an explicit version:

$go get github.com/OpenRouterTeam/go-sdk@v0.5.0

Requirements: Go 1.25 or higher

Get your API key from openrouter.ai/settings/keys.

Quick start

See examples/README.md for runnable examples, starting with examples/chat, or the API Reference for the full method list.