DifySDK 0.2.1
dotnet add package DifySDK --version 0.2.1
NuGet\Install-Package DifySDK -Version 0.2.1
<PackageReference Include="DifySDK" Version="0.2.1" />
<PackageVersion Include="DifySDK" Version="0.2.1" />
<PackageReference Include="DifySDK" />
paket add DifySDK --version 0.2.1
#r "nuget: DifySDK, 0.2.1"
#:package DifySDK@0.2.1
#addin nuget:?package=DifySDK&version=0.2.1
#tool nuget:?package=DifySDK&version=0.2.1
DifySDK
Native AOT-compatible Refit interfaces and source-generated System.Text.Json metadata for the Dify API.
For dependency-injection registration and API-key authentication, install DifySDK.Extensions.
Install
dotnet add package DifySDK
Create a client directly
using System.Net.Http.Headers;
using System.Text.Json;
using DifySDK.Chats;
using DifySDK.Models;
using DifySDK.Serialization;
using Refit;
var httpClient = new HttpClient
{
BaseAddress = new Uri("https://api.dify.ai/v1/")
};
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", "app-your-api-key");
var settings = new RefitSettings(
new SystemTextJsonContentSerializer(new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
TypeInfoResolver = DifyJsonSerializerContext.Default
}));
var chat = RestService.For<IDifyChatApi>(httpClient, settings);
var response = await chat.SendAsync(new ChatMessageRequest
{
Query = "What can you do?",
User = "user-123",
InputValues = new Dictionary<string, object?>
{
["language"] = "en"
}
});
Console.WriteLine(response.Answer);
InputValues lets callers use ordinary .NET values. The SDK converts them to the Dictionary<string, JsonElement> shape required by Dify without reflection, which is suitable for Native AOT.
Consume streaming responses
Use the IAsyncEnumerable<DifyStreamEvent> extension for all supported SSE endpoints.
using DifySDK.Chats;
using DifySDK.Models;
using DifySDK.Streaming;
await foreach (var streamEvent in chat.StreamAsync(new ChatMessageRequest
{
Query = "Write a haiku about APIs",
User = "user-123"
}))
{
if (streamEvent is DifyMessageStreamEvent message)
Console.Write(message.Answer);
if (streamEvent is DifyErrorStreamEvent error)
Console.Error.WriteLine(error.Message);
}
The same pattern is available for completions, workflows, workflow event reconnection, knowledge-base pipeline runs, and datasource-node runs.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Refit (>= 13.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DifySDK:
| Package | Downloads |
|---|---|
|
DifySDK.Extensions
A typed .NET client for the Dify API |
GitHub repositories
This package is not used by any popular GitHub repositories.
fix dependency inject
**Full Changelog**: https://github.com/LGinC/DifySDK/compare/v0.2...v0.2.1