A powerful, developer-friendly Go SDK for OpenRouter.ai - the platform that gives you unified access to 100+ AI models from OpenAI, Anthropic, Google, and more through a single consistent API.
Warning
This client is not yet stable and the API signature may change in the future until it reaches version 1.0.0, so be careful when upgrading. However, the API signature should not change too much.
- 🚀 Simple & Intuitive API - Fluent builder pattern with method chaining for clean, readable code
- 🔄 Smart Fallbacks - Automatically retry with alternative models if your first choice fails or is rate-limited
- 🛠️ Function Calling - Let AI models access your tools and functions when needed
- 📊 Structured Outputs - Force responses in valid JSON format with schema validation
- 🧠 Complete Control - Fine-tune model behavior with temperature, top-p, frequency penalty and more
- 🔍 Debug Mode - Instantly see the exact requests and responses for easier development
Go version 1.22 or higher is required.
go get github.com/eduardolat/openroutergo
package main
import (
"fmt"
"log"
"github.com/eduardolat/openroutergo"
)
// Replace with your own API key and the model you want to use
const apiKey = "sk......."
const model = "deepseek/deepseek-r1:free"
func main() {
// Create a client with your API key
client, err := openroutergo.
NewClient().
WithAPIKey(apiKey).
Create()
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Build and execute your request with a fluent API
completion, resp, err := client.
NewChatCompletion().
WithModel(model).
WithSystemMessage("You are a helpful assistant expert in geography.").
WithUserMessage("What is the capital of France?").
Execute()
if err != nil {
log.Fatalf("Failed to execute completion: %v", err)
}
// Print the model's first response
fmt.Println("Response:", resp.Choices[0].Message.Content)
// Continue the conversation seamlessly, the last response is
// automatically added to the conversation history
_, resp, err = completion.
WithUserMessage("That's great! What about Japan?").
Execute()
if err != nil {
log.Fatalf("Failed to execute completion: %v", err)
}
// Print the model's second response
fmt.Println("Response:", resp.Choices[0].Message.Content)
}
We've included several examples to help you get started quickly:
- Basic Usage - Simple chat completion with any model
- Clone Completion - Reuse configurations for multiple requests
- Conversation - Build multi-turn conversations with context
- Model Fallbacks - Gracefully handle rate limits and save money with alternative models
- Function Calling - Allow AI to call your application functions
- Advanced Options - Explore additional parameters for fine-tuning
- JSON Responses - Get structured, validated outputs
- Get your API key from OpenRouter.ai
- Install the package:
go get github.com/eduardolat/openroutergo
- Start building with the examples above
I'm Eduardo, if you like my work please ⭐ star the repo and find me on the following platforms:
This project is licensed under the MIT License - see the LICENSE file for details.