Call the Claude API from Go using the official Anthropic Go SDK. Install the package, create a client, and send your first message with full type safety.
The official Anthropic Go SDK provides full type safety and supports all Claude API features.
go get github.com/anthropics/anthropic-sdk-go
package main
import (
"context"
"fmt"
"os"
anthropic "github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
)
func main() {
client := anthropic.NewClient(
option.WithAPIKey(os.Getenv("ANTHROPIC_API_KEY")),
)
message, err := client.Messages.New(
context.Background(),
anthropic.MessageNewParams{
Model: anthropic.F(anthropic.ModelClaudeSonnet4_6),
MaxTokens: anthropic.F(int64(1024)),
Messages: anthropic.F([]anthropic.MessageParam{
anthropic.UserMessage("What is the capital of France?"),
}),
},
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v
", err)
os.Exit(1)
}
fmt.Println(message.Content[0].(anthropic.TextBlock).Text)
}
message, err := client.Messages.New(
context.Background(),
anthropic.MessageNewParams{
Model: anthropic.F(anthropic.ModelClaudeSonnet4_6),
MaxTokens: anthropic.F(int64(1024)),
System: anthropic.F([]anthropic.TextBlockParam{
{Type: anthropic.F(anthropic.TextBlockParamTypeText),
Text: anthropic.F("You are a concise technical assistant.")},
}),
Messages: anthropic.F([]anthropic.MessageParam{
anthropic.UserMessage("Explain goroutines briefly."),
}),
},
)
stream := client.Messages.NewStreaming(
context.Background(),
anthropic.MessageNewParams{
Model: anthropic.F(anthropic.ModelClaudeSonnet4_6),
MaxTokens: anthropic.F(int64(1024)),
Messages: anthropic.F([]anthropic.MessageParam{
anthropic.UserMessage("Write a Go function that reads a CSV file."),
}),
},
)
for stream.Next() {
event := stream.Current()
if delta, ok := event.Delta.(anthropic.ContentBlockDeltaEventDelta); ok {
if delta.Type == "text_delta" {
fmt.Print(delta.Text)
}
}
}
if err := stream.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Stream error: %v
", err)
}
See the Python quickstart or Node.js quickstart for equivalents in other languages. For cost estimates on Go workloads, use the Claude API Cost Calculator.