Other Official SDKs Basics
8 examples to get you started with the other official Claude SDKs - 5 basic and 3 intermediate.
Prerequisites
- An Anthropic API key, set as the
ANTHROPIC_API_KEYenvironment variable. - The SDK package for whichever language you're using, installed via that language's package manager (
go get, Maven/Gradle, NuGet, Composer, or Bundler). - Nothing else - each example below is a complete, standalone program.
Basic Examples
1. First Call in Go
The simplest possible Messages API call using anthropic-sdk-go.
package main
import (
"context"
"fmt"
"os"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
)
func main() {
client := anthropic.NewClient(
option.WithAPIKey(os.Getenv("ANTHROPIC_API_KEY")),
)
message, err := client.Messages.New(context.Background(), anthropic.MessageNewParams{
Model: anthropic.ModelClaudeSonnet5,
MaxTokens: 1024,
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Say hello in one sentence.")),
},
})
if err != nil {
panic(err)
}
fmt.Println(message.Content[0].Text)
}anthropic.NewClientreads no config beyond the API key option; there's no separate "connect" step.- Errors come back as a normal Go
errorvalue, not an exception, so checkerrbefore touchingmessage. message.Contentis a slice of content blocks; a plain text reply isContent[0].Text.
Related: Calling Claude from Go with the Official SDK - streaming and tool_use in Go
2. First Call in Java
The same call using the Java SDK's builder-style client.
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
public class FirstCall {
public static void main(String[] args) {
AnthropicClient client = AnthropicOkHttpClient.fromEnv();
MessageCreateParams params = MessageCreateParams.builder()
.model(Model.CLAUDE_SONNET_5)
.maxTokens(1024)
.addUserMessage("Say hello in one sentence.")
.build();
Message message = client.messages().create(params);
System.out.println(message.content().get(0).text().get());
}
}AnthropicOkHttpClient.fromEnv()readsANTHROPIC_API_KEYfrom the environment automatically.MessageCreateParams.builder()is Java's idiomatic way to assemble a request with optional fields.- A failed call throws a typed exception rather than returning an error value, so wrap this in a try/catch in real code.
3. First Call in C#
The same call using the C# SDK's typed request object and async/await.
using Anthropic.SDK;
using Anthropic.SDK.Messaging;
var client = new AnthropicClient(Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY"));
var response = await client.Messages.GetClaudeMessageAsync(new MessageParameters
{
Model = AnthropicModels.ClaudeSonnet5,
MaxTokens = 1024,
Messages = new List<Message>
{
new Message(RoleType.User, "Say hello in one sentence.")
}
});
Console.WriteLine(response.Message);- The client is constructed directly with the API key rather than an options object.
GetClaudeMessageAsyncfollows .NET's async convention, so it must be awaited inside anasynccontext.MessageParametersmirrors the Messages API request body field-for-field, just as a typed C# object.
4. First Call in PHP
The same call using PHP's associative-array request style.
<?php
require 'vendor/autoload.php';
use Anthropic\Anthropic;
$client = Anthropic::factory()
->withApiKey(getenv('ANTHROPIC_API_KEY'))
->make();
$response = $client->messages()->create([
'model' => 'claude-sonnet-5',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'Say hello in one sentence.'],
],
]);
echo $response->content[0]->text;Anthropic::factory()is a fluent builder;withApiKey()andmake()produce the configured client.- Request parameters are a plain associative array, matching the Messages API's JSON shape directly.
- PHP has no compile-time type checking here, so a typo in a key name (like
max_token) fails at request time, not before.
5. First Call in Ruby
The same call using Ruby's hash-based request style.
require "anthropic"
client = Anthropic::Client.new(api_key: ENV["ANTHROPIC_API_KEY"])
response = client.messages.create(
model: "claude-sonnet-5",
max_tokens: 1024,
messages: [
{ role: "user", content: "Say hello in one sentence." }
]
)
puts response.content[0].textAnthropic::Client.newtakes the API key directly as a keyword argument.- Ruby hashes with symbol keys map cleanly onto the Messages API's JSON body, the same role as PHP's associative array.
- Like PHP, there's no compile-time check on the request shape, so validate keys carefully in larger codebases.
Intermediate Examples
6. Reading the Response Shape in Go
Every SDK returns the same underlying fields; this example reads more of them.
message, err := client.Messages.New(context.Background(), anthropic.MessageNewParams{
Model: anthropic.ModelClaudeSonnet5,
MaxTokens: 256,
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Name three Go standard library packages.")),
},
})
if err != nil {
panic(err)
}
fmt.Println("Stop reason:", message.StopReason)
fmt.Println("Input tokens:", message.Usage.InputTokens)
fmt.Println("Output tokens:", message.Usage.OutputTokens)
for _, block := range message.Content {
if block.Type == "text" {
fmt.Println(block.Text)
}
}StopReasontells you why generation stopped:end_turn,max_tokens,tool_use, and so on - check it before assuming the reply is complete.Usagecarries token counts for both sides of the exchange, which every SDK exposes in some typed or dictionary form.Contentis always a list of blocks even for a single text reply, because the same field also carriestool_useblocks.
Related: Messages API Basics - the request/response shape every SDK wraps
7. Setting a System Prompt Across Languages
The system parameter behaves identically everywhere; only the syntax to set it changes.
MessageCreateParams params = MessageCreateParams.builder()
.model(Model.CLAUDE_SONNET_5)
.maxTokens(512)
.system("You are a terse, technical code reviewer. Answer in bullet points only.")
.addUserMessage("Review this function signature: func Add(a, b int) int")
.build();
Message message = client.messages().create(params);
System.out.println(message.content().get(0).text().get());response = client.messages.create(
model: "claude-sonnet-5",
max_tokens: 512,
system: "You are a terse, technical code reviewer. Answer in bullet points only.",
messages: [
{ role: "user", content: "Review this function signature: func Add(a, b int) int" }
]
)- The
systemfield is a top-level request parameter in every SDK, never a message in themessagesarray. - It steers tone and constraints for the whole conversation, not just the next reply.
- Keep it stable across turns in a multi-turn conversation; changing it mid-conversation can produce inconsistent behavior.
8. Handling Errors Idiomatically Per Language
The same rate-limit failure, handled the way each language expects.
message, err := client.Messages.New(ctx, params)
if err != nil {
var apiErr *anthropic.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == 429 {
fmt.Println("Rate limited, back off and retry.")
return
}
panic(err)
}try
{
var response = await client.Messages.GetClaudeMessageAsync(parameters);
}
catch (RateLimitException)
{
Console.WriteLine("Rate limited, back off and retry.");
}- Go surfaces errors as values you inspect with
errors.As, matching Go's no-exceptions convention. - C# and Java throw typed exceptions you catch by class, matching their platforms' conventions.
- Whichever language you're in, the underlying condition (an HTTP 429 from the Messages API) is the same - only the catching mechanism differs.
Related: Idiomatic Tool Use Across Go, Java, C#, PHP, and Ruby - the same cross-language comparison, applied to tool_use
Stack versions: Written against the Claude model lineup current as of ~June 2026 - Claude Fable 5, Claude Opus 4.8, Claude Sonnet 5 (the default), and Claude Haiku 4.5 - and the current official SDKs for Go, Java, C#, PHP, and Ruby. Model names, SDK versions, and pricing move quickly - verify current specifics at platform.claude.com/docs before relying on them.