More SDKs and Code Examples
Besides Python, C# and TypeScript, there are official SDKs for Java, Go, Ruby and PHP. They all have the same shape: create a client (ANTHROPIC_API_KEY from the environment), call messages.create, then read the content blocks after checking their type.
12.1 Install commands
| Language | Install | Client |
|---|---|---|
| Java | Maven/Gradle: com.anthropic:anthropic-java | AnthropicOkHttpClient.fromEnv() |
| Go | go get github.com/anthropics/anthropic-sdk-go/v2 | anthropic.NewClient() |
| Ruby | gem install anthropic | Anthropic::Client.new |
| PHP | composer require "anthropic-ai/sdk" | new Anthropic\Client() |
| C# | dotnet add package Anthropic | new AnthropicClient() |
| TypeScript | npm install @anthropic-ai/sdk | new Anthropic() |
12.2 Java
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Message;
AnthropicClient client = AnthropicOkHttpClient.fromEnv(); // ANTHROPIC_API_KEY
MessageCreateParams params = MessageCreateParams.builder()
.model("claude-opus-5-5")
.maxTokens(16000L)
.addUserMessage("How does @Transactional work in Spring Boot?")
.build();
Message response = client.messages().create(params);
response.content().stream()
.flatMap(block -> block.text().stream())
.forEach(t -> System.out.println(t.text()));12.3 Go
import (
"context"
"fmt"
"github.com/anthropics/anthropic-sdk-go/v2"
)
client := anthropic.NewClient() // reads ANTHROPIC_API_KEY from the environment
resp, err := client.Messages.New(context.Background(), anthropic.MessageNewParams{
Model: "claude-opus-5-5",
MaxTokens: 16000,
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Goroutine vs thread?")),
},
})
if err != nil { log.Fatal(err) }
for _, block := range resp.Content {
switch v := block.AsAny().(type) {
case anthropic.TextBlock:
fmt.Println(v.Text)
}
}12.4 Ruby and PHP
require "anthropic"
client = Anthropic::Client.new
message = client.messages.create(
model: :"claude-opus-5-5", max_tokens: 16000,
messages: [{ role: "user", content: "What is an N+1 query in Rails?" }]
)
message.content.each { |b| puts b.text if b.type == :text } # type is a Symboluse Anthropic\Client;
$client = new Client();
$message = $client->messages->create(
model: 'claude-opus-5-5',
maxTokens: 16000, // named args are camelCase in PHP
messages: [['role' => 'user', 'content' => 'Explain Laravel queues']],
);
foreach ($message->content as $block) {
if ($block->type === 'text') echo $block->text;
}12.5 C# — Vision, Files API and Batches
using Anthropic;
using Anthropic.Core;
using Anthropic.Models.Files;
using Anthropic.Models.Messages;
AnthropicClient client = new();
// 1. Upload the file once
var upload = await client.Files.Upload(new FileUploadParams
{
File = new BinaryContent
{
Stream = File.OpenRead("invoice.jpg"),
FileName = "invoice.jpg",
ContentType = new("image/jpeg"),
},
});
// 2. Send the image by file_id
var response = await client.Messages.Create(new MessageCreateParams
{
Model = Model.ClaudeOpus5_5,
MaxTokens = 1024,
Messages = [ new UserMessageParam {
Content = new List<ContentBlockParam> {
new ContentBlockParam(new ImageBlockParam(
new ImageBlockParamSource(new FileImageSource(upload.ID)))),
new ContentBlockParam(new TextBlockParam("What is the invoice total?")),
},
} ],
});// Batch: 50% cheaper, async
var batch = await client.Messages.Batches.Create(new() {
Requests = [
new() { CustomID = "review-1",
Params = new() { Model = "claude-haiku-5-5", MaxTokens = 200,
Messages = [ new UserMessageParam { Content = "Sentiment: Great product!" } ] } },
],
});
// Poll client.Messages.Batches.Retrieve(batch.ID) until ProcessingStatus == "ended",
// then iterate client.Messages.Batches.Results(batch.ID) (match by CustomID).12.6 Citations code (Python)
r = client.messages.create(
model="claude-opus-5-5", max_tokens=2000,
messages=[{"role": "user", "content": [
{"type": "document",
"source": {"type": "text", "media_type": "text/plain", "data": POLICY_TEXT},
"title": "HR Leave Policy",
"citations": {"enabled": True}},
{"type": "text", "text": "How many days is maternity leave?"},
]}],
)
for b in r.content:
if b.type == "text":
print(b.text)
for c in (b.citations or []):
print(" ↳", c.cited_text) # the exact quote from the document