Skip to content

.NET

Obstruo exposes an OpenAI-compatible API. The official OpenAI .NET package supports custom endpoints and works with Obstruo out of the box.

Prerequisites

  • An ObstruoKey and endpoint URL - see Quick Start
  • A Logical Model configured in your project
  • .NET 8 or later (the OpenAI NuGet package requires .NET 8+)

OpenAI .NET SDK

Install the package:

dotnet add package OpenAI

Chat completions

using OpenAI;
using OpenAI.Chat;

var client = new ChatClient(
    model: "obstruo-chat",
    credential: new ApiKeyCredential("your-obstruo-key"),
    options: new OpenAIClientOptions
    {
        Endpoint = new Uri("https://api.obstruo.ai/your-org/v1")
    }
);

ChatCompletion completion = await client.CompleteChatAsync(
    new UserChatMessage("Summarise this contract: ...")
);

Console.WriteLine(completion.Content[0].Text);

Streaming

await foreach (StreamingChatCompletionUpdate update in
    client.CompleteChatStreamingAsync("Tell me a story."))
{
    foreach (ChatMessageContentPart part in update.ContentUpdate)
    {
        Console.Write(part.Text);
    }
}

Embeddings

using OpenAI.Embeddings;

var embeddingClient = new EmbeddingClient(
    model: "obstruo-embedding",
    credential: new ApiKeyCredential("your-obstruo-key"),
    options: new OpenAIClientOptions
    {
        Endpoint = new Uri("https://api.obstruo.ai/your-org/v1")
    }
);

OpenAIEmbedding embedding = await embeddingClient.GenerateEmbeddingAsync(
    "The quick brown fox"
);

ReadOnlyMemory<float> vector = embedding.ToFloats();

Environment variables

Store credentials outside your source code:

export OBSTRUO_API_KEY="your-obstruo-key"
export OBSTRUO_BASE_URL="https://api.obstruo.ai/your-org/v1"
var apiKey = Environment.GetEnvironmentVariable("OBSTRUO_API_KEY")!;
var baseUrl = Environment.GetEnvironmentVariable("OBSTRUO_BASE_URL")!;

var client = new ChatClient(
    model: "obstruo-chat",
    credential: new ApiKeyCredential(apiKey),
    options: new OpenAIClientOptions { Endpoint = new Uri(baseUrl) }
);

Reading gateway metadata

Obstruo adds an obstruo key to each response. Access it via GetRawResponse() or a raw HttpClient call:

using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

using var http = new HttpClient();
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

var payload = JsonSerializer.Serialize(new
{
    model = "obstruo-chat",
    messages = new[] { new { role = "user", content = "Hello!" } }
});

var response = await http.PostAsync(
    baseUrl + "/chat/completions",
    new StringContent(payload, Encoding.UTF8, "application/json")
);

using var doc = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
if (doc.RootElement.TryGetProperty("obstruo", out var meta))
{
    // routing info
    var routing = meta.GetProperty("routing");
    Console.WriteLine(routing.GetProperty("resolved_model").GetString()); // "gpt-4o-mini"
    Console.WriteLine(routing.GetProperty("provider_type").GetString());  // "OpenAI"

    // redaction summary
    var redaction = meta.GetProperty("redaction");
    Console.WriteLine(redaction.GetProperty("total_entities").GetInt32()); // 0 = no PII
}

Next steps