Claude API Golang SDK Example Code: A Working Guide
If you searched for a Claude API Golang SDK, here's the short answer: Anthropic does not ship an official Go SDK. There are official SDKs for Python and TypeScript/JavaScript, but Go developers have to either use a community-maintained client or call the HTTPS API directly with net/http. The good news is that Claude's API is a plain JSON-over-HTTPS interface, so writing a working Go client takes about 40 lines of code and gives you full control.
This article walks through exactly that: a minimal Go client for the Messages API, a streaming example, a tool-use example, and a note on community SDKs if you'd rather not maintain your own wrapper.
Why there's no official Go SDK
Anthropic's SDK investment has gone into Python and TypeScript because that's where most LLM application code lives. Go is common for backend services, CLIs, and infra tooling — exactly the kind of code that often needs to call an LLM API without pulling in a heavy client library. For that use case, raw net/http is often the pragmatic choice anyway, since the request/response shape is stable JSON.
Minimal Go client for Claude's Messages API
Here's a working example that sends a single message and parses the response:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type MessagesRequest struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
Messages []Message `json:"messages"`
}
type MessagesResponse struct {
ID string `json:"id"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
}
func main() {
reqBody := MessagesRequest{
Model: "claude-sonnet-4",
MaxTokens: 1024,
Messages: []Message{
{Role: "user", Content: "Explain goroutines in two sentences."},
},
}
payload, _ := json.Marshal(reqBody)
req, _ := http.NewRequest("POST", "https://api.anthropic.com/v1/messages", bytes.NewBuffer(payload))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", os.Getenv("ANTHROPIC_API_KEY"))
req.Header.Set("anthropic-version", "2023-06-01")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var result MessagesResponse
json.Unmarshal(body, &result)
for _, c := range result.Content {
fmt.Println(c.Text)
}
}
This is the whole shape of a Claude API client in Go: build a struct, marshal it, POST it, unmarshal the response. No external dependencies required.
Streaming responses in Go
Claude supports server-sent events for streaming. In Go, you read the response body line by line with bufio.Scanner and parse data: lines:
req.Header.Set("Content-Type", "application/json")
// add "stream": true to the request body
resp, _ := client.Do(req)
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "data: ") {
var event map[string]interface{}
json.Unmarshal([]byte(line[6:]), &event)
if delta, ok := event["delta"].(map[string]interface{}); ok {
if text, ok := delta["text"].(string); ok {
fmt.Print(text)
}
}
}
}
This is roughly the same pattern you'd use for any SSE-based LLM API — the only thing that changes between providers is the JSON shape of the event payloads.
Tool use (function calling) in Go
Tool use just adds a tools field to the request body with a JSON schema. In Go, that means defining a nested struct or using map[string]interface{} if you want flexibility:
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]interface{} `json:"input_schema"`
}
tool := Tool{
Name: "get_weather",
Description: "Get current weather for a city",
InputSchema: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"city": map[string]interface{}{"type": "string"},
},
"required": []string{"city"},
},
}
When Claude decides to call the tool, the response content will include a block with type: "tool_use" containing the tool name and input — you parse that, run your Go function, and send the result back in a follow-up request with role: "user" and a tool_result content block.
Community SDKs worth knowing about
If maintaining your own HTTP wrapper isn't appealing, there are community Go packages (searchable on GitHub as "go-anthropic" or similar) that wrap these same endpoints with typed structs and streaming helpers. They're not official, so check maintenance activity and pin a version before depending on them in production.
A simpler path: a stable REST API instead of raw key management
One recurring pain point with calling Claude directly from backend services — Go included — is key and quota management: rotating keys, tracking usage per team member, and handling retries/streaming consistently across services. SubToAPI turns your existing Claude access into a straightforward HTTPS API with sub_live_... application keys, so your Go service talks to one stable endpoint instead of managing provider-specific auth quirks.
Since it's standard HTTPS, the same Go patterns above work unchanged — just point at SubToAPI's endpoint and swap the header:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"claude-sonnet-4","max_tokens":1024,"messages":[{"role":"user","content":"Hello"}]}'
Usage metadata, streaming, and tool use all work through the same API shape described in the docs, with a quickstart covering the messages, streaming, and tool use endpoints. Plans start at €9/month on the Solo tier, with Team and Scale options for multi-seat setups — see pricing or start a free trial at signup.
questions
Is there an official Claude API SDK for Go? No. Anthropic currently maintains official SDKs for Python and TypeScript only. Go developers use net/http directly or a community-maintained package.
Can I stream Claude responses in Go? Yes. Claude's streaming uses server-sent events over the same HTTPS connection — read the body with bufio.Scanner, parse lines prefixed with data:, and extract the delta.text field from each JSON event.
Do I need a special library for tool use in Go? No. Tool use is just a tools field in the JSON request body with a schema, and a tool_use content block in the response. Standard Go structs or map[string]interface{} handle it without extra dependencies.