Safely access external systems in your app with secure authentication mechanisms like keys, tokens, and service accounts. AI Agents can perform all these task!
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 app.
The Databutton AI agent is fully capable of handling such tasks. Simply describe your requirements during the building process!
Remember, the Databutton Agent can access the list of available secrets, so if you're unsure or need to double-check, just ask them!
Adding or updating a secret manually
Updating or adding a new secret can done through our UI by heading over to the configuration page for your 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!
Code Usage Examples
Using secrets
To retrieve an API Key secret, you can use this code:
import databutton as db# Reading the 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 dbimport json# Retrieve the secret as a stringservice_account = db.secrets.get('SERVICE_ACCOUNT')# Convert the string to a dictionaryjson_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:
Save your Google Sheets Service Account JSON file as a secret in Databutton. Let's call it GSPREAD_SERVICE_ACCOUNT.
Load the secret in your app:
import databutton as dbimport json# Retrieve the secret as a stringsecret = db.secrets.get("GSPREAD_SERVICE_ACCOUNT")# Convert the string to a dictionarycredentials = json.loads(secret)
Authenticate with the Google Sheets API:
import gspread# Authenticate with the Google Sheets API using the credentialsgc = 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.