csharp
cli-sdks-libraries/sdks/csharp
History
cli-sdks-libraries/sdks/csharp Changed · +1 / -1 lines
} ``` -For authentication options including Workload Identity Federation, see [Authentication](https://platform.claude.com/docs/en/manage-claude/authentication). +For authentication options including Workload Identity Federation, see [Authentication](https://platform.claude.com/docs/en/manage-claude/authentication). If your API key is a [personal or service account key](https://platform.claude.com/docs/en/manage-claude/authentication#key-types) with access to multiple workspaces, set the workspace ID in the `anthropic-workspace-id` request header; [Select a workspace](https://platform.claude.com/docs/en/manage-claude/authentication#select-a-workspace) shows the per-request option for this SDK. ## Client configuration
cli-sdks-libraries/sdks/csharp Changed · +3 / -3 lines
```csharp using System; -using Anthropic.Models.Beta.Files; +using Anthropic.Models.Files; FileDownloadParams parameters = new() { FileID = "file_id" }; -var response = await client.Beta.Files.Download(parameters); +var response = await client.Files.Download(parameters); Console.WriteLine(response); ```
```csharp using System.IO; -using var response = await client.Beta.Files.Download(parameters); +using var response = await client.Files.Download(parameters); using var contentStream = await response.ReadAsStream(); using var fileStream = File.Open(path, FileMode.OpenOrCreate); await contentStream.CopyToAsync(fileStream); // Or any other Stream
cli-sdks-libraries/sdks/csharp First recorded · 467 lines, first recorded
## Installation ## Requirements ## Usage ## Client configuration ### Modifying configuration ## Streaming ## Error handling ## Retries ## Timeouts ## Pagination ### Auto-pagination ### Manual pagination ## Response validation ## IChatClient integration ## Requests and responses ## Advanced usage ### Binary responses ### Raw responses ### Logging ### Undocumented API functionality ## Platform integrations ## Semantic versioning ## Additional resources
The first capture of this source. The page was already there, and this is what it said.
---
title: C# SDK
url: https://platform.claude.com/docs/en/cli-sdks-libraries/sdks/csharp
description: Install and configure the Anthropic C# SDK for .NET applications with IChatClient integration
---
The Anthropic C# SDK provides convenient access to the Claude API from applications written in C#.
<Info>
The C# SDK is currently in beta. APIs may change between versions.
</Info>
<Info>
For API feature documentation with code examples, see the [API reference](https://platform.claude.com/docs/en/api/overview). This page covers C#-specific SDK features and configuration.
</Info>
<Warning>
As of version 10+, the `Anthropic` package is now the official Anthropic SDK for C#. Package versions 3.X and below were previously used for the tryAGI community-built SDK, which has moved to [`tryAGI.Anthropic`](https://www.nuget.org/packages/tryagi.Anthropic/). If you need to continue using the former client in your project, update your package reference to `tryAGI.Anthropic`.
</Warning>
## Installation
Install the package from [NuGet](https://www.nuget.org/packages/Anthropic):
```bash
dotnet add package Anthropic
```
## Requirements
This library requires .NET Standard 2.0 or later.
## Usage
```csharp
using System;
using Anthropic;
using Anthropic.Models.Messages;
AnthropicClient client = new();
MessageCreateParams parameters = new()
{
MaxTokens = 1024,
Messages =
[
new()
{
Role = Role.User,
Content = "Hello, Claude",
},
],
Model = Model.ClaudeOpus5,
};
var message = await client.Messages.Create(parameters);
foreach (var block in message.Content)
{
if (block.TryPickText(out var textBlock))
{
Console.WriteLine(textBlock.Text);
}
}
```
For authentication options including Workload Identity Federation, see [Authentication](https://platform.claude.com/docs/en/manage-claude/authentication).
## Client configuration
Configure the client using environment variables:
```csharp
using Anthropic;
// Configured using the ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL environment variables
AnthropicClient client = new();
```
Or manually:
```csharp
using Anthropic;
AnthropicClient client = new() { ApiKey = "my-anthropic-api-key" };
```
Or using a combination of the two approaches.
See this table for the available options:
| Property | Environment variable | Required | Default value |
| ----------- | ---------------------- | -------- | ----------------------------- |
| `ApiKey` | `ANTHROPIC_API_KEY` | false | - |
| `AuthToken` | `ANTHROPIC_AUTH_TOKEN` | false | - |
| `BaseUrl` | `ANTHROPIC_BASE_URL` | true | `"https://api.anthropic.com"` |
### Modifying configuration
To temporarily use a modified client configuration, while reusing the same connection and thread pools, call `WithOptions` on any client or service:
```csharp
using System;
var message = await client
.WithOptions(options =>
options with
{
BaseUrl = "https://example.com",
Timeout = TimeSpan.FromSeconds(42),
}
)
.Messages.Create(parameters);
Console.WriteLine(message);
```
Using a [`with` expression](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/with-expression) makes it easy to construct the modified options.
The `WithOptions` method does not affect the original client or service.
## Streaming
The SDK defines methods that return response "chunk" streams, where each chunk can be individually processed as soon as it arrives instead of waiting on the full response. Streaming methods generally correspond to [SSE](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) or [JSONL](https://jsonlines.org) responses.
A streaming method always has a `Streaming` suffix in its name, even if it doesn't have a non-streaming variant.
These streaming methods return [`IAsyncEnumerable`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1):
```csharp
using System;
using Anthropic.Models.Messages;
MessageCreateParams parameters = new()
{
MaxTokens = 1024,
Messages =
[
new()
{
Role = Role.User,
Content = "Hello, Claude",
},
],
Model = Model.ClaudeOpus5,
};
await foreach (var message in client.Messages.CreateStreaming(parameters))
{
Console.WriteLine(message);
}
```
## Error handling
The SDK throws custom unchecked exception types:
* `AnthropicApiException`: Base class for API errors. See this table for which exception subclass is thrown for each HTTP status code:
| Status | Exception |
| ------ | ---------------------------------------- |
| 400 | `AnthropicBadRequestException` |
| 401 | `AnthropicUnauthorizedException` |
| 403 | `AnthropicForbiddenException` |
| 404 | `AnthropicNotFoundException` |
| 422 | `AnthropicUnprocessableEntityException` |
| 429 | `AnthropicRateLimitException` |
| 5xx | `Anthropic5xxException` |
| others | `AnthropicUnexpectedStatusCodeException` |
Additionally, all 4xx errors inherit from `Anthropic4xxException`.
* `AnthropicSseException`: thrown for errors encountered during SSE streaming after a successful initial HTTP response.
* `AnthropicIOException`: I/O networking errors.
* `AnthropicInvalidDataException`: Failure to interpret successfully parsed data. For example, when accessing a property that's supposed to be required, but the API unexpectedly omitted it from the response.
* `AnthropicException`: Base class for all exceptions.
## Retries
The SDK automatically retries 2 times by default, with a short exponential backoff between requests.
Only the following error types are retried:
* Connection errors (for example, because of a network connectivity problem)
* 408 Request Timeout
* 409 Conflict
* 429 Rate Limit
* 5xx Internal
The API may also explicitly instruct the SDK to retry or not retry a request.
To set a custom number of retries, configure the client using the `MaxRetries` property:
```csharp
using Anthropic;
AnthropicClient client = new() { MaxRetries = 3 };
```
Or configure a single method call using `WithOptions`:
```csharp
using System;
var message = await client
.WithOptions(options =>
options with { MaxRetries = 3 }
)
.Messages.Create(parameters);
Console.WriteLine(message);
```
## Timeouts
Requests time out after 10 minutes by default.
To set a custom timeout, configure the client using the `Timeout` option:
```csharp
using System;
using Anthropic;
AnthropicClient client = new() { Timeout = TimeSpan.FromSeconds(42) };
```
Or configure a single method call using `WithOptions`:
```csharp
using System;
var message = await client
.WithOptions(options =>
options with { Timeout = TimeSpan.FromSeconds(42) }
)
.Messages.Create(parameters);
Console.WriteLine(message);
```
## Pagination
The SDK defines methods that return paginated lists of results. It provides convenient ways to access the results either one page at a time or item-by-item across all pages.
### Auto-pagination
To iterate through all results across all pages, use the `Paginate` method, which automatically fetches more pages as needed. The method returns an [`IAsyncEnumerable`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1):
```csharp
using System;
var page = await client.Messages.Batches.List(parameters);
await foreach (var item in page.Paginate())
{
Console.WriteLine(item);
}
```
### Manual pagination
To access individual page items and manually request the next page, use the `Items` property, and `HasNext` and `Next` methods:
```csharp
var page = await client.Messages.Batches.List();
while (true)
{
foreach (var item in page.Items)
{
Console.WriteLine(item);
}
if (!page.HasNext())
{
break;
}
page = await page.Next();
}
```
## Response validation
In rare cases, the API may return a response that doesn't match the expected type. By default, the SDK does not throw an exception in this case. It throws `AnthropicInvalidDataException` only if you directly access the property.
If you would prefer to check that the response is completely well-typed upfront, then either call `Validate`:
```csharp
var message = await client.Messages.Create(parameters);
message.Validate();
```
Or configure the client using the `ResponseValidation` option:
```csharp
using Anthropic;
AnthropicClient client = new() { ResponseValidation = true };
```
Cut at 300 lines.