This library provides unofficial Go client for Openrouter API
go get github.com/revrost/go-openrouter
package main
import (
"context"
"fmt"
openrouter "github.com/revrost/go-openrouter"
)
func main() {
client := openrouter.NewClient(
"your token",
openrouter.WithXTitle("My App"),
openrouter.WithHTTPReferer("https://myapp.com"),
)
resp, err := client.CreateChatCompletion(
context.Background(),
openrouter.ChatCompletionRequest{
Model: openrouter.DeepseekV3,
Messages: []openrouter.ChatCompletionMessage{
{
Role: openrouter.ChatMessageRoleUser,
Content: openrouter.Content{Text: "Hello!"},
},
},
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return
}
fmt.Println(resp.Choices[0].Message.Content)
}
- Visit the openrouter website at https://openrouter.ai/docs/quick-start.
- If you don't have an account, click on "Sign Up" to create one. If you do, click "Log In".
- Once logged in, navigate to your API key management page.
- Click on "Create new secret key".
- Enter a name for your new key, then click "Create secret key".
- Your new API key will be displayed. Use this key to interact with the openrouter API.
Note: Your API key is sensitive information. Do not share it with anyone.
For deepseek models, sometimes its better to use openrouter integration feature and pass in your own API key into the control panel for better performance, as openrouter will use your API key to make requests to the underlying model which potentially avoids shared rate limits.
JSON Schema for function calling
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
Using the jsonschema
package, this schema could be created using structs as such:
FunctionDefinition{
Name: "get_current_weather",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"location": {
Type: jsonschema.String,
Description: "The city and state, e.g. San Francisco, CA",
},
"unit": {
Type: jsonschema.String,
Enum: []string{"celsius", "fahrenheit"},
},
},
Required: []string{"location"},
},
}
The Parameters
field of a FunctionDefinition
can accept either of the above styles, or even a nested struct from another library (as long as it can be marshalled into JSON).
Structured Outputs
func main() {
ctx := context.Background()
client := openrouter.NewClient(os.Getenv("OPENROUTER_API_KEY"))
type Result struct {
Location string `json:"location"`
Temperature float64 `json:"temperature"`
Condition string `json:"condition"`
}
var result Result
schema, err := jsonschema.GenerateSchemaForType(result)
if err != nil {
log.Fatalf("GenerateSchemaForType error: %v", err)
}
request := openrouter.ChatCompletionRequest{
Model: openrouter.DeepseekV3,
Messages: []openrouter.ChatCompletionMessage{
{
Role: openrouter.ChatMessageRoleUser,
Content: openrouter.Content{Text: "What's the weather like in London?"},
},
},
ResponseFormat: &openrouter.ChatCompletionResponseFormat{
Type: openrouter.ChatCompletionResponseFormatTypeJSONSchema,
JSONSchema: &openrouter.ChatCompletionResponseFormatJSONSchema{
Name: "weather",
Schema: schema,
Strict: true,
},
},
}
pj, _ := json.MarshalIndent(request, "", "\t")
fmt.Printf("request :\n %s\n", string(pj))
res, err := client.CreateChatCompletion(ctx, request)
if err != nil {
fmt.Println("error", err)
} else {
b, _ := json.MarshalIndent(res, "", "\t")
fmt.Printf("response :\n %s", string(b))
}
}
Contributing Guidelines, we hope to see your contributions!