Skip to content

Go SDK

SDKMAX is fully OpenAI-compatible. Go projects can use the official OpenAI Go SDK (github.com/openai/openai-go) directly, pointing it at SDKMAX via a custom BaseURL.

Install

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

Initialize the client

go
package main

import (
    "context"
    "fmt"

    "github.com/openai/openai-go/v2"
    "github.com/openai/openai-go/v2/option"
)

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

    resp, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
        Model: "gpt-4o",
        Messages: []openai.ChatCompletionMessageParamUnion{
            openai.UserMessage("Explain SDKMAX's core capabilities."),
        },
    })
    if err != nil {
        panic(err)
    }
    fmt.Println(resp.Choices[0].Message.Content)
}

Or configure via environment variables:

bash
export OPENAI_API_KEY="sk-your-key"
export OPENAI_BASE_URL="https://api.sdkmax.com/v1"
go
client := openai.NewClient() // reads the two env vars above automatically

Streaming

go
stream := client.Chat.Completions.NewStreaming(context.Background(), openai.ChatCompletionNewParams{
    Model: "gpt-4o",
    Messages: []openai.ChatCompletionMessageParamUnion{
        openai.UserMessage("Write a short poem about an AI gateway."),
    },
})

for stream.Next() {
    chunk := stream.Current()
    if len(chunk.Choices) > 0 {
        fmt.Print(chunk.Choices[0].Delta.Content)
    }
}
if err := stream.Err(); err != nil {
    panic(err)
}

Switching models

go
models := []string{"gpt-4o", "claude-opus-4-8", "deepseek-chat"}
for _, model := range models {
    resp, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
        Model:    model,
        Messages: []openai.ChatCompletionMessageParamUnion{openai.UserMessage("What's 1+1?")},
    })
    if err != nil {
        fmt.Println(model, "error:", err)
        continue
    }
    fmt.Println(model, "->", resp.Choices[0].Message.Content)
}

Error handling

go
resp, err := client.Chat.Completions.New(context.Background(), params)
if err != nil {
    var apiErr *openai.Error
    if errors.As(err, &apiErr) {
        // apiErr.StatusCode == 429 means quota exhausted or rate-limited — back off and retry
        fmt.Println(apiErr.StatusCode, apiErr.Message)
    }
    return
}

Full error code reference: Error Codes.

Not using the official SDK?

The standard library's net/http works fine too — set the Authorization: Bearer sk-xxx header and build the JSON body per Making Requests.

SDKMAX — Enterprise AI Gateway, Aggregating Global AI Resources