Skip to main content

Using Secrets in Your Queries and Code

Secrets in Livedocs allow you to securely store sensitive information, such as API keys and database credentials, and use them directly in your workflows without exposing them in your documents. This guide focuses on how to access and use secrets within a Python element, demonstrating their practical applications.

Accessing Secrets in Python Code

To retrieve a secret value in a Python element, use the following syntax:

livedocs.secrets("<Secret-Name>")

This method securely fetches the value of the secret identified by <Secret-Name>. The secret value is never displayed directly in the document, ensuring your sensitive information remains protected.

You can also specify a default value in case the secret is unavailable:

livedocs.secrets("<Secret-Name>", "default_value")

This feature ensures your workflows can still run with a fallback value when a secret is missing.

Practical Example: Using Secrets to Construct an API Request

In this example, we will demonstrate how to retrieve a secret (API key) and incorporate it into an API request URL. This approach ensures that sensitive information remains secure while constructing requests dynamically.

Step 1: Add an API Key as a Secret

  1. Navigate to the Secrets tab.
  2. Add a new secret with the following details:
  • Name: API_KEY
  • Value: your_api_key_here (replace with a valid API key from a public API service).
  1. Click Save secrets.

Step 2: Use the Secret to Construct the API Request

  1. Add a Python element to your document.

  2. Enter the following code:

    # Retrieve the API key from secrets
    api_key = livedocs.secrets("API_KEY")

    # Construct the API request URL
    base_url = "https://api.example.com/data?"
    parameters = f"api_key={api_key}&query=something"

    # Display the constructed API request URL
    print(f"Constructed API Request URL: {base_url}{parameters}")
  3. Click Run to execute the code and see the result.

The result is a dynamically constructed URL that incorporates your securely retrieved API key. It will look like this (with your API key included):

Constructed API Request URL: https://api.example.com/data?api_key=your_api_key_here&query=something

This constructed URL can be used in API calls or as part of a larger workflow, showcasing how securely retrieved secrets integrate seamlessly into your code.

With this guide, you have seen how secrets can be retrieved and integrated securely into Python elements. As you build more complex workflows, continue leveraging secrets to maintain the integrity and security of your sensitive information.

logo
© 2024-25 Livedocs Inc.
Privacy Policy
logo
logo