Connect to ChatGPTs API using C# (with Blazor and Console examples.)
Article have related YouTube video
This article is a guide to using the OpenAI API to generate text using the ChatGPT model. It explains how to create an API key and how to use a simple C# method to retrieve a "lorem ipsum" string in a Console app, and to generate an Instagram post by the input of a topic in a Blazor Server App.
Before we can get started using the API, you need to get an API key. It is completely free to create an account and as a new user you get $18 in free trial usage that will last for 3 months. This is a good way to learn to program against the API for literally free. After that you need to insert some money, however it is very cheap to make requests. Ex. to make it generate an Instagram post with hashtags about a specific topic, it would cost around $0.01-$0.02 to generate the text.
Getting API key from ChatGPT
Go to https://openai.com/api/ and login. After that click on the profile-menu at the right top and choose View API Keys from the list. On API keys page click Create new secret key. It is important that you save this key a safe place where you can copy/paste it in the future because you need to use it for the code we are going to make in C# to connect to the API. The API key is at is says, the key to your specific account so that you can use the API as you (your account), so don’t share it with people you don’t trust. Once you see the key on the website, you can not retrieve it again, so if you need it in the future you have to save it. Another option is to just create another key, however for security reasons you might want to keep it at a low, how many keys you have generated
Simple C# method (Copy/paste to Console App)
This method will simply retrieve a lorem ipsum string for you. Remember to insert your own API key into the apikey variable! You would also have to install the Newtonsoft.Json NuGet Package. But when that is done, you can go and copy and paste this into a Console App.
The jsonContent you sent with the API request is the prompt, model and max_tokens. There is a lot more options in the API Documentation. However the prompt is the text your send to the API just like when you write to ChatGPT in their own solution. The Model is what AI you want to connect to, this can change in the future as the AI is getting better versions. The max_tokens is the maximum amount of tokens you want to use on the request, which is like OpenAI's own currency for using their API. There is many factors about how many tokens you use on a request, like what AI model you contact and how long your own request text is. For more information about tokens, check this link
Blazor example
In this example we will use an input field to insert a topic and a button that will run a method that generates an instagram post.
First include this HttpClient to the Program.cs file.
Program.cs
For the actual page take this code an remember to insert your own API key. It is basically just an input field where you write a topic. Then enter the button and wait for a response. Remember it can take a couple of seconds to get the response, but it will come if your code doesnt crash of course. Also remember again to install the Newtonsoft.Json Nuget Package.