Anthropic SDK Usage Guide
Overview
Set up your development environment to call the Anthropic API using the SDK in your preferred language. This page covers how to set up your local development environment to use the Anthropic API. You can use officially supported SDKs or your favorite HTTP client.
Create and Export API Key
Before you begin, create an API key through the token instructions for secure API access. Store the key in a secure location, such as your .zshrc file or another text file on your computer. After generating the API key, export it as an environment variable in your terminal.
export ANTHROPIC_API_KEY="your_api_key_here"setx ANTHROPIC_API_KEY "your_api_key_here"$env:ANTHROPIC_API_KEY="your_api_key_here"Note: The Anthropic SDK automatically reads your API key from the system environment. You can also set the API key directly in your code, but this is not secure, especially when sharing code. It's recommended to use environment variables to protect your key.
Install Official SDK
JavaScript
To use the Anthropic API in server-side JavaScript environments (such as Node.js, Deno, or Bun), you can use the official TypeScript and JavaScript Anthropic SDK.
Install SDK
Install the Anthropic SDK using your preferred package manager. Here's an example command using npm:
npm install @anthropic-ai/sdkAfter installing the Anthropic SDK, create a file named anthropic-example.mjs and copy the example code into it:
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
baseURL: "https://api.agentsflare.com/anthropic/v1",
});
const response = await client.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
messages: [
{
role: 'user',
content: 'Write a one-sentence bedtime story about a unicorn.'
}
]
});
console.log(response.content[0].text);Execute with node anthropic-example.mjs (or equivalent command for Deno or Bun), and you'll see the API request output in a few seconds.
Python
To use the Anthropic API in Python, use the official Anthropic Python SDK.
Install SDK
Install the Anthropic SDK using pip:
pip install anthropicAfter installing the Anthropic SDK, create a file named anthropic-example.py and copy the example code into it:
import anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("ANTHROPIC_API_KEY"),
base_url="https://api.agentsflare.com/anthropic/v1"
)
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
)
print(response.content[0].text)Execute python anthropic-example.py, and you'll see the API request output in a few seconds.
.NET
To use the Anthropic API in .NET, use the official Anthropic .NET SDK.
Install SDK
Install the Anthropic SDK using NuGet:
dotnet add package AnthropicAfter installing the Anthropic SDK, create a file named anthropic-example.cs and copy the example code into it:
using Anthropic;
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
var client = new AnthropicClient(
Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY"),
new Uri("https://api.agentsflare.com/anthropic/v1")
);
var response = await client.Messages.CreateAsync(new MessageRequest
{
Model = "claude-3-5-sonnet-20241022",
MaxTokens = 1024,
Messages = new[]
{
new Message
{
Role = Role.User,
Content = "Write a one-sentence bedtime story about a unicorn."
}
}
});
Console.WriteLine(response.Content[0].Text);
}
}Execute dotnet run anthropic-example.cs, and you'll see the API request output in a few seconds.
Java
To use the Anthropic API in Java, use the official Anthropic Java SDK.
Install SDK
Install the Anthropic SDK using Maven:
<dependency>
<groupId>com.anthropic</groupId>
<artifactId>anthropic-java</artifactId>
<version>1.0.0</version>
</dependency>After installing the Anthropic SDK, create a file named anthropic-example.java and copy the example code into it:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.models.Message;
import com.anthropic.models.MessageCreateParams;
import com.anthropic.models.Model;
AnthropicClient client = AnthropicOkHttpClient.fromEnv();
MessageCreateParams params = MessageCreateParams.builder()
.addUserMessage("Write a one-sentence bedtime story about a unicorn.")
.model(Model.CLAUDE_3_5_SONNET)
.maxTokens(1024)
.build();
Message message = client.messages().create(params);
System.out.println(message.getContent().get(0).getText());Execute java anthropic-example.java, and you'll see the API request output in a few seconds.
Go
To use the Anthropic API in Go, use the official Anthropic Go SDK.
Install SDK
Install the Anthropic SDK using Go Modules:
go get github.com/anthropics/anthropic-sdk-goAfter installing the Anthropic SDK, create a file named anthropic-example.go and copy the example code into it:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/anthropics/anthropic-sdk-go"
)
func main() {
client := anthropic.NewClient(
anthropic.WithAPIKey(os.Getenv("ANTHROPIC_API_KEY")),
anthropic.WithBaseURL("https://api.agentsflare.com/anthropic/v1"),
)
response, err := client.Messages.New(context.Background(), anthropic.MessageNewParams{
Model: anthropic.F(anthropic.ModelClaude3_5Sonnet20241022),
MaxTokens: anthropic.F(int64(1024)),
Messages: anthropic.F([]anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Write a one-sentence bedtime story about a unicorn.")),
}),
})
if err != nil {
log.Fatalf("Messages.New error: %v", err)
}
fmt.Println(response.Content[0].Text)
}Execute go run anthropic-example.go, and you'll see the API request output in a few seconds.