# Go (/docs/sdk/golang)

The official [OpenAI Go SDK](https://github.com/openai/openai-go) 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 [#install]

```bash
go get github.com/openai/openai-go/v3
```

## Create a chat completion [#create-a-chat-completion]

```go title="main.go"
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)
}
```

```text
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]

```go
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 [#choose-providers]

The SDK has no field for OhMyGPT's `provider` routing object. Add it to the request body with `option.WithJSONSet`:

```go
completion, err := client.Chat.Completions.New(ctx, params,
	option.WithJSONSet("provider", map[string]any{"sort": "price"}),
)
```

[Provider Routing](/docs/provider-routing) lists the fields.

## Read the cost [#read-the-cost]

Non-streaming responses carry the charge in the `X-Cost-Credits` header. Capture the raw response with `option.WithResponseInto`:

```go
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"))
```

```text
01a19b5d-e3c3-71ce-9ef9-68ee39f92a91 5
```

See [Costs](/docs/costs) for how Credits convert to USD.
