/openroutergo

Easy to use OpenRouter Golang SDK

Primary LanguageGoMIT LicenseMIT

OpenRouterGo

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.

Go Reference Go Report Card Release Version License

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.

Features

  • 🚀 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

Installation

Go version 1.22 or higher is required.

go get github.com/eduardolat/openroutergo

Quick Start Example

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)
}

More Examples

We've included several examples to help you get started quickly:

Get Started

  1. Get your API key from OpenRouter.ai
  2. Install the package: go get github.com/eduardolat/openroutergo
  3. Start building with the examples above

About me

I'm Eduardo, if you like my work please ⭐ star the repo and find me on the following platforms:

License

This project is licensed under the MIT License - see the LICENSE file for details.