Introduction

Welcome to the first lesson of Getting Started with OpenAI API in C#. In this lesson, you will set up your development environment and send a test request to OpenAI’s GPT-4o model using C#. Getting the environment configured correctly is essential for smooth progress with OpenAI APIs, so we’ll make sure you’re ready from the start.

By the end of this lesson, you will have a working .NET console project, know how to install the necessary libraries, securely store your API key, and verify your setup by sending a simple prompt to the OpenAI API. This foundation will prepare you for hands-on tasks and more advanced features later in the course.

Creating a .NET Console Project

First, create a new .NET console project in a new folder as your workspace for this course. If you’ve set up projects before, this step will be familiar—just open your terminal and run:

This creates a folder named MediaTranscriber with a minimal project structure, and moves you into that folder. We’ll use .NET 9.0 throughout this course.

Installing the OpenAI Package

Add the OpenAI package to your project to enable communication with OpenAI’s services. The OpenAI package is an official .NET library that makes it easy to access GPT and other models from your C# code. It handles authentication, request formatting, and response parsing, so you can focus on your application logic.

Install it using NuGet by running:

If you’re working in the CodeSignal environment, this package is already installed for you. For your own future projects, you can learn more about the package on its official GitHub repository.

Configuring Environment Variables

To use OpenAI’s services, you’ll need an API key. Keep this key private and never hardcode it in your source files. Store it securely in a file named .env in your project folder:

Replace your_openai_api_key_here with your actual API key. You can obtain an API key from the OpenAI platform dashboard. Using environment variables ensures your credentials are not exposed in version control or shared codebases.

Making Your First Request

Before working with transcription, let’s verify your setup by sending a test request to OpenAI’s GPT-4o model. Open Program.cs and add the following code:

Part 1: Importing Namespaces and Retrieving Environment Variables

Here, you import the necessary namespaces and retrieve your API key and (optionally) a custom OpenAI base URL from environment variables. This ensures your credentials and endpoint are not hardcoded in your source files.

Part 2: Configuring the OpenAI Client

In this part, you configure the client options with the endpoint, create a credential object using your API key, and initialize the ChatClient with the GPT-4o model, credential, and options. This setup is flexible and works with both the default and custom OpenAI endpoints.

Part 3: Sending a Prompt and Printing the Response

Here, you send a prompt to the GPT-4o model using the CompleteChat method and print the assistant’s response to the console. This confirms that your setup is working as expected.

Running and Verifying Your Application

Run your project with:

If everything is set up correctly, you’ll see output similar to:

If you don’t see the expected result, double-check your .env file, API key, and package installation. Pay attention to error messages about missing environment variables or misconfiguration—these are the most common setup issues.

Lesson Summary and Next Steps

In this lesson, you set up a .NET console project, installed the OpenAI package, securely configured your API key, and successfully sent your first test request to the OpenAI API. These essential steps establish your foundation for working with OpenAI in C#.

Before starting with transcription in the practice section, you’ll get a chance to get acquainted with the CodeSignal environment and send a simple request to see everything in action. With your setup verified, you’re ready to move on to real-world transcription tasks and the advanced features of the GPT-4o-Transcribe model.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal