This blog post provides instructions for using the official OpenAI .NET library to create a console application with the latest OpenAI Responses API.
The official OpenAI .NET library is developed in collaboration with Microsoft, and the Azure SDK team announced that future releases of the Azure SDK will include the official OpenAI .NET library.
Responses API
The OpenAI Responses API is the latest and most advanced offering from OpenAI. It aims to become a comprehensive tool that integrates all functionalities into one library. Users of the Assistants API should plan to migrate to the Responses API once it matches feature parity, as OpenAI will deprecate the Assistants API in early 2026.
Before you start
To use the OpenAI .NET library, you need to create an OpenAI account and purchase credit to make API calls.
After setting up your account, create a new project and generate an API Key. The API Key will be used to authenticate to the OpenAI service.
Install the Official OpenAI .NET Library
Create a C# Console application and run this command using .NET CLI to install the official OpenAI .NET library.
dotnet add package OpenAI –prerelease
NUnit is being installed for unit testing.
dotnet add package NUnit –version 4.3.2
Setup Console Application
To configure the console application to use the service, open Program.cs and insert the following code.
using NUnit.Framework;
using System;
using OpenAI.Responses;
using OpenAI;
using OpenAI.Chat;
OpenAIResponseClient client = new("o3-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
OpenAIResponse response = await client.CreateResponseAsync(
[ResponseItem.CreateUserMessageItem("What is the capital of France?")]
);
Console.WriteLine($"[ASSISTANT]: {response.Status}");
foreach (ResponseItem outputItem in response.OutputItems)
if (outputItem is MessageResponseItem message)
{
Console.WriteLine($"[{message.Role}] {message.Content.FirstOrDefault()?.Text}");
}
Create Environment Variable for the API-Key
To run the application, add the API Key as an environment variable. For PowerShell, use the following command.
$Env: OPENAI_API_KEY = “Key-Details”
Select Model
In the above code we need to set the LLM model we would like the application to use. We set the model in the following line.
OpenAIResponseClient client = new(“o3-mini”, Environment.GetEnvironmentVariable(“OPENAI_API_KEY”));
We are using o3-mini. To switch models, visit the page and select one.
https://platform.openai.com/settings/organization/limits
Run the Program
To run the program, simply save the code and run using the following command.
dotnet run
The result should be:
[ASSISTANT]: Completed
[Assistant] The capital of France is Paris.
Discover more from CPI Consulting Pty Ltd Experts in Cloud, AI and Cybersecurity
Subscribe to get the latest posts sent to your email.