Go SDK
Use nRouter with the official OpenAI Go SDK
Last updated
nRouter works directly with the official Go client module, verified on pkg.go.dev. Set the base URL to https://api.nrouter.ai/v1 and pass your sk-nrouter-... key — guardrails, caching, smart routing, and rate-limits auto-apply from your org config.
Installation
go get github.com/nRouterAI/nrouter-sdk/sdks/go/v2@v2.2.1Package Reference: pkg.go.dev/github.com/nRouterAI/nrouter-sdk/sdks/go/v2
Setup
Export your virtual API key in your environment:
export NROUTER_API_KEY="sk-nrouter-your-key-here"Initialize the client with your virtual key and nRouter base URL:
import (
"os"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
client := openai.NewClient(
option.WithAPIKey(os.Getenv("NROUTER_API_KEY")),
option.WithBaseURL("https://api.nrouter.ai/v1"),
)Chat Completion
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