Runnable examples for the OpenRouter Go SDK. Each subdirectory is a self-contained module with its own go.mod.
- Go 1.25 or higher
- An OpenRouter API key from openrouter.ai/settings/keys
export OPENROUTER_API_KEY="your-api-key"| Directory | SDK client | Run |
|---|---|---|
chat |
client.Chat.Send |
cd examples/chat && go run . |
chat-stream |
streaming chat | cd examples/chat-stream && go run . |
models |
client.Models.List |
cd examples/models && go run . |
model |
client.Models.Get |
cd examples/model && go run . |
embed |
client.Embeddings.Generate |
cd examples/embed && go run . |
rerank |
client.Rerank.Rerank |
cd examples/rerank && go run . |
providers |
client.Providers.List |
cd examples/providers && go run . |
generation |
client.Generations.GetGeneration |
see below |
Start with chat if you are new to the SDK.
The generation example looks up metadata for a prior request. Run chat first, then pass the completion id:
cd examples/chat && go run .
export OPENROUTER_GENERATION_ID="gen-..."
cd ../generation && go run .Each example pins a released SDK version in go.mod:
require github.com/OpenRouterTeam/go-sdk v0.5.0This should match the version in README.md and .speakeasy/gen.lock releaseVersion. CI runs scripts/bump-examples.sh after releases to keep these in sync.
To use a different version:
go get github.com/OpenRouterTeam/go-sdk@v0.5.0
go run .Do not add a replace directive when copying an example into your own project.
When working inside this repository, the root go.work file wires examples to the local SDK checkout automatically. No per-example replace directives are needed.
If you clone the repo and examples fail to resolve the SDK module, run commands from anywhere inside the repository so Go picks up go.work.
Add a new subdirectory under examples/ with its own go.mod. Examples in this directory are not overwritten by Speakeasy generation.
New examples should:
require github.com/OpenRouterTeam/go-sdkat a pinned release version- Be listed in the root
go.workfile for local development
When Speakeasy bumps .speakeasy/gen.lock, CI runs scripts/bump-examples.sh on main to sync example pins and docs with releaseVersion.
Locally:
scripts/bump-examples.sh # reads releaseVersion from .speakeasy/gen.lock
scripts/bump-examples.sh v0.6.0 # or pass an explicit version
scripts/verify-examples.sh # compile + version alignment checksPull requests that change SDK code or examples must pass scripts/verify-examples.sh in CI.