Changing API Keys on a Server

I need to change my API keys. When I do it on google colab or locally, it works fine. However, when I do so on the server I’ve deployed my app to, it makes no difference, and I get the same issue the old keys caused.

Is there any other file I need to edit to replace my API keys on a Linux server where a GPT Flask app is deployed? I’ve obviously changed it in the main programme file but that hasn’t worked. Thanks!

This all depends on the language, the way you are serving the application, and how you are setting it. There’s no OpenAI-specific issue here. You may need to restart your server.

1 Like

Unless the python app is set to debug mode and to check for file modifications the running app will not reload changed API keys, if you shut the app down and restart it, it will.

3 Likes

Might be a cache problem as well.
Some applications store config in cache and some read them on startup (so restart sounds like a good idea as well).

2 Likes

Thanks. Do you mean kill the service and then restart it? In other words, the service the app is running on, like apache? A sudo reboot of the server didn’t help. Thanks!

Ok, so it all depends how your python app is getting the API keys, following best practice from the Documentation they “should” be stored in the environment variables, usually set from .bashrc, if that’s the case then you need to alter them in there and issue a source ~/.bashrc command to update the environment variables.

It could also be hard coded into the source, not best practice but… it happens, if that’s the case then so long as you have updated the source .py file in the correct folder, and assuming you have a flask app, then you can either kill that task or if it’s running from says a screens “screen” you can just go to that screen and do a cntrl+c to exit the running application, then restart it again.

There are a lot of potential ways your app is being ran, is this on a VPS, a co-lo, a machine in your garage, an Azure deployment env… does it auto start if the server is rebooted? if so what mechanism is being used to start the application, are you sure the application you are editing is the live app? you can test this in a quick and dirty way by just putting “dfjsdkhfgksdhgkds” (i.e. garbage) into the .py file you “think” is running and then see if you get an error when it’s supposed to be running… (you want it to error) now you know you have the right file, remove the garbage and edit the API key.

At this moment in time we need to find out what mechanism you are using to store API keys, I’m assuming you set this up so there should be some set if instructions you followed to get to this point, we need to know those steps to be able to assist further.

1 Like

Hi @ysotech1

Can you provide more detail on the issue?

  1. The API keys are in the source code. Under my GPT functions I have a line openai.api_key =
  2. It’s on an Azure VM
  3. I could try the “dfjsdkhfgksdhgkds” method, but I’m currently getting an error due to the keys. I’m 99% sure this is the correct file, because the other day there was a non-openai-related error which just caused the webapp to refresh when trying to run. I fixed it, and now it gives an error 500 instead.

Should I kill the .py process that contains the keys and then start it up again? If so, how do I find its PID?

Thank you very much.

Assuming you have command line access and you have a debian/ubuntu disty on there and your flask app is utilising port 80 you can run sudo lsof -i :80 to get the PID of anything taking that port, change port number to whatever your application is using.

I’ve got the PID of apache2, when I execute kill 707, or sudo kill 707, I get a “no such process” response. And yeah, ubuntu is on there. Thanks for the help.

ok, maybe that was the wrong approach, I usually have a web server running in my flask app to keep things all tied together, if you don’t have that then you can try ps aux | grep python which should list any python apps that are running, hopefully you will spot yours in that list.

I’ve found 2 proceses that get triggered when I run the app on a browser, one of which comes up with the ps aux | grep python. Should I kill that process? How do I restart it once I’ve done so? Many thanks.

Ok, not knowing your deployment environment, do you know where your application is?

It should update when you reboot the server, and I think you mentioned you have already tried that. What is the actual API key issue you are getting?

Yes, I know where it is. Do I just run that python script in the command line after I’ve killed it? And yeah, I tried sudo reboot, didn’t help.

Ok, so what is the actual error/issue you are facing?

It’s a credit issue. Have changed to keys with credit, but still getting that error.

Do the key with credit have payment methods associated with them?

No, the keys do not have payment methods associated with them. However, I have succesfully used them to run the programme locally and on google colab.

I tried the “gurabgaba9uwgba9” method and still get an OpenAI error. Either the changes aren’t being implemented or it’s using another file.

Are you certain you are connecting to the Azure server when you do your tests? Not some A record that is still pointing at a test environment? It does seem that something has gotten mixed up further back in the chain.

Make a backup of your current test environment files so they are safe. How are you deploying your application to the Azure server? as in, how do you know the file you are editing is actually on the server itself? Are you running a CLI command like nano or vim to edit the file on the machine? or are you editing a file on your local machine and then deploying the modification to the Azure VM?