# Error while loading API key

**URL:** <https://discuss.streamlit.io/t/error-while-loading-api-key/40529>\
**Category:** Community Cloud\
**Tags:** macos, streamlit-cloud\
**Created:** [April 1, 2023, 7:31pm UTC](https://discuss.streamlit.io/t/error-while-loading-api-key/40529 "2023-04-01T19:31:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikhiljha97](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/nikhiljha97/32/24968_2.png) [@nikhiljha97](https://discuss.streamlit.io/u/nikhiljha97)\
**Post date:** [April 1, 2023, 7:31pm UTC](https://discuss.streamlit.io/t/error-while-loading-api-key/40529/1 "2023-04-01T19:31:16Z")

</div>

I am trying to load the OpenAI API\_KEY into my main.py script (streamlit application) and while everything runs smoothly when run from local, but as soon as I host my app on streamlit cloud, I get an error  
`**KeyError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you're on Streamlit Cloud, click on 'Manage app' in the lower right of your app).**  **Traceback:**  **File "/home/appuser/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script** **exec(code, module. __dict__ )**  **File "/app/cannabis-recommendation-using-ml/main.py", line 44, in <module>** **openai.api_key=os.environ['API_KEY']**  **File "/usr/local/lib/python3.10/os.py", line 680, in __getitem__ ** **raise KeyError(key) from None**`

I am storing my OpenAI API\_KEY in a .env file which has already been pushed to the gitlab(but couldnt be discovered due to .gitignore). Here is my snapshot of my .env file , .gitignore file and the main.py.

 ![Screenshot-2](https://global.discourse-cdn.com/streamlit/original/3X/e/6/e69a210c4b0eb076e0a11f559284e2ec1e27b5b2.png)  
 ![Screenshot-3](https://global.discourse-cdn.com/streamlit/original/3X/d/9/d95c0c944f22eaa91e27d7c8a874f6dfbdda6497.png)  
 ![Screenshot-4](https://global.discourse-cdn.com/streamlit/original/3X/8/d/8dab6fb23b54f1b918f04d587f174d6682efd464.png)  
 ![Screenshot](https://global.discourse-cdn.com/streamlit/original/3X/7/8/78d77b652af85e8544c5562a9c2346f91474cfe3.png)

---

<div class="post-metadata">

**Author:** ![Franky1](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/franky1/32/6509_2.png) [@Franky1](https://discuss.streamlit.io/u/Franky1)\
**Post date:** [April 1, 2023, 7:38pm UTC](https://discuss.streamlit.io/t/error-while-loading-api-key/40529/2 "2023-04-01T19:38:19Z")

</div>

It can’t work that way either and that’s the wrong approach. How should Streamlit Cloud get hold of the `.env` file if its not in the repo??? Here is the correct approach:

> **[Secrets management - Streamlit Docs](https://docs.streamlit.io/streamlit-community-cloud/get-started/deploy-an-app/connect-to-data-sources/secrets-management)**

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [March 31, 2024, 7:39pm UTC](https://discuss.streamlit.io/t/error-while-loading-api-key/40529/3 "2024-03-31T19:39:08Z")

</div>

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