anthropics/anthropic-sdk-go

Access to Anthropic's safety-first language model APIs via Go

Claude SDK for Go

What it is – A Go client library that lets Go programs call Anthropic’s Claude large‑language‑model API. It wraps the HTTP endpoints in Go‑idiomatic types and helpers, so developers can send prompts and receive model responses without dealing with raw requests.

Key pieces

  • Client constructionanthropic.NewClient takes options such as option.WithAPIKey. The API key can also be read from the ANTHROPIC_API_KEY environment variable.
  • Message API – The primary entry point is client.Messages.New, which accepts a MessageNewParams struct. You specify the model (e.g., ModelClaudeOpus4_6), max tokens, and an ordered list of message blocks (user, assistant, system, etc.).
  • Message building helpers – Functions like anthropic.NewUserMessage and anthropic.NewTextBlock make it easy to construct the JSON payloads expected by the API.
  • Typed responses – The call returns a Message struct whose Content field contains the model’s reply in a structured form.

How to add it

# Using go modules
go get -u github.com/anthropics/anthropic-sdk-go@v1.68.0

Then import it in code:

import (
    "github.com/anthropics/anthropic-sdk-go" // imported as anthropic
    "github.com/anthropics/anthropic-sdk-go/option"
)

Quick example

client := anthropic.NewClient(option.WithAPIKey("my-anthropic-api-key"))
msg, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
    MaxTokens: 1024,
    Model:     anthropic.ModelClaudeOpus4_6,
    Messages: []anthropic.MessageParam{
        anthropic.NewUserMessage(anthropic.NewTextBlock("What is a quaternion?")),
    },
})
if err != nil { panic(err) }
fmt.Printf("%+v\n", msg.Content)

The snippet sends a user prompt to Claude and prints the model’s answer.

Requirements – Go 1.24 or newer.

Where to learn more – Full API reference at https://platform.claude.com/docs/en/api/sdks/go and the official Claude API docs linked in the README.

License – MIT.

相關

  • 專案
  • 專案
  • 專案
  • 專案
  • 專案