← Blog

Claude API .NET/C# SDK Example: A Working Guide

2026-10-26 · 4 min read · SubToAPI Team

If you're searching for a Claude API .NET or C# SDK, the short answer is: Anthropic doesn't publish one. Official SDKs exist for Python and TypeScript/JavaScript only. For .NET, you have two real options: call the REST API directly with HttpClient (straightforward, no dependencies), or use a community-maintained NuGet wrapper (less documented, versions lag behind the API).

This guide shows you how to do the first option properly — a working C# example that sends messages, streams responses, and handles tool use — plus where a hosted layer like SubToAPI can remove some of the boilerplate if you're shipping this to production.

Why there's no official Claude .NET SDK

Anthropic's REST API is simple enough (a handful of JSON endpoints) that an official SDK for every language isn't strictly necessary — HttpClient plus System.Text.Json gets you 95% of the way there with none of the dependency risk. The tradeoff is you write your own request/response models and handle retries, streaming parsing, and error codes yourself.

Basic C# example: sending a message

Here's a minimal working call using HttpClient and record types for the payload:

using System.Net.Http.Json;
using System.Text.Json;

var client = new HttpClient();
client.DefaultRequestHeaders.Add("x-api-key", Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY"));
client.DefaultRequestHeaders.Add("anthropic-version", "2023-06-01");

var payload = new
{
    model = "claude-sonnet-4-5",
    max_tokens = 1024,
    messages = new[]
    {
        new { role = "user", content = "Explain nullable reference types in two sentences." }
    }
};

var response = await client.PostAsJsonAsync(
    "https://api.anthropic.com/v1/messages",
    payload
);

var json = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(json);
var text = doc.RootElement
    .GetProperty("content")[0]
    .GetProperty("text")
    .GetString();

Console.WriteLine(text);

This works as-is in a console app, minimal API, or Blazor server component. Wrap it in a service class and inject HttpClient via IHttpClientFactory for anything production-sized.

Streaming responses in C#

Claude's streaming endpoint returns Server-Sent Events. HttpClient doesn't parse SSE natively, so you read the stream line by line:

var request = new HttpRequestMessage(HttpMethod.Post, "https://api.anthropic.com/v1/messages")
{
    Content = JsonContent.Create(new
    {
        model = "claude-sonnet-4-5",
        max_tokens = 1024,
        stream = true,
        messages = new[] { new { role = "user", content = "Write a haiku about async/await." } }
    })
};

using var stream = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
using var reader = new StreamReader(await stream.Content.ReadAsStreamAsync());

while (!reader.EndOfStream)
{
    var line = await reader.ReadLineAsync();
    if (line is null || !line.StartsWith("data:")) continue;

    var data = line["data:".Length..].Trim();
    if (data == "[DONE]") break;

    using var doc = JsonDocument.Parse(data);
    if (doc.RootElement.TryGetProperty("delta", out var delta) &&
        delta.TryGetProperty("text", out var textToken))
    {
        Console.Write(textToken.GetString());
    }
}

This is the pattern you'd use for a chat UI in a Blazor or MAUI app where you want tokens appearing as they arrive rather than waiting for the full response.

Tool use (function calling) in C#

Tool use works the same way in C# as any other language — you declare the tool schema in the request, and Claude's response includes a tool_use block when it decides to call it:

var payload = new
{
    model = "claude-sonnet-4-5",
    max_tokens = 1024,
    tools = new object[]
    {
        new
        {
            name = "get_weather",
            description = "Get current weather for a city",
            input_schema = new
            {
                type = "object",
                properties = new { city = new { type = "string" } },
                required = new[] { "city" }
            }
        }
    },
    messages = new[]
    {
        new { role = "user", content = "What's the weather in Lisbon?" }
    }
};

Parse the response's content array, check for a block with "type": "tool_use", extract the input object, run your actual C# method, and send the result back in a follow-up message with tool_result. This round-trip is identical regardless of language — the full spec is covered in detail in /docs/tools if you want the exact schema.

Skipping the plumbing with SubToAPI

Everything above works, but at team scale it means every developer manages their own API key, there's no shared usage visibility, and you're writing the same auth headers and retry logic in every service.

SubToAPI sits between your C# code and Claude: it issues scoped application keys (sub_live_...), handles streaming and tool use with the same request shape shown above, and gives you per-key usage metadata in a dashboard — useful when multiple .NET services or team members are calling Claude under one account.

The C# code barely changes — swap the host and header:

client.DefaultRequestHeaders.Add("Authorization", $"Bearer {Environment.GetEnvironmentVariable("SUBTOAPI_KEY")}");

var response = await client.PostAsJsonAsync(
    "https://api.subtoapi.app/v1/messages",
    payload
);

Same JSON shape, same streaming behavior documented at /docs/streaming, same message structure at /docs/messages. You get a free trial at /signup, and plans start at €9/month for solo use, scaling to €19 or €49 per seat for teams — see /pricing for the full breakdown. For a first integration, /docs/quickstart walks through the exact request this article uses.

Whether you call Claude directly or through a proxy layer, the underlying C# code is the same HttpClient pattern — the SDK question really comes down to how much key management and observability you want to build yourself versus get out of the box.

questions

Is there an official Anthropic SDK for .NET or C#? No. Anthropic maintains official SDKs for Python and TypeScript only. For .NET you either call the REST API directly with HttpClient, as shown above, or use a third-party NuGet package, which may lag behind new API features.

Can I use HttpClient for streaming Claude responses in C#? Yes. HttpClient doesn't parse Server-Sent Events natively, but you can read the response stream with a StreamReader, split on data: lines, and parse each JSON chunk as shown in the streaming example above.

Do I need a package to handle tool use in C#? No package is required. Tool use is just a specific JSON request/response shape — define your tool schema, send it in the tools array, and parse tool_use blocks from the response content with System.Text.Json, the same way you'd parse any other API response.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →