OpenRouter.NET
0.4.0
See the version list below for details.
dotnet add package OpenRouter.NET --version 0.4.0
NuGet\Install-Package OpenRouter.NET -Version 0.4.0
<PackageReference Include="OpenRouter.NET" Version="0.4.0" />
<PackageVersion Include="OpenRouter.NET" Version="0.4.0" />
<PackageReference Include="OpenRouter.NET" />
paket add OpenRouter.NET --version 0.4.0
#r "nuget: OpenRouter.NET, 0.4.0"
#:package OpenRouter.NET@0.4.0
#addin nuget:?package=OpenRouter.NET&version=0.4.0
#tool nuget:?package=OpenRouter.NET&version=0.4.0
OpenRouter.NET
A modern .NET SDK for the OpenRouter API - providing a unified interface to access multiple LLM providers with streaming support, tool calling, artifacts, and structured object generation.
🆕 Generate Object - Get Structured Data from LLMs
Generate validated, typed objects from LLMs without manual JSON parsing:
import { z } from 'zod';
import { useGenerateObject } from '@openrouter-dotnet/react';
const schema = z.object({
name: z.string(),
age: z.number(),
hobbies: z.array(z.string())
});
const { object } = useGenerateObject({
schema,
prompt: "Generate a profile for a developer named Sarah",
endpoint: '/api/generate-object'
});
// object is fully typed with TypeScript IntelliSense!
📘 Quick Start Guide | 📚 Full Documentation
Features
🚀 Simple and intuitive API 🔒 Type-safe with full async/await support 📦 Lightweight with minimal dependencies 🎯 Built for .NET 9.0+ 🔄 Full streaming support 🛠️ Tool calling (server-side auto-execute and client-side modes) 📄 Artifact support with incremental parsing ⚡ Async enumerable streaming (IAsyncEnumerable) ✨ NEW: Structured object generation with Zod schemas
Installation
dotnet add package OpenRouter.NET
Or via NuGet Package Manager:
Install-Package OpenRouter.NET
For LLMs
Want to use this SDK with your LLM? Check out llms.txt - give this file to your LLM of choice and it will be ready to implement features with OpenRouter.NET for you!
Quick Start
Basic Usage
using OpenRouter.NET;
using OpenRouter.NET.Models;
var client = new OpenRouterClient("your-api-key");
var request = new ChatCompletionRequest
{
Model = "anthropic/claude-3.5-sonnet",
Messages = new List<Message>
{
Message.FromUser("Hello! What's the weather like?")
}
};
var response = await client.CreateChatCompletionAsync(request);
Console.WriteLine(response.Choices[0].Message.Content?.ToString() ?? "No response");
Streaming
using OpenRouter.NET;
using OpenRouter.NET.Models;
var client = new OpenRouterClient("your-api-key");
var request = new ChatCompletionRequest
{
Model = "anthropic/claude-3.5-sonnet",
Messages = new List<Message>
{
Message.FromUser("Tell me a story")
}
};
await foreach (var chunk in client.StreamAsync(request))
{
if (chunk.TextDelta != null)
{
Console.Write(chunk.TextDelta);
}
}
SSE Streaming (for Web APIs)
Perfect for building real-time streaming endpoints that work with browsers using Server-Sent Events:
using OpenRouter.NET;
using OpenRouter.NET.Sse;
using OpenRouter.NET.Models;
// In your ASP.NET endpoint
app.MapPost("/api/chat/stream", async (HttpContext context, ChatRequest body) =>
{
var client = new OpenRouterClient("your-api-key");
var request = new ChatCompletionRequest
{
Model = "anthropic/claude-3.5-sonnet",
Messages = body.Messages
};
// One line to stream everything as SSE!
await client.StreamAsSseAsync(request, context.Response);
});
record ChatRequest(List<Message> Messages);
Client-side (JavaScript/TypeScript):
Since this is a POST endpoint with a request body, use the fetch API with streaming:
async function streamChat(messages) {
const response = await fetch('/api/chat/stream', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ messages })
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n').filter(line => line.startsWith('data: '));
for (const line of lines) {
const event = JSON.parse(line.slice(6));
if (event.type === 'text') {
appendText(event.textDelta);
}
}
}
}
Note: The standard
EventSourceAPI only supports GET requests. For POST endpoints with request bodies, use fetch with streaming as shown above. See SSE Helper Documentation for complete client examples, event types, and TypeScript definitions.
This automatically handles:
- ✅ Text deltas
- ✅ Tool calls (server-side and client-side)
- ✅ Artifacts (started, content, completed)
- ✅ Completion events
- ✅ Proper SSE formatting
- ✅ Headers and connection management
Event types sent:
text- Text content deltastool_executing- Tool startingtool_completed- Tool finished with resulttool_error- Tool failedtool_client- Client-side tool call requestedartifact_started- Artifact beginningartifact_content- Artifact content chunkartifact_completed- Artifact finishedcompletion- Stream finishederror- Error occurred
Artifacts
using OpenRouter.NET;
using OpenRouter.NET.Models;
var client = new OpenRouterClient("your-api-key");
var request = new ChatCompletionRequest
{
Model = "anthropic/claude-3.5-sonnet",
Messages = new List<Message>
{
Message.FromUser("Create a hello world function")
}
};
// Enable artifact support
request.EnableArtifactSupport();
await foreach (var chunk in client.StreamAsync(request))
{
if (chunk.TextDelta != null)
{
Console.Write(chunk.TextDelta);
}
if (chunk.Artifact is ArtifactCompleted completed)
{
Console.WriteLine($"\n\nArtifact: {completed.Title}");
Console.WriteLine(completed.Content);
}
}
Conversation (Multi-turn)
using OpenRouter.NET;
using OpenRouter.NET.Models;
var client = new OpenRouterClient("your-api-key");
var conversationHistory = new List<Message>();
// Add user message
conversationHistory.AddUserMessage("What is the capital of France?");
var request = new ChatCompletionRequest
{
Model = "anthropic/claude-3.5-sonnet",
Messages = conversationHistory
};
var response = await client.CreateChatCompletionAsync(request);
var assistantMessage = response.Choices[0].Message.Content?.ToString() ?? "";
// Add assistant response to history
conversationHistory.AddAssistantMessage(assistantMessage);
// Continue conversation
conversationHistory.AddUserMessage("What's the population?");
request.Messages = conversationHistory;
response = await client.CreateChatCompletionAsync(request);
Console.WriteLine(response.Choices[0].Message.Content?.ToString() ?? "No response");
Samples
Check out the samples/ directory for complete working examples:
- BasicCliSample - Interactive CLI to list models and chat with streaming
- OpenRouterWebApiSample - ASP.NET Core Web API showcasing all SDK features including streaming, tools, artifacts, and multimodal
To run a sample:
export OPENROUTER_API_KEY="your-key-here"
cd samples/BasicCliSample
dotnet run
Or for the Web API sample:
export OPENROUTER_API_KEY="your-key-here"
cd samples/OpenRouterWebApiSample
dotnet run
Requirements
- .NET 9.0 or later
Documentation
Full documentation coming soon.
Known Issues
Next.js SSE Streaming: Next.js compression buffers responses before sending, causing SSE streams to arrive as one big chunk. Set compress: false in next.config.js to fix.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
Acknowledgments
Built for the OpenRouter API platform.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- JsonSchema.Net (>= 7.4.0)
-
net9.0
- JsonSchema.Net (>= 7.4.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.7.5 | 39,086 | 2/13/2026 |
| 0.7.4 | 792 | 11/25/2025 |
| 0.7.3 | 507 | 11/19/2025 |
| 0.7.2 | 466 | 11/19/2025 |
| 0.7.1 | 463 | 11/19/2025 |
| 0.7.0 | 469 | 11/19/2025 |
| 0.6.1 | 342 | 11/11/2025 |
| 0.6.0 | 334 | 11/11/2025 |
| 0.5.0 | 549 | 11/6/2025 |
| 0.4.0 | 271 | 11/5/2025 |
| 0.3.1 | 249 | 11/5/2025 |
| 0.3.0 | 251 | 10/28/2025 |
| 0.2.0 | 193 | 10/24/2025 |
| 0.1.0 | 246 | 10/23/2025 |