# .NET

Build AI applications with C# using the Graphlit SDK.

{% hint style="info" %}
**New to Graphlit?** Complete the [Quickstart tutorial](/getting-started/quickstart.md) for a hands-on introduction.
{% endhint %}

***

## Installation

Install the Graphlit client with NuGet:

```bash
dotnet add package Graphlit
```

**Requirements:**

* .NET 6.0 or higher (or .NET 8.0+)
* Graphlit account with [API credentials](/account-setup-one-time/create-project.md)

***

## Quick Start

```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Graphlit;

class Program
{
    static async Task Main(string[] args)
    {
        // Reads from environment variables
        using var httpClient = new HttpClient();
        var graphlit = new Graphlit(
            httpClient,
            organizationId: Environment.GetEnvironmentVariable("GRAPHLIT_ORGANIZATION_ID"),
            environmentId: Environment.GetEnvironmentVariable("GRAPHLIT_ENVIRONMENT_ID"),
            jwtSecret: Environment.GetEnvironmentVariable("GRAPHLIT_JWT_SECRET")
        );
        
        // Ingest content
        var response = await graphlit.IngestText.ExecuteAsync(
            name: "Product Requirements",
            text: "Our AI agent needs persistent memory across sessions..."
        );
        
        response.EnsureNoErrors();
        
        Console.WriteLine($"✅ Memory created: {response.Data?.IngestText?.Id}");
    }
}
```

{% hint style="success" %}
**That's it!** Set `GRAPHLIT_ORGANIZATION_ID`, `GRAPHLIT_ENVIRONMENT_ID`, and `GRAPHLIT_JWT_SECRET` in your environment and the SDK reads them automatically.
{% endhint %}

***

## Configuration

### Environment Variables (Production)

**Option 1: launchSettings.json** (Development)

```json
{
  "profiles": {
    "MyApp": {
      "environmentVariables": {
        "GRAPHLIT_ORGANIZATION_ID": "your_actual_org_id",
        "GRAPHLIT_ENVIRONMENT_ID": "your_actual_env_id",
        "GRAPHLIT_JWT_SECRET": "your_actual_jwt_secret"
      }
    }
  }
}
```

**Option 2: User Secrets** (Development)

```bash
dotnet user-secrets init
dotnet user-secrets set "GRAPHLIT_ORGANIZATION_ID" "your_actual_org_id"
dotnet user-secrets set "GRAPHLIT_ENVIRONMENT_ID" "your_actual_env_id"
dotnet user-secrets set "GRAPHLIT_JWT_SECRET" "your_actual_jwt_secret"
```

**Option 3: appsettings.json** (Never commit secrets)

```json
{
  "Graphlit": {
    "OrganizationId": "",
    "EnvironmentId": "",
    "JwtSecret": ""
  }
}
```

Load with IConfiguration:

```csharp
using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables()  // Overrides appsettings
    .AddUserSecrets<Program>()  // Development secrets
    .Build();

using var httpClient = new HttpClient();
var graphlit = new Graphlit(
    httpClient,
    organizationId: config["Graphlit:OrganizationId"],
    environmentId: config["Graphlit:EnvironmentId"],
    jwtSecret: config["Graphlit:JwtSecret"]
);
```

{% hint style="warning" %}
**Security:** Use Azure Key Vault, AWS Secrets Manager, or environment variables in production. Never commit secrets to source control. Add `appsettings.*.json` with secrets to `.gitignore`.
{% endhint %}

***

## Common Patterns

### Ingest Content

```csharp
// From URL
var response = await graphlit.IngestUri.ExecuteAsync(
    uri: "https://example.com/document.pdf"
);

response.EnsureNoErrors();

// From text
var textResponse = await graphlit.IngestText.ExecuteAsync(
    name: "Meeting Notes",
    text: "Discussion about Q4 planning..."
);

textResponse.EnsureNoErrors();
```

### Search Memory

```csharp
var input = new ContentFilter
{
    Search = "quarterly planning"
};

var response = await graphlit.QueryContents.ExecuteAsync(input);

response.EnsureNoErrors();

foreach (var content in response.Data?.Contents?.Results ?? Array.Empty<Content>())
{
    Console.WriteLine($"📄 {content.Name}");
}
```

### Chat with Context

```csharp
// Create conversation
var conversationInput = new ConversationInput
{
    Name = "AI Assistant"
};

var conversation = await graphlit.CreateConversation.ExecuteAsync(conversationInput);

conversation.EnsureNoErrors();

// Ask questions
var response = await graphlit.PromptConversation.ExecuteAsync(
    prompt: "What did we discuss about Q4 planning?",
    id: conversation.Data?.CreateConversation?.Id
);

response.EnsureNoErrors();

Console.WriteLine(response.Data?.PromptConversation?.Message?.Message);
```

***

## Next Steps

**Quickstarts:**

* [Quickstart: Your First Agent](/getting-started/quickstart.md) - Build a streaming agent in 7 minutes
* [AI Agents](/tutorials/ai-agents.md) - Create agents with persistent memory

**Examples:**

* [.NET Examples](https://github.com/graphlit/graphlit-samples/tree/main/dotnet) - Console apps and samples
* [Microsoft Agent Framework](/tutorials/ai-agents.md) - Integration patterns

**Resources:**

* [.NET SDK on GitHub](https://github.com/graphlit/graphlit-client-dotnet)
* [Use Case Library](/api-guides/use-cases.md) - 100+ code examples
* [Ask Graphlit](/resources/ask-graphlit.md) - AI code assistant
* [Join Discord](https://discord.gg/ygFmfjy3Qx) - Get help from the community


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.graphlit.dev/sdk-setup/for-dotnet-developers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
