🐹
Languages

Use Hanzo AI with Go

Use Hanzo AI in Go using the official OpenAI Go client or any OpenAI-compatible Go library. Hanzo's standard REST API works with all HTTP clients.

Base URL: https://api.hanzo.ai/v1

API Key: Get yours at hanzo.ai/signup · Fully OpenAI-compatible · 390+ models available

🐹

Created by Go Authors / Google

License: BSD-3-Clause · View source on GitHub →

Hanzo AI is OpenAI-compatible, so existing Go code works with zero refactoring. We deeply appreciate the Go Authors / Google team for building and maintaining this open-source project.

OpenAI Go client

go
go get github.com/openai/openai-go

package main

import (
	"context"
	"fmt"
	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {
	client := openai.NewClient(
		option.WithBaseURL("https://api.hanzo.ai/v1"),
		option.WithAPIKey("your-hanzo-api-key"),
	)

	resp, _ := client.Chat.Completions.New(context.Background(),
		openai.ChatCompletionNewParams{
			Model: openai.F("zen4-pro"),
			Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
				openai.UserMessage("Hello from Go!"),
			}),
		},
	)
	fmt.Println(resp.Choices[0].Message.Content)
}

Raw HTTP client

go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
)

func main() {
	body, _ := json.Marshal(map[string]any{
		"model":    "zen4-pro",
		"messages": []map[string]string{{"role": "user", "content": "Hello!"}},
	})

	req, _ := http.NewRequest("POST", "https://api.hanzo.ai/v1/chat/completions", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("HANZO_API_KEY"))
	req.Header.Set("Content-Type", "application/json")

	resp, _ := http.DefaultClient.Do(req)
	defer resp.Body.Close()
	io.Copy(os.Stdout, resp.Body)
	fmt.Println()
}

Streaming

go
stream, _ := client.Chat.Completions.NewStreaming(context.Background(),
	openai.ChatCompletionNewParams{
		Model: openai.F("zen4-pro"),
		Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
			openai.UserMessage("Write a poem"),
		}),
	},
)
defer stream.Close()

for stream.Next() {
	chunk := stream.Current()
	if len(chunk.Choices) > 0 {
		fmt.Print(chunk.Choices[0].Delta.Content)
	}
}

Environment

bash
export HANZO_API_KEY="your-hanzo-api-key"
go run main.go

Ready to get started?

Create a free account and get your API key. 100K API calls/month free forever.