SDK Integrations
Go
Call OhMyGPT from Go with the official OpenAI Go SDK.
The official OpenAI Go SDK works with OhMyGPT as is: set the base URL and key, and the same client calls every chat model in the catalog. The examples use openai-go v3 and Chat Completions.
Install
go get github.com/openai/openai-go/v3Create a chat completion
package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
)
func main() {
client := openai.NewClient(
option.WithBaseURL("https://api.ohmygpt.com/v1"),
option.WithAPIKey(os.Getenv("OHMYGPT_API_KEY")),
)
completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "zhipu/glm-5.3-flash",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("Write a haiku about the sea."),
},
})
if err != nil {
panic(err)
}
fmt.Println(completion.Choices[0].Message.Content)
}Waves kiss the shoreline,
salt air carries old stories—
the tide pulls me home.Set Model to "ohmygpt/mock" for a free test run; it returns a fixed reply. Any model ID from GET /v1/models works here.
Stream
stream := client.Chat.Completions.NewStreaming(context.Background(), openai.ChatCompletionNewParams{
Model: "zhipu/glm-5.3-flash",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("Write a haiku about the sea."),
},
})
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)
}Choose providers
The SDK has no field for OhMyGPT's provider routing object. Add it to the request body with option.WithJSONSet:
completion, err := client.Chat.Completions.New(ctx, params,
option.WithJSONSet("provider", map[string]any{"sort": "price"}),
)Provider Routing lists the fields.
Read the cost
Non-streaming responses carry the charge in the X-Cost-Credits header. Capture the raw response with option.WithResponseInto:
var resp *http.Response // import "net/http"
completion, err := client.Chat.Completions.New(ctx, params, option.WithResponseInto(&resp))
if err != nil {
panic(err)
}
fmt.Println(resp.Header.Get("X-Request-Id"), resp.Header.Get("X-Cost-Credits"))01a19b5d-e3c3-71ce-9ef9-68ee39f92a91 5See Costs for how Credits convert to USD.