401 Incorrect API key provided. Even though the key is correct

Im building a AI route planning tool. I’m getting a API erorr. Look:

import { NextResponse } from ‘next/server’;
import OpenAI from ‘openai’;
import ‘dotenv/config’

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: “api openai v1 (you can’t send links in the forum)”,
defaultHeaders: {
‘Authorization’: Bearer ${process.env.OPENAI_API_KEY},
},
});

export async function POST(req: Request) {
if (!process.env.OPENAI_API_KEY) {
console.error(‘OpenAI API key is missing’);
return NextResponse.json(
{ error: ‘OpenAI API key is not configured’ },
{ status: 500 }
);
}

try {
const { prompt } = await req.json();
console.log(‘Attempting to use OpenAI with prompt:’, prompt);

if (!prompt) {
  return NextResponse.json(
    { error: 'Prompt is required' },
    { status: 400 }
  );
}

try {
  const completion = await openai.chat.completions.create({
    model: "gpt-3.5-turbo",
    messages: [
      {
        role: "system",
        content: "You are a helpful travel route planner. Provide detailed travel routes and itineraries."
      },
      {
        role: "user",
        content: prompt
      }
    ],
    temperature: 0.7,
    max_tokens: 500
  });

  console.log('OpenAI response received:', completion.choices[0].message);

  return NextResponse.json({
    content: completion.choices[0].message.content,
    status: 200
  });

} catch (openaiError: any) {
  console.error('Detailed OpenAI Error:', {
    message: openaiError.message,
    type: openaiError.type,
    status: openaiError.status,
    headers: openaiError.headers,
  });
  
  return NextResponse.json(
    { error: `OpenAI API Error: ${openaiError.message}` },
    { status: openaiError.status || 500 }
  );
}

} catch (error: any) {
console.error(‘Request processing error:’, error);
return NextResponse.json(
{ error: Error processing your request: ${error.message} },
{ status: 500 }
);
}
}

Detailed OpenAI Error: {
message: ‘401 Incorrect API key provided: [api-key] You can find your API key at the openai web. (you can’t include links here at the community of openai)’,
type: ‘invalid_request_error’,
status: 401,
headers: {
‘alt-svc’: ‘h3=“:443”; ma=86400’,
‘cf-cache-status’: ‘DYNAMIC’,
‘cf-ray’: ‘8e356e9ffbab6e90-BTS’,
connection: ‘keep-alive’,
‘content-length’: ‘414’,
‘content-type’: ‘application/json; charset=utf-8’,
date: ‘Sat, 16 Nov 2024 06:30:24 GMT’,
server: ‘cloudflare’,
‘set-cookie’: ‘__cf_bm=jxOhIFxoEDrj7U9467NgBVs5YrGe2zjl7.nw4dCA5BA-1731738624-1.0.1.1-i0GMuXDbbu88BTJqrTtkqwX7Pe7n3zBT6n166xWhr1lAGKpZeywOk1PKRugRv3YwfTkYQpMHNLg9xqmXQRiBCA; path=/; expires=Sat, 16-Nov-24 07:00:24 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None, _cfuvid=r10VWUbr5P9bC.zJoOrMIUrhOE5ql4N9ZN6qKU6aEpo-1731738624584-0.0.1.1-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None’,
‘strict-transport-security’: ‘max-age=31536000; includeSubDomains; preload’,
vary: ‘Origin’,
‘x-content-type-options’: ‘nosniff’,
‘x-request-id’: ‘req_6f030715c35e3192178282f045a156c6’
}
}

I have funds in the account, and the same account used to work. Any help would be greatly appreciated.
Thanks a lot.