Originally published at claudeguide.io/claude-api-csharp-dotnet
Claude API C# .NET Tutorial: Complete Integration Guide (2026)
You can call the Claude API from C# .NET using the official Anthropic NuGet package — install with dotnet add package Anthropic.SDK, set your ANTHROPIC_API_KEY, and send messages in under 15 lines of code. The SDK supports .NET 6+, streaming, tool use, prompt caching, and multi-turn conversations. This tutorial walks through setup, authentication, every major feature, and production patterns with tested code examples.
Installation and Setup
Create a new project or add the SDK to an existing one:
dotnet new console -n ClaudeDemo
cd ClaudeDemo
dotnet add package Anthropic.SDK
Set your API key as an environment variable:
# Linux/macOS
export ANTHROPIC_API_KEY="sk-ant-..."
# Windows PowerShell
$env:ANTHROPIC_API_KEY = "sk-ant-..."
Or store it in appsettings.json for ASP.NET Core projects:
{
"Anthropic": {
"ApiKey": "sk-ant-..."
}
}
Security note: Never hardcode API keys in source files. Use environment variables, user secrets (dotnet user-secrets), or Azure Key Vault in production.
Your First API Call
using Anthropic.SDK;
using Anthropic.SDK.Messaging;
var client = new AnthropicClient();
var message = await client.Messages.CreateAsync(new MessageParameters
{
Model = "claude-sonnet-4-5",
MaxTokens = 1024,
Messages = new List<Message
---
## Multi-Turn Conversations
Build conversation history by tracking messages:
csharp
var conversation = new List<Message
Top comments (0)