Retrieve a secret
This recipe retrieves a Databricks secret. Use secrets to securely connect to external services and APIs.
Code snippet
app.py
import streamlit as st
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
def get_secret(scope, key):
try:
secret_response = w.secrets.get_secret(scope=scope, key=key)
decoded_secret = base64.b64decode(secret_response.value).decode('utf-8')
return decoded_secret
except Exception as e:
st.error("Secret not found or inaccessible. Please create a secret scope and key before retrieving.")
scope_name = st.text_input("Secret scope:", "my_secret_scope")
secret_key = st.text_input("Secret key (name):", "api_key")
if st.button("Retrieve"):
secret = get_secret(scope_name, secret_key)
st.success("Secret retrieved! The value is securely handled in the backend.")
Resources
Permissions
Your app service principal needs the following permissions:
CAN READ
on the secret scope
See Manage secret scope permissions for more information.
Dependencies
- Databricks SDK for Python -
databricks-sdk
- Streamlit -
streamlit
requirements.txt
databricks-sdk
streamlit