Links
🤫

Secrets

Safely access external systems in your app with secure authentication mechanisms like keys, tokens, and service accounts.
The secret store on the app configuration page
If you're building an app in Databutton that requires secrets like API Keys (to e.g OpenAI or Pinecone) or Service Accounts (to e.g Google Cloud), don't worry! It's easy to store these as secrets and retrieve them to use in your code.
Your secrets are encrypted in transit and at rest. They are also only available to those you have invited as collaborators to your specific data app.

Adding or updating a secret

Updating or adding a new secret can done through our UI by heading over to the configuration page for your data app. You can find that under the 'Configuration' button in the lower left corner.
You can paste both API Keys and JSON directly into the "Value" field!

Using secrets

To retrieve an API Key secret, you can use this code:
import databutton as db
# Reading the data app secrets named 'API_TOKEN'
API_KEY = db.secrets.get("API_KEY")
And for Service Accounts, you can store the JSON as a secret and retrieve it like this:
import databutton as db
import json
# Retrieve the secret as a string
service_account = db.secrets.get('SERVICE_ACCOUNT')
# Convert the string to a dictionary
json_service_account = json.loads(service_account)
Once you've retrieved the secrets, you can use them in your code to authenticate with the relevant service.

An example – Google Sheets

Here's an example of how to authenticate with the Google Sheets API using a Service Account secret:
  1. 1.
    Save your Google Sheets Service Account JSON file as a secret in Databutton. Let's call it GSPREAD_SERVICE_ACCOUNT.
  2. 2.
    Load the secret in your app:
import databutton as db
import json
# Retrieve the secret as a string
secret = db.secrets.get("GSPREAD_SERVICE_ACCOUNT")
# Convert the string to a dictionary
credentials = json.loads(secret)
  1. 3.
    Authenticate with the Google Sheets API:
import gspread
# Authenticate with the Google Sheets API using the credentials
gc = gspread.service_account_from_dict(credentials)
That's it! With this example, you should now be able to authenticate with the Google Sheets API using a Service Account secret.