I wrote a simple C# code to read chatAPI, got 403 error

Hi, Try to write a C# code to access https://api.openai.com/v1/engines/davinci/jobs, but get 403 error.
How do I know if my API key is legit?
Thanks

The endpoint is wrong

Your key is probably right

Look in the documentation for the completion endpoint instead

1 Like

should be text-davinci-003? This is the code that ChatGPT gave me, I got 400 error, do I need use NET 4.8?
static void Main(string args)
{
string input = “Hello, how are you”;
string apiKey = “MY key”;

                var client = new HttpClient();
                client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

                var content = new StringContent("{\"prompt\": \"" + input + "\", \"model\": \"text-davinci-003\", \"max_tokens\":100,\"temperature\":0.5}", Encoding.UTF8, "application/json");
                var response = client.PostAsync("https://api.openai.com/v1/engines/davinci/jobs", content).Result;
                var responseString = response.Content.ReadAsStringAsync().Result;

                dynamic responseJson = JsonConvert.DeserializeObject(responseString);
                string completion = responseJson.choices[0].text;

                Console.WriteLine("Completion: " + completion);
            }

Don’t use ChatGPT as it can get it wrong…

The docs are here…

1 Like

As Paul said, Chatgpt has lied to you

You have no choice but to read the online docs

Your API key and the model name is NOT the issue right now

I finally got it, this is working code:
var client = new HttpClient();
client.DefaultRequestHeaders.Add(“Authorization”, $“Bearer {apiKey}”);
{

            var content = new StringContent("{\"model\": \"text-davinci-003\", \"prompt\": \"Say this is a test\", \"temperature\": 0, \"max_tokens\":7}", Encoding.UTF8, "application/json");
            var response = client.PostAsync("https://api.openai.com/v1/completions", content).Result;
            var responseString = response.Content.ReadAsStringAsync().Result;

            dynamic responseJson = JsonConvert.DeserializeObject(responseString);
            string completion = responseJson.choices[0].text;

            Console.WriteLine("Completion: " + completion);
        }

Thank you, Ray and Paul!

2 Likes

Yeah, this catches a lot of people.

Glad you got it sorted.

Feel free to stick around and help others if you can!

2 Likes