claude-code-system-prompts/system-prompts/data-claude-api-reference-c.md
2026-02-18 14:50:58 -07:00

70 lines
1.6 KiB
Markdown

<!--
name: 'Data: Claude API reference — C#'
description: C# SDK reference including installation, client initialization, basic requests, streaming, and tool use
ccVersion: 2.1.47
-->
# Claude API — C#
> **Note:** The C# SDK is the official Anthropic SDK for C# (currently in beta). Tool runner and Agent SDK are not available.
## Installation
\`\`\`bash
dotnet add package Anthropic
\`\`\`
## Client Initialization
\`\`\`csharp
using Anthropic;
// Default (uses ANTHROPIC_API_KEY env var)
AnthropicClient client = new();
// Explicit API key (use environment variables — never hardcode keys)
AnthropicClient client = new() {
ApiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY")
};
\`\`\`
---
## Basic Message Request
\`\`\`csharp
using Anthropic.Models.Messages;
var parameters = new MessageCreateParams
{
Model = Model.ClaudeOpus4_6,
MaxTokens = 1024,
Messages = [new() { Role = Role.User, Content = "What is the capital of France?" }]
};
var message = await client.Messages.Create(parameters);
Console.WriteLine(message);
\`\`\`
---
## Streaming
\`\`\`csharp
var parameters = new MessageCreateParams
{
Model = Model.ClaudeOpus4_6,
MaxTokens = 1024,
Messages = [new() { Role = Role.User, Content = "Write a haiku" }]
};
await foreach (var msg in client.Messages.CreateStreaming(parameters))
{
Console.Write(msg);
}
\`\`\`
---
## Tool Use (Manual Loop)
The C# SDK supports raw tool definitions via JSON schema. See the [shared tool use concepts](../shared/tool-use-concepts.md) for the tool definition format and agentic loop pattern.