> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openlayer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# / .NET

> Use the Openlayer C# / .NET SDK to interact with the Openlayer platform

You can use the `Openlayer` C# / .NET SDK to interact with the Openlayer platform. It supports .NET 8+ and any `netstandard2.0` runtime, including .NET Framework 4.6.2+.

## Install

You can install the `Openlayer` package from **NuGet**:

```bash theme={null}
dotnet add package Openlayer
```

## Usage

The client reads your API key from the `OPENLAYER_API_KEY` environment variable. The methods follow the REST paths of the [API reference](https://docs.openlayer.com/api-reference/rest/overview.md).

```csharp theme={null}
using Openlayer;
using Openlayer.Api.InferencePipelines.Item.DataStream;
using Openlayer.Api.Models;

var client = new OpenlayerClient(); // reads OPENLAYER_API_KEY

var response = await client.InferencePipelines[pipelineId].DataStream.PostAsync(new DataStreamPostRequestBody
{
    Config = new DataStreamPostRequestBody.DataStreamPostRequestBody_config
    {
        LLMData = new LLMData
        {
            InputVariableNames = new() { "user_query" },
            OutputColumnName = "output",
            NumOfTokenColumnName = "tokens",
            CostColumnName = "cost",
            TimestampColumnName = "timestamp",
        },
    },
    Rows = new()
    {
        new DataStreamPostRequestBody_rows
        {
            AdditionalData = new Dictionary<string, object>
            {
                ["user_query"] = "what is the meaning of life?",
                ["output"] = "42",
                ["tokens"] = 7,
                ["cost"] = 0.02,
                ["timestamp"] = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
            },
        },
    },
});
```

To use a self-hosted deployment, set the `OPENLAYER_BASE_URL` environment variable or pass `OpenlayerClientOptions.BaseUrl`.

## Tracing

The `Openlayer.Tracing` namespace traces your LLM app. Each request becomes a trace in your
inference pipeline, with the same columns as the Python and TypeScript SDKs. Set
`OPENLAYER_API_KEY` and `OPENLAYER_INFERENCE_PIPELINE_ID`, then:

* Wrap your functions with `Tracer.TraceAsync(name, inputs, fn)`. Steps nest by async flow, so
  concurrent requests are separate traces.
* Call `.WithOpenlayerTracing()` on any `Microsoft.Extensions.AI` `IChatClient` (OpenAI, Azure
  OpenAI, Ollama, ...) to record each LLM call with its model, tokens and parameters.
* Call `await Tracer.FlushAsync()` before a short-lived process exits.

See [Instrument your code](https://docs.openlayer.com/monitoring/instrument.md) for a full example.

## Learn more

To learn more about the `Openlayer` C# / .NET SDK, check out the GitHub repository:

> [card] [SDK GitHub repository](https://github.com/openlayer-ai/openlayer-csharp)
>
> Check out the source code of the C# / .NET SDK.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.