Claude API Go SDK Example: A Working Golang Guide
Is there an official Claude API Go SDK?
No. Anthropic ships official SDKs for Python and TypeScript, but there is no first-party Go client. If you're searching for a "Claude API Go lang SDK example," the honest answer is that you'll either write a thin HTTP wrapper yourself (which takes about 40 lines) or use a community-maintained package that itself is just a wrapper around net/http and encoding/json.
The good news: Claude's API is plain JSON over HTTPS, so Go — with its excellent standard library for HTTP and JSON — doesn't actually need a heavy SDK. Below is a complete, working example you can copy into a real project, followed by streaming and tool-use variants.
A minimal Go client for the Messages API
Here's the full request/response cycle with no external dependencies beyond the standard library.
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 {
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
}
func main() {
reqBody := MessagesRequest{
Model: "claude-3-5-sonnet-20241022",
MaxTokens: 1024,
Messages: []Message{
{Role: "user", Content: "Summarize what Go channels are in one sentence."},
},
}
payload, _ := json.Marshal(reqBody)
req, err := http.NewRequest("POST", "https://api.anthropic.com/v1/messages", bytes.NewBuffer(payload))
if err != nil {
panic(err)
}
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 _, block := range result.Content {
fmt.Println(block.Text)
}
}
That's a fully functional Claude API call in idiomatic Go — no SDK required. The pattern is always: marshal a struct to JSON, set headers, send the POST, unmarshal the response into matching structs.
Handling streaming responses in Go
Streaming is where a raw HTTP approach gets more verbose, because you need to read Server-Sent Events line by line instead of unmarshaling a single JSON blob. Use bufio.Scanner over the response body:
req.Header.Set("Accept", "text/event-stream")
// add "stream": true to MessagesRequest
resp, _ := client.Do(req)
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "data: ") {
data := strings.TrimPrefix(line, "data: ")
// parse the event JSON and extract delta.text
fmt.Println(data)
}
}
You'll need to handle event: content_block_delta, event: message_stop, and ping events separately — the SSE format includes several event types you'll want to switch on rather than assume every line is content.
Adding tool use
Tool definitions are just another field on the request struct:
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]interface{} `json:"input_schema"`
}
type MessagesRequestWithTools struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
Messages []Message `json:"messages"`
Tools []Tool `json:"tools"`
}
Populate InputSchema as a nested map[string]interface{} matching JSON Schema, marshal it the same way, and check result.Content for blocks with "type": "tool_use" instead of "text". The parsing logic is the same pattern — define a struct, unmarshal, switch on the type field.
Why writing your own client is usually fine
Because Go's JSON handling is this clean, most teams don't actually need a third-party SDK for Claude. The parts that add real complexity are usually not the HTTP call itself but everything around it: retry logic on 429s, key rotation, per-user usage tracking, exposing a safe subset of the API to a frontend team, and giving non-engineers visibility into spend.
If that's the problem you're solving — not "how do I call Claude from Go" but "how do I turn Claude access into something my team and customers can safely consume" — a wrapper like SubToAPI handles that layer for you. You get application-scoped keys (sub_live_...), usage metadata per key, and team seats, while your Go code barely changes:
req, err := http.NewRequest("POST", "https://api.subtoapi.app/v1/messages", bytes.NewBuffer(payload))
req.Header.Set("Authorization", "Bearer "+os.Getenv("SUBTOAPI_KEY"))
req.Header.Set("Content-Type", "application/json")
Same request struct, same response shape, same streaming pattern — just a different base URL and auth header. That means the Go code above works against SubToAPI with a two-line change, which is useful if you want per-application keys instead of sharing one Anthropic key across every service you build. Full request/response shapes are in the docs and quickstart, with dedicated pages for messages, streaming, and tool use.
Should you use a community Go SDK instead?
A few unofficial Go packages exist on GitHub that wrap the Messages API. They can save you boilerplate, but check three things before adopting one: whether it's updated for the current API version header, whether it supports streaming and tool use (many only cover basic completions), and how actively it's maintained. Given how small the official client code is — the example above is the whole thing — many Go teams find it simpler to own a 60-line internal package than depend on an unmaintained wrapper.
questions
Does Anthropic publish an official Go SDK for the Claude API? No. Official SDKs exist for Python and TypeScript. For Go, you call the HTTPS JSON API directly using net/http and encoding/json, or use a third-party package.
What's the minimum code needed to call Claude from Go? Define request/response structs matching the Messages API shape, marshal your request with json.Marshal, POST it with http.NewRequest, set the x-api-key and anthropic-version headers, and unmarshal the response body.
Can I use the same Go code against a hosted API wrapper instead of Anthropic directly? Yes. Services like SubToAPI expose the same request/response format over HTTPS with a Bearer token instead of x-api-key, so your existing Go structs and parsing logic work unchanged — you just change the base URL and auth header.