Overview

Environment variables in Sample Workflows provide a secure way to manage configuration values, API keys, and other sensitive data that your workflow needs to access. These variables are injected at deploy time and are only available within your workflow code.

How Environment Variables Work

Deployment-Time Injection

Environment variables are injected into your workflow when it’s deployed to the Sample platform. This means:

  • They become available to your workflow code after deployment
  • Changes to environment variables require a new deployment to take effect

Accessing Environment Variables

In your workflow code, you can access environment variables using Python’s built-in os.getenv() function:

import os
from workflows_py.workflow import Workflow, Step

def process_with_api_key(ctx):
    # Access environment variables
    api_key = os.getenv("API_KEY")
    database_url = os.getenv("DATABASE_URL")
    debug_mode = os.getenv("DEBUG_MODE", "false")  # with default value

    if not api_key:
        raise ValueError("API_KEY environment variable is required")

    # Use the environment variables in your logic
    response = make_api_call(api_key, database_url)
    return {"result": response}

workflow = Workflow()
workflow.then(
    Step("api-call", process_with_api_key)
)

Best Practices

Security Considerations

  1. Never hardcode sensitive values in your workflow code
  2. Use environment variables for API keys, tokens, and connection strings
  3. Set appropriate access controls for who can view and modify environment variables

Naming Conventions

  1. Use UPPERCASE with underscores for environment variable names
  2. Be descriptive but concise: DATABASE_URL, STRIPE_API_KEY
  3. Group related variables with prefixes: AWS_ACCESS_KEY, AWS_SECRET_KEY

Default Values

Always provide sensible defaults when possible:

# Good: Provides a default value
debug_mode = os.getenv("DEBUG_MODE", "false")
timeout = int(os.getenv("API_TIMEOUT", "30"))

# Better: Handle missing required variables gracefully
api_key = os.getenv("API_KEY")
if not api_key:
    raise ValueError("API_KEY environment variable is required")

Common Use Cases

API Integration

def call_external_api(ctx):
    api_key = os.getenv("EXTERNAL_API_KEY")
    base_url = os.getenv("EXTERNAL_API_URL", "https://api.example.com")

    headers = {"Authorization": f"Bearer {api_key}"}
    # Make API calls using the configured values

Database Connections

def connect_to_database(ctx):
    db_url = os.getenv("DATABASE_URL")
    db_timeout = int(os.getenv("DB_TIMEOUT", "10"))

    # Use connection parameters
    connection = create_connection(db_url, timeout=db_timeout)

Feature Flags

def conditional_processing(ctx):
    feature_enabled = os.getenv("FEATURE_X_ENABLED", "false").lower() == "true"

    if feature_enabled:
        # Execute new feature logic
        return process_with_new_feature(ctx)
    else:
        # Execute legacy logic
        return process_with_legacy_feature(ctx)

This allows your workflow code to remain the same across environments while adapting to different configurations.

Important Notes

  • Environment variables are only available in workflow code, not in screen components
  • Changes to environment variables require redeployment to take effect
  • Environment variables are scoped to the specific workflow and environment
  • Use the Sample platform’s environment variable management interface to set and update values securely