Installation & Quickstart
Relevant source files
The following files were used as context for generating this wiki page:
The Anthropic SDK for Go enables developers to integrate Claude's generative AI capabilities into Go applications. It provides a structured interface for interacting with the Anthropic API, as well as support for cloud-provider specific implementations like AWS Bedrock and Google Vertex AI.
This guide covers the necessary steps to install the library, configure the client, and execute a basic message request.
Sources: README.md:1-12, examples/message/main.go:1-25
Installation
Requirements
The SDK requires Go version 1.22 or higher.
Sources: README.md:46
Adding the Dependency
You can add the SDK to your project using the standard go get command. It is recommended to pin to a specific version for stability.
go get -u 'github.com/anthropics/anthropic-sdk-go@v1.30.0'
Alternatively, you can include the import path in your source code, and Go will manage the dependency during the next build or tidy operation.
import (
"github.com/anthropics/anthropic-sdk-go" // imported as anthropic
)
Sources: README.md:16-30
Local Development from Source
To use a local version of the library (e.g., for debugging or contributing), use a replace directive in your go.mod file:
go mod edit -replace github.com/anthropics/anthropic-sdk-go=/path/to/anthropic-sdk-go
Sources: CONTRIBUTING.md:28-32
Client Configuration
The SDK uses a client-based architecture. You initialize a client to manage connections, authentication, and request parameters.
Authentication Methods
The SDK supports multiple authentication strategies depending on the target environment:
| Method | Environment Variable | Option / Helper |
|---|---|---|
| Anthropic API Key | ANTHROPIC_API_KEY | option.WithAPIKey("key") |
| AWS Bedrock | AWS_REGION, AWS_BEARER_TOKEN_BEDROCK | bedrock.WithLoadDefaultConfig(ctx) |
| Google Vertex AI | N/A | vertex.WithGoogleAuth(ctx, region, projectID) |
Sources: README.md:34-40, examples/bedrock/main.go:13-15, examples/vertex/main.go:13-15, usage_test.go:27-30
Basic Client Initialization
The following diagram illustrates the flow of initializing a client and preparing it for a request.
This diagram shows the decision paths for configuring the SDK client based on the chosen provider.
Sources: README.md:34-40, examples/bedrock/main.go:13-15, examples/vertex/main.go:13-15
Quickstart Example
A basic interaction involves creating a client and sending a message to the Messages service.
package main
import (
"context"
"fmt"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
)
func main() {
// Initialize client
client := anthropic.NewClient(
option.WithAPIKey("my-anthropic-api-key"),
)
// Create a message
message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
MaxTokens: 1024,
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("What is a quaternion?")),
},
Model: anthropic.ModelClaudeOpus4_6,
})
if err != nil {
panic(err.Error())
}
// Output result
fmt.Printf("%+v\n", message.Content)
}
Key Components in Quickstart
anthropic.NewClient: Constructs the central interface for all API calls.anthropic.MessageNewParams: A struct used to define the request, includingModel,MaxTokens, and theMessageslist.anthropic.NewUserMessage: A helper function to create a message with the "user" role.anthropic.NewTextBlock: Encapsulates text content within a message block.
Sources: README.md:34-60, examples/message/main.go:10-25
Development Workflow
For developers contributing to or testing the SDK, the repository includes utility scripts.
Environment Setup
Run the bootstrap script to install dependencies:
./scripts/bootstrap
./scripts/lint
Running Examples
The SDK includes an examples/ directory with various use cases. You can run them directly:
go run ./examples/message
Sources: CONTRIBUTING.md:7-12, CONTRIBUTING.md:21-25
Testing
Most tests require a mock server. The project recommends using a server based on the OpenAPI spec.
./scripts/mock
./scripts/test
Sources: CONTRIBUTING.md:34-40
Summary
To get started with the Anthropic SDK for Go, ensure you have Go 1.22+ installed, add the dependency via go get, and initialize an anthropic.Client with your API key. The SDK provides a type-safe way to interact with Claude models, supporting both direct Anthropic API access and cloud-provider integrations like AWS Bedrock and Vertex AI.