Seeking Assistance with 404 Error on OpenAI API - Novice User

Hello OpenAI Community,

I am new to programming and have embarked on a project that’s quite a stretch for me. I’ve been using ChatGPT-4 to guide me through setting up and coding HTML, CSS, and JavaScript for using the OpenAI API. Despite following guidance and checking my setup numerous times, I am encountering a persistent “404 Not Found” error when attempting POST requests to the OpenAI API.

Issue Description:
Every time I try to execute a POST request to https://api.openai.com/v1/completions, I receive a 404 error, indicating that the server can’t find the requested resource. Below are the details of the error messages I am seeing in my console:

api.openai.com/v1/completions:1 Failed to load resource: the server responded with a status of 404 ()
pen.js:24 Error generating content: Error: API call failed with status: 404 at generateQuestions (pen.js:47:13) at async HTMLButtonElement.<anonymous> (pen.js:11:25)

Code Snippet:

document.addEventListener("DOMContentLoaded", () => {
  const generateBtn = document.getElementById("generateBtn");
  generateBtn.addEventListener("click", async () => {
    const jobTitle = document.getElementById("jobTitle").value;
    const educationLevel = document.getElementById("educationLevel").value;
    const yearsExperience = document.getElementById("yearsExperience").value;

    try {
      const questions = await generateQuestions(jobTitle, educationLevel, yearsExperience);
      const certifications = await generateCertifications(jobTitle, yearsExperience);

      displayQuestions(questions);
      displayCertifications(certifications);
    } catch (error) {
      console.error("Error generating content:", error);
    }
  });

  async function generateQuestions(jobTitle, educationLevel, yearsExperience) {
    const response = await fetch("https://api.openai.com/v1/completions", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: "Bearer [API_KEY_HIDDEN_FOR_SECURITY]"
      },
      body: JSON.stringify({
        model: "gpt-3.5-turbo",
        prompt: `As a ${yearsExperience} years experienced ${jobTitle} with ${educationLevel} education, please provide 7 recommended interview questions and 3 optional questions.`,
        max_tokens: 200,
        temperature: 0.7,
        n: 1,
        stop: ["###"]
      })
    });

    if (!response.ok) {
      throw new Error(`API call failed with status: ${response.status}`);
    }

    const data = await response.json();
    if (!data.choices || data.choices.length === 0) {
      throw new Error("No choices returned from the API.");
    }

    return data.choices[0].text.trim().split("\n");
  }
});

Additional Context:

  • I am using the “gpt-3.5-turbo” model.
  • I have verified the API key is correct.
  • I have credits available that have not been used due to this error.

I would greatly appreciate any insights or guidance on what might be causing this error and how to resolve it. I am eager to learn and successfully implement this API for my project.

Thank you all in advance for your help and patience!

Maybe the issue is in your end point? As per this Documentation the correct end point would be

https://api.openai.com/v1/chat/completions
1 Like

Jayroc,

did you ever find a fix to your problem? Because im having the same thing happening.

This is usually either old libraries or confusion about the endpoint address, if you can post a code segment in a new topic thread, we can take a look.