Documentation

Environment Variables

Configure the SDK using environment variables for secure and flexible deployments

Best Practice

Using environment variables for configuration keeps sensitive data like API keys out of your code and allows different settings for development, staging, and production environments.

Using from_env() Factory Method

The recommended way to use environment variables is with the from_env() factory method

python
from scopix import Scopix, SyncScopix
# Async client - loads all config from environment variables
async with Scopix.from_env() as client:
result = await client.files.upload("photo.jpg")
# Sync client - same pattern
with SyncScopix.from_env() as client:
result = client.files.upload("photo.jpg")
# Override specific values while using env vars for the rest
async with Scopix.from_env(timeout=60.0) as client:
result = await client.files.upload("photo.jpg")

Environment Variables Reference

All supported environment variables

VariableTypeDefaultDescription
SCOPIX_API_KEYstringRequiredYour Scopix API key
SCOPIX_BASE_URLstringProduction APIAPI base URL
SCOPIX_TIMEOUTfloat300.0Request timeout in seconds
SCOPIX_MAX_RETRIESint3Maximum retry attempts
SCOPIX_RETRY_DELAYfloat1.0Initial retry delay (exponential backoff)
SCOPIX_POLLING_INTERVALfloat2.0Interval between status polls
SCOPIX_POLLING_TIMEOUTfloat360.0Maximum polling wait time
SCOPIX_TENANT_IDstringNoneTenant ID for multi-tenant deployments
SCOPIX_PROXY_URLstringNoneProxy URL (http or https)
SCOPIX_ENABLE_TRACINGboolfalseEnable OpenTelemetry tracing. Accepted values: true, 1, yes, on (case-insensitive)

Setting Environment Variables

Set variables in your shell or system environment

bash
# Linux / macOS
export SCOPIX_API_KEY="scopix_your_api_key_here"
export SCOPIX_TIMEOUT="60.0"
# Windows PowerShell
$env:SCOPIX_API_KEY = "scopix_your_api_key_here"
$env:SCOPIX_TIMEOUT = "60.0"

Using .env Files

Install python-dotenv

For local development, use a .env file with the optional dotenv extra

bash
# Install SDK with dotenv support
pip install "scopix[dotenv]" --extra-index-url https://scopix:YOUR_API_KEY@api.scopix.ai/api/v2/sdk/simple/

Create .env File

Create a .env file in your project root

bash
# .env file
SCOPIX_API_KEY=scopix_your_api_key_here
SCOPIX_TIMEOUT=60.0
SCOPIX_MAX_RETRIES=5
SCOPIX_RETRY_DELAY=0.5
SCOPIX_POLLING_INTERVAL=3.0
SCOPIX_POLLING_TIMEOUT=180.0

Load .env in Your Code

Use the load_dotenv() helper before creating the client

python
from scopix import load_dotenv, Scopix
# Load .env file first
load_dotenv()
# Now from_env() picks up values from .env
async with Scopix.from_env() as client:
result = await client.files.upload("photo.jpg")
print(result.description)

Custom .env Path

Specify a custom path to your .env file

python
from pathlib import Path
from scopix import load_dotenv, Scopix
# Load from a specific path (string or Path)
load_dotenv("/path/to/config/.env")
load_dotenv(Path("config") / ".env")
# Override existing values (useful for testing)
load_dotenv(".env.test", override=True)
async with Scopix.from_env() as client:
result = await client.files.upload("photo.jpg")

Security Warning

Never commit .env files containing API keys to version control. Add .env to your .gitignore file.

Priority Order

When resolving configuration values, the SDK uses this priority

  1. Explicit parameters passed to from_env()
  2. Environment variables (from OS or .env file)
  3. Default values defined in the SDK
python
# SCOPIX_TIMEOUT=120.0 is set in environment
# This uses timeout=60.0 (explicit parameter wins)
async with Scopix.from_env(timeout=60.0) as client:
...
# This uses timeout=120.0 (from environment)
async with Scopix.from_env() as client:
...

Accessing Constants in Code

Environment variable names are exported as constants

python
from scopix import (
ENV_API_KEY,
ENV_BASE_URL,
ENV_TIMEOUT,
ENV_MAX_RETRIES,
ENV_RETRY_DELAY,
ENV_POLLING_INTERVAL,
ENV_POLLING_TIMEOUT,
ENV_TENANT_ID,
ENV_PROXY_URL,
ENV_ENABLE_TRACING,
)
import os
# Check if API key is configured
if not os.environ.get(ENV_API_KEY):
print(f"Please set {ENV_API_KEY} environment variable")