Claude API in Go

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.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

The official Anthropic Go SDK provides full type safety and supports all Claude API features.

Installation

go get github.com/anthropics/anthropic-sdk-go

Basic message

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)
}

With system prompt

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."),
        }),
    },
)

Streaming

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.

Frequently asked questions

Is there an official Go SDK for the Anthropic API?
Yes. The official Go SDK is `github.com/anthropics/anthropic-sdk-go`. It is maintained by Anthropic, ships with full Go type safety, and supports messages, streaming, tool use, and batch API.
How do I pass optional parameters in Go?
The SDK uses the `param` package for optional fields. Pass `anthropic.F(value)` for required fields and `anthropic.F(value)` or `anthropic.Null[T]()` for optional ones. Omit fields using the zero value of `param.Field[T]`.
Does the Go SDK support context cancellation?
Yes. All SDK methods accept a `context.Context` as the first parameter. Pass a context with a deadline or cancellation to handle timeouts: `ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)`.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript