Search code examples
node.jsnext.jsopenai-api

Dall-E API in Node.js always returns 400 Bad Request


I'm trying to implement OpenAI's Dall-E API in my Next.js project but every reload returns 400 Bad Request.

I'm following the steps exactly as they are in the documentation but I still get this error.

Im using Next.js 13's app directory. This is my page.tsx file below. I'm calling the predict function in an async function.

const { Configuration, OpenAIApi } = require("openai");

async function predict() {
  const configuration = new Configuration({
    apiKey: process.env.OPEN_AI,
  });
  const openai = new OpenAIApi(configuration);

  const response = await openai.createImage(
    {
      prompt: "a white siamese cat",
    },
    {
      validateStatus: function (status: number) {
        return status < 500;
      },
    }
  );

  console.log(response);
}

export default async function Home() {
  const response = await predict();

  return (
    <div className="flex h-full flex-col items-center justify-center px-8">
      <h1>Get a picture of a cat</h1>
    </div>
  );
}

this is the response

    status: 400,
    statusText: 'BAD REQUEST',
    headers: {
      date: 'Wed, 11 Jan 2023 10:42:12 GMT',
      'content-type': 'application/json',
      'content-length': '172',
      connection: 'keep-alive',
      'access-control-allow-origin': '*',
      'openai-version': '2020-10-01',
      'openai-organization': 'user-***************',
      'x-request-id': '6f3b88d1c538d56b102d76d2f1dc6aee',
      'openai-processing-ms': '55',
      'strict-transport-security': 'max-age=15724800; includeSubDomains'
    },

Solution

  • I found out that my code is fine the issue was that my API Key was under a trial license with no available money to spend! I added my card and now I'm receiving 200.