February 20 2023
ChatGPT (Generative Pre-trained Transforer) is a large language model developed by OpenAI. It is designed to understand natural language input from users and generate appropriate responses using advanced algorithms and machine learning techniques. ChatGPT can be used in a variety of applications, including chatbots, virtual assistants, customer service, and more. Here I will show you how you can integrate/call ChatGPT API using C# programming language in 5 steps in 5 minutes.
In this step, adding the OpenAI NuGet package is crucial for connecting your C# application to ChatGPT. It's a simple yet essential part of integrating AI in .NET.
To integrate ChatGPT, the initial step involves installing the OpenAI C# SDK. This can be accomplished by executing the following command in the Package Manager Console using the NuGet package manager:

In order to be able to call and authenticate to the OpenAI API, it is necessary to generate a unique API key. To do this: • Visit: https://platform.openai.com/account/api-keys • Log in • Create your API Key by clicking on "Create new secret key" button.

After successfully installing the OpenAI C# SDK, the next step is to initialize it by providing your OpenAI API key. To accomplish this, create an instance of the OpenAI class and pass your API key as a parameter.

In order to call the API and get a response to the prompt we set, it is necessary to call the CreateCompletionAsync() method, which accepts the following parameters:

Once the process of generating the completions has concluded, it is possible to exhibit them within your C# application.
Congratulations! By following these straightforward steps, you now have the ability to seamlessly integrate ChatGPT into your C# code and generate text completions using the model. This was a light and short newsletter issue considering that I haven't commented on ChatGPT so far, so I wanted to do something practical.
Make a coffee and check out source code directly on my GitHub repository.
Want to build AI features in .NET? Download the AI in .NET Starter Kit - complete source code for Semantic Search and RAG System, ready to run with Ollama Mistral and Neon Serverless DB.
Stop arguing about code style. In this course you get a production-proven setup with analyzers, CI quality gates, and architecture tests — the exact system I use in real projects. Join here.
Not sure yet? Grab the free Starter Kit — a drop-in setup with the essentials from Module 01.
Design Patterns that Deliver — Solve real problems with 5 battle-tested patterns (Builder, Decorator, Strategy, Adapter, Mediator) using practical, real-world examples. Trusted by 650+ developers.
Just getting started? Design Patterns Simplified covers 10 essential patterns in a beginner-friendly, 30-page guide for just $9.95.
Every Monday morning, I share 1 actionable tip on C#, .NET & Architecture that you can use right away. Join here.
Join 20,000+ subscribers who mass-improve their .NET skills with actionable tips on C#, Architecture & Best Practices.
Subscribe to the TheCodeMan.net and be among the 20,000+ subscribers gaining practical tips and resources to enhance your .NET expertise.