Go SDK
Integrate Go applications with nRouter using our official Go SDK or OpenAI Go client. Access typed helpers, automatic cost tracking, and resilient routing.
Last updated
nRouter delivers a first-class, idiomatic Go SDK verified on pkg.go.dev, with automated x-nr-* cost tracking and typed helpers. nRouter is also 100% compatible with the official OpenAI Go SDK (github.com/openai/openai-go).
1. Official nRouter Go SDK (Recommended)
Installation
go get github.com/nRouterAI/nrouter-sdk/sdks/go/v3@v3.0.0Package Reference: pkg.go.dev/github.com/nRouterAI/nrouter-sdk/sdks/go/v3
Setup & Usage
Export your virtual API key in your environment:
export NROUTER_API_KEY="sk-nrouter-your-key-here"Initialize the client with NewFromEnv() and make a chat completion:
package main
import (
"context"
"fmt"
"log"
nrouter "github.com/nRouterAI/nrouter-sdk/sdks/go/v3"
)
func main() {
// Reads NROUTER_API_KEY from environment and targets https://api.nrouter.ai/v1
client, err := nrouter.NewFromEnv()
if err != nil {
log.Fatal(err)
}
res, err := client.ChatCompletions(context.Background(), map[string]any{
"model": "gpt-5.4-mini",
"messages": []any{
map[string]any{"role": "user", "content": "Hello, nRouter!"},
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(res.Body["choices"])
if res.Meta.Cost != nil {
fmt.Printf("Cost: $%v (%s)\n", *res.Meta.Cost, res.Meta.CostStatus)
}
}2. OpenAI Go SDK (Alternative)
If your application already uses github.com/openai/openai-go, point the client to nRouter by configuring WithBaseURL:
Installation
go get github.com/openai/openai-goSetup & Usage
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
func main() {
client := openai.NewClient(
option.WithAPIKey(os.Getenv("NROUTER_API_KEY")),
option.WithBaseURL("https://api.nrouter.ai/v1"),
)
response, err := client.Chat.Completions.New(context.Background(),
openai.ChatCompletionNewParams{
Model: "gpt-5.4-mini",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("Hello, nRouter!"),
},
},
)
if err != nil {
log.Fatalf("Error: %v", err)
}
fmt.Println(response.Choices[0].Message.Content)
}Per-Request Overrides
The Go SDK does not yet support extra_body fields natively. To pass nrouter_* fields per request (prompt templates, cache toggle), use a raw HTTP POST and include them in the JSON body:
{
"model": "gpt-5.5",
"messages": [{"role": "user", "content": "Summarize Q1 earnings..."}],
"nrouter_prompt_template_id": "your-summarizer-id",
"nrouter_prompt_variables": {"language": "Spanish"},
"nrouter_cache": false
}Guardrails are not part of this body. You assign them in the dashboard at key, team, or organization scope — the narrowest scope that mentions a guardrail wins — and they run automatically on every request that scope covers.
Response Headers
Every successful response carries:
x-nr-request-id— id for this call, and the join key for its spend rowx-nr-model— the model that actually served the requestx-nr-cost-status—exactwhen we priced the call,unpricedwhen we could notx-nr-request-cost— USD spend for this call. Absent whenx-nr-cost-statusisunpriced: nRouter never reports a cost of0for a call it could not pricex-nr-input-tokens,x-nr-output-tokens,x-nr-total-tokens— token counts as reported by the provider
Next Steps
- cURL Examples — Inspect raw request/response shape
- Authentication — API key best practices
- Chat Completions API — Full API reference