C# to call openAI always gives 400 Bad request


            var payload = new
            {
                model = "gpt-4o",  // Use the correct model name (check if 'gpt-4o' is valid)
                messages = new[]
                {
                    new { role = "system", content = "You are an automation engineer, who works like a Locator identifier." },
                    new { role = "user", content = $"{prompt}" }
                },
                max_tokens = 1000,
                temperature = 0.7
            };

            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
                

                    
                    var response = client.PostAsync("https://api.openai.com/v1/completions", new StringContent(payload.ToString(),Encoding.UTF8, "application/json")).Result;
                    var responseString = response.Content.ReadAsStringAsync().Result;

                string responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                var responseJson = JsonDocument.Parse(responseBody);

                Console.WriteLine("Completion: " + responseJson);
                

                // Verify response structure
                if (responseJson.RootElement.TryGetProperty("choices", out JsonElement choices) &&
                    choices.GetArrayLength() > 0 &&
                    choices[0].TryGetProperty("message", out JsonElement message) &&
                    message.TryGetProperty("content", out JsonElement contentElement))
                {
                    
                    output = contentElement.GetString();
                    AddToFile();
                    return contentElement.GetString();
                }

I am trying to call openai using the above method. But of vain. I am getting 400 bad request and not able to figure out what the issue is. I tried addign Host and User Agent and even tried to make charset removed from the header to match how the API works in postman. Am I doing anything wrong? My prompt is 2500 tokens and I am expecting output in 1000 tokens, which si way within the limits

You can try the ApiUrl “v1/chat/completions” instead “v1/completions” for gpt-4o version