API requests work only in my local environment

Hi guys, I have a little problem using the OpenAI API.

When I run my app locally (localhost:5000) the API requests work fine. But when I do that on my app deployed online it doesn’t work and I get the ERROR 401.

I tried regenerating API KEYS more than one time, all data inside the code and the .env file is correct, but I don’t understand why this works only in my local environment.

The code is the same as the Documentation Example.

Do you have any suggestions?

Thanks in advance! :pray:t2:

Error Code 401 - Invalid Authentication | OpenAI Help Center Have you had a look at this ? Maybe this will help you troubleshoot ?

Feels like you’re not providing the api key. How are you including it locally? How does it change in your Sever?

Usually .env files aren’t uploaded when deploying. You should check how you can add an enviroment variable to your hosting service.

1 Like

@piotarboa I’m experiencing the same thing now. What ended up solving the issue for you? All env vars are represented in a parameter store for my deployed app

401 errors almost always mean missing or invalid API Key. Double, triple, quadruple check your key is properly set in your deployed app. Also look at the full response of the API request and it will include an error code and message with more details.

Yep. It turned out to be a race condition for dotenv to execute the config method. Moving some things around gave time for the env vars to get passed around. Thanks for the quick response.

1 Like