# .NET SDK

> Use the core .NET client with API keys or browser sign-in, search and read code, and ask Corfedo AI with streaming, reattach and your own tools.

Source: https://docs.corfedo.ai/connect/sdks/dotnet/

`Corfedo.Client` is the framework-independent .NET 10 client. `Corfedo.Extensions.AI` adds Corfedo to [Microsoft.Extensions.AI](https://docs.corfedo.ai/connect/frameworks/microsoft-extensions-ai/). The packages use Apache-2.0. The packages are not on nuget.org. Release candidates are on the GitHub Packages feed `https://nuget.pkg.github.com/corfedo/index.json`, which needs a GitHub account with package read access.

`client.Search.SearchAsync` and `client.Code.ReadFileAsync` search and read code, see [Search and read code](https://docs.corfedo.ai/connect/api/search-and-read/).

## Ask Corfedo AI

Use a key file with **Search and read code** and **Ask Corfedo AI**:

```csharp
using Corfedo.Client;
using Corfedo.Client.Models;

using var client = await CorfedoClient.FromApiKeyFileAsync(
    Environment.GetEnvironmentVariable("CORFEDO_API_KEY_FILE")!);
var run = client.Ai.Stream(new()
{
    ClientMessageId = Guid.CreateVersion7(),
    Text = "How are search requests routed?"
});
await foreach (var item in run)
    if (item is AiTextDeltaEvent delta) Console.Write(delta.Text);
Console.WriteLine(run.Outcome);
Console.WriteLine(run.Answer);
```

`run.Answer`, `Citations` and `Usage` come from the completed run. Do not join the streamed text. `SendAsync` waits without streaming and returns an `AiRunResult`. A wait can end before completion with the run still in progress.

Continue with `ConversationId` and a new `ClientMessageId`. Use `ListProfilesAsync`, `ListConversationsAsync`, `GetConversationAsync` and `DeleteConversationAsync` on `client.Ai` for discovery and history.

## Sign in a person

```csharp
using Corfedo.Client.Authentication;

var credentials = await CorfedoAuthentication.SignInAsync(new()
{
    ApiBaseUri = new Uri("https://corfedo.example.com/"),
    BrowserSignInStarted = signIn =>
        Console.Error.WriteLine($"If the browser does not open, open this link: {signIn.AuthorizationUri}")
});
using var client = new CorfedoClient(credentials);
```

The SDK opens the system browser and waits for the browser to return to a `localhost` address. Show `AuthorizationUri` so the person can open it when the browser does not start. `BrowserOpened` says whether it started.

Where no browser can open, for example over SSH, set `OpenBrowser = false`. After the person signs in on another computer, their browser goes to a `localhost` address that does not load there. Pass that address to `signIn.CompleteAsync` to finish sign-in. It throws `ArgumentException` for an address from another sign-in.

Sign-in does not save the credentials. To keep a person signed in, see [Keep a human session alive](https://docs.corfedo.ai/connect/authentication/#keep-a-human-session-alive).

## Reattach and cancel

```csharp
var observer = client.Ai.Reattach(run.RunId!.Value, run.Sequence);
await foreach (var item in observer) Console.WriteLine(item);
var snapshot = await client.Ai.GetRunAsync(run.RunId.Value);
var cancellation = await client.Ai.CancelAsync(run.RunId.Value);
```

Run the calls in `RequiredCalls` of the stream that sent the message. A reattached stream only lists `PendingCalls`. Declare `SendAiMessage.ClientTools`, execute the owned calls in your application and submit JSON results with `StreamToolResults` or `SubmitToolResultsAsync`. Results use `CallId` and exactly one of `Output` or `Error`.

See [Client tools](https://docs.corfedo.ai/connect/api/ai/client-tools/) for the result limit and recovery after a crash. Cancelling enumeration closes the subscription. `CancelAsync` requests run cancellation.

## Errors and recovery

The core client uses `CorfedoClientException` and `CorfedoFailure`. A missing permission is `AccessDenied`, request conflicts are `Conflict`, and monthly spending refusal is `SpendingLimitReached`.

Use [session renewal](https://docs.corfedo.ai/connect/authentication/#keep-a-human-session-alive) for long-lived human credentials. If the connection drops, the SDK reconnects for up to 30 seconds. See [streaming](https://docs.corfedo.ai/connect/api/ai/streaming/) for snapshot and answer semantics.
