Search code examples
next.jsopenai-apinextjs-api-router

NextJS with "openai api" got 401 error at endpoint createChatCompletion() but 200 ok at createCompletion()


In short

NextJS api router failed to call openai api endpoint createChatCompletion() while pure Node.js code can succeed.

Detail

I use NextJS 's api router as coded here to call "openai api" endpoints using Node.js package openai

I got 401 at calling endpoint createChatCompletion() view code - though succeeded 200 with createCompletion() view code

enter image description here

I double check with pure Node.js code, and can succeed calling createChatCompletion(), strange...

What am I missing?

p.s.

My search in Stack Overflow and Google search result with no helpful found


Solution

  • As far as I know, nextjs define env in next.config.js

    Sample

      // ... other nextjs config
      env: {
        APIAPP_HOST: 'http://yourapihost', 
        // ...
      }