Os.environ values in streamlit cloud

I’m deploying a chat app that uses OpenAI API on Streamlit Cloud where the user has the supply an api key to use it. At the moment, I save the passed api key as

os.environ['API_KEY'] = st.text_input("api key")

and use os.environ['API_KEY'] wherever an api key is needed. However, I noticed that when deployed on Streamlit Cloud, this variable is set to the first user’s and subsequent users keep using the first user’s API key.

That leads to my question, does a deployed app share the same instance for everyone who uses it or is it different instances for everyone who opens a link to the app page? I guess a more on-point quesiton is, are the os.environ values gonna be different for everyone who uses the app or is it the same for everyone who uses this app?

1 Like

Streamlit app is run on the server. The os.environ is global to this server so all users of the app has a single os.environ.

To avoid this mistake, store the key in session state. Each user has its own session state.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.