# I am getting an error in this code. what is wrong. It logs the messages in the console. message is being sent corectly from the frontend. api is throwing error

**URL:** https://community.openai.com/t/i-am-getting-an-error-in-this-code-what-is-wrong-it-logs-the-messages-in-the-console-message-is-being-sent-corectly-from-the-frontend-api-is-throwing-error/729279
**Category:** API
**Tags:** api
**Created:** [April 27, 2024, 8:56am UTC](https://community.openai.com/t/i-am-getting-an-error-in-this-code-what-is-wrong-it-logs-the-messages-in-the-console-message-is-being-sent-corectly-from-the-frontend-api-is-throwing-error/729279 "2024-04-27T08:56:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![code7](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/code7/32/354726_2.png) [@code7](https://community.openai.com/u/code7)
#### Post date: [April 27, 2024, 8:56am UTC](https://community.openai.com/t/i-am-getting-an-error-in-this-code-what-is-wrong-it-logs-the-messages-in-the-console-message-is-being-sent-corectly-from-the-frontend-api-is-throwing-error/729279/1 "2024-04-27T08:56:11Z")

</div>

import { NextResponse } from “next/server”;  
import { auth } from “@clerk/nextjs”;  
import { OpenAI } from “openai”;  
const openai = new OpenAI({ apiKey: process.env.OPENAI\_API\_KEY });

export async function POST(req: Request) {  
try {  
console.log(“\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*”);  
const { userId } = auth();  
const body = await req.json();  
const { messages } = body;  
if (!userId) {  
return new NextResponse(“Unauthorized”, { status: 401 });  
}  
console.log(“iddddddddddddddd”);

```
console.log("keyyyyyyyyyyyy");
console.log(messages);

if (!messages)
  return new NextResponse("Messages are required", { status: 400 });

console.log("mesageeesssssssssssssssss");
const response = await openai.chat.completions.create({
  messages,
  model: "gpt-3.5-turbo",
});
console.log(response.choices[0].message.content);

return NextResponse.json(response.choices[0].message.content);

```

} catch (error) {  
console.error(“[CONVERSATION\_ERROR]”,error);  
return new NextResponse(“Internal error”, { status: 500 });  
}  
}

---

<div class="post-metadata">

### Author: ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/719663_2.png) [@\_j](https://community.openai.com/u/_j)
#### Post date: [April 27, 2024, 10:04am UTC](https://community.openai.com/t/i-am-getting-an-error-in-this-code-what-is-wrong-it-logs-the-messages-in-the-console-message-is-being-sent-corectly-from-the-frontend-api-is-throwing-error/729279/2 "2024-04-27T10:04:05Z")

</div>

> [@code7](#):
>
> ```auto
> const response = await openai.chat.completions.create({
> messages,
> model: "gpt-3.5-turbo",
> });
> 
> ```

Above, the you are missing the actual parameter value for messages.

It should be specified like the model (but with your variable with the json array object of messages instead of the model name as a string.) It is not a positional parameter.

---

<div class="post-metadata">

### Author: ![ramn7](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ramn7/32/195226_2.png) [@ramn7](https://community.openai.com/u/ramn7)
#### Post date: [April 27, 2024, 8:47pm UTC](https://community.openai.com/t/i-am-getting-an-error-in-this-code-what-is-wrong-it-logs-the-messages-in-the-console-message-is-being-sent-corectly-from-the-frontend-api-is-throwing-error/729279/3 "2024-04-27T20:47:50Z")

</div>

Try to see what’s in messages it should look like [here](https://platform.openai.com/docs/guides/text-generation/chat-completions-api) (an array).  
If it’s indeed correct it could be useful to see what the error is saying.

---

<div class="post-metadata">

### Author: ![code7](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/code7/32/354726_2.png) [@code7](https://community.openai.com/u/code7)
#### Post date: [May 1, 2024, 6:11pm UTC](https://community.openai.com/t/i-am-getting-an-error-in-this-code-what-is-wrong-it-logs-the-messages-in-the-console-message-is-being-sent-corectly-from-the-frontend-api-is-throwing-error/729279/4 "2024-05-01T18:11:06Z")

</div>

In my open ai api i am in free tier,and today i am trying to make the request after one day , but still it says 429 error, RateLimitError: 429 You exceeded your current quota, please check your plan and billing details. I am trying for the first time for today

---

<div class="post-metadata">

### Author: ![ramn7](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ramn7/32/195226_2.png) [@ramn7](https://community.openai.com/u/ramn7)
#### Post date: [May 1, 2024, 7:16pm UTC](https://community.openai.com/t/i-am-getting-an-error-in-this-code-what-is-wrong-it-logs-the-messages-in-the-console-message-is-being-sent-corectly-from-the-frontend-api-is-throwing-error/729279/5 "2024-05-01T19:16:21Z")

</div>

Hey,

You should probably check your plan and billing details as suggested.

Here is a related post that might provide some help too: [429 Error : insufficient\_quota](https://community.openai.com/t/429-error-insufficient-quota/492350)
