All jobs

Move API Keys to st.secrets in a Streamlit App

Yes. An agent swaps every sidebar key box for an st.secrets lookup that falls back to the input, adds an example secrets file, and keeps the tests green.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads every page that takes a key. In an app shaped like streamlit/llm-examples, Chatbot.py asks for an OpenAI key with st.text_input("OpenAI API Key", key="chatbot_api_key", type="password") in the sidebar and stops with st.info("Please add your OpenAI API key to continue.") when it is empty. pages/1_File_Q&A.py does the same for an Anthropic key under file_qa_api_key, pages/2_Chat_with_search.py under langchain_search_api_key_openai, pages/5_Chat_with_user_feedback.py under feedback_api_key, and pages/3 and 4 use unkeyed inputs. Six inputs, two providers, and the key typed into a browser tab every session. Page 5 already reads st.secrets.TRUBRICS_EMAIL and TRUBRICS_PASSWORD behind an "TRUBRICS_EMAIL" in st.secrets guard, so the pattern the app should use everywhere is in the repository once.

It reads the tests before changing a line, because they pin the behaviour. app_test.py drives Chatbot.py with AppTest.from_file("Chatbot.py").run(), asserts the info message when no key is set, then sets at.text_input(key="chatbot_api_key") to a dummy value and checks the mocked completion lands in the third chat message. The LangChain quickstart test finds the key box as at.sidebar.text_input[0]. .github/workflows/app-testing.yml runs those through streamlit/streamlit-app-action with ruff on and pytest producing JUnit output, on Python 3.11. Whatever the agent does, an app with no secrets file must still show the input and the info message, or two tests fail.

The change is one helper and six call sites. A small utils/keys.py exposes get_api_key(secret_name, label, widget_key): it returns st.secrets[secret_name] when the secret exists, rendering a short sidebar caption that the key is configured, and otherwise renders the same password input the page had, with the same key, so widget state and the tests keep working. Each page calls it with OPENAI_API_KEY or ANTHROPIC_API_KEY and its own widget key; pages 3 and 4 keep their unkeyed inputs by passing no key. .streamlit/secrets.toml.example lists both names with empty values, a new .gitignore excludes .streamlit/secrets.toml, and the devcontainer's streamlit run Chatbot.py needs nothing more because Streamlit reads the file from the working directory.

Tests close the loop. A new test in app_test.py sets at.secrets["OPENAI_API_KEY"] before run(), asserts no info message appears and no sidebar text input with the chatbot_api_key key is rendered, and checks the mocked completion still arrives. The existing tests run unchanged and pass, because with no secret the helper renders what they look for. The agent runs pytest -v, ruff check . under the 105-column .ruff.toml, and streamlit run Chatbot.py --server.headless true in the sandbox once with an example secrets file in place to see the caption, then removes the file before committing.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Move API keys to st.secrets in this Streamlit app. Read Chatbot.py, every file under pages/, app_test.py, requirements.txt, requirements-dev.txt, .ruff.toml, .pre-commit-config.yaml, .devcontainer/devcontainer.json and .github/workflows/app-testing.yml first, and list every text_input that takes a key, its widget key and which provider it is for; note any place st.secrets is already used. Write one helper that returns the secret when it is set, showing a sidebar caption instead of the input, and otherwise renders the same password input with the same widget key as before. Use it in every page. Add .streamlit/secrets.toml.example with the secret names and a .gitignore that excludes the real secrets file. Add a test that sets at.secrets before run and asserts the input is gone and the mocked completion still works, and keep the existing tests passing unchanged. Run pytest -v and ruff check in the sandbox, and start the app headless once with a temporary secrets file to confirm the caption renders, removing the file before you commit. Open a pull request that lists each page changed, the secret names, and the test and lint output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request on its own branch that adds utils/keys.py, .streamlit/secrets.toml.example and .gitignore, changes Chatbot.py and the five files under pages/ to call the helper, and extends app_test.py with one test. requirements.txt is unchanged, because st.secrets ships with Streamlit, and the workflow is unchanged because it already runs pytest and ruff.

The description lists each page with its old widget key and the secret name it now reads, quotes pytest -v showing the original tests and the new one passing with the OpenAI mock, and the ruff check result. It states that no real secrets file is committed and shows the two-line example file.

Before merging, a reviewer checks that every page still shows the input when the secret is absent, that the caption does not print any part of the key, and that the secret names match what the team's Streamlit Community Cloud or container settings will provide. Adding a per-page model selector or reading the key from an environment variable as a third fallback is a follow-up on the same session.

Run it on every app ready to deploy

This is a change to start from the terminal and read later: one POST /o/{org}/sessions with this prompt and the repository in resources. The run reads the pages, writes the helper, runs the tests and opens the pull request while nobody watches. The sandbox parks with its disk when the turn ends, so a follow-up such as also read the key from an environment variable when there is no secrets file wakes it with the virtualenv and the branch intact.

A team with several internal Streamlit tools sends the same body once per repository, one run each, and each run finds that app's own inputs and widget keys. Give the API key a webhook_url and Rush posts run.completed or run.failed with the session_url when each turn ends, so the pull request links reach one channel. Send a request_id so a retried POST returns the run it already started.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

A secrets change is easy to get almost right: one page still reads the input, a caption leaks the first characters of a key, a test that set the input now finds nothing to set. The way to know is to run the AppTest suite with and without a secrets file and to start the app once each way. The agent does both in its own sandbox before the pull request exists, so the diff arrives with the suite's output instead of a request to try it locally.

The record keeps the reasoning. The transcript shows the agent finding the existing st.secrets guard in page 5 and copying its shape, the test it wrote for the secret path, and the headless start with and without the file. A reviewer who wants environment variables as a further fallback asks in a follow-up and the parked sandbox makes the change with the tests already in place.

Frequently asked questions

Will the app still work for someone who has no secrets file?

Yes. When the secret is missing the helper renders the same password input with the same widget key the page used before, and the existing info message appears when it is empty. That is why the original tests pass without modification.

Where does the secrets file live and is it committed?

Streamlit reads .streamlit/secrets.toml from the working directory. The pull request commits only .streamlit/secrets.toml.example with empty values and a .gitignore entry for the real file, which the repository did not have before.

How is the secret path tested without a real key?

AppTest lets a test assign at.secrets before run, so the new test sets OPENAI_API_KEY to a dummy value, mocks the OpenAI completion the way the existing test does, and asserts that no key input is rendered and the reply still appears.

Does this change requirements.txt or the CI workflow?

No. st.secrets is part of Streamlit, and the workflow already runs ruff and pytest through streamlit-app-action, which picks up the new test automatically.

What about the Trubrics credentials page 5 already reads from secrets?

They stay as they are. That page was already using st.secrets with an in-secrets guard, and the helper follows the same pattern for the API keys; the pull request points at that existing code as the precedent.

More Streamlit jobs

More jobs like this: Hand off a long task

Keep reading