Vault (SDK)

Fetch secrets from TrailAI's encrypted vault at runtime, optionally loading them straight into your process environment.

Store secrets first: Vault (dashboard).


The API

trail.get_secrets(
    key=None,
    tags=None,
    should_set_env=None,
) -> dict | None
Argument Purpose
key Fetch one secret by name
tags Fetch every secret carrying these tags
should_set_env True writes fetched values into os.environ under their vault names

Basic usage

import trail

trail.init(
    application_name="support-bot",
    url=TRAIL_URL, public_key=TRAIL_PUBLIC_KEY, secret_key=TRAIL_SECRET_KEY,
)

# One secret, into the environment
trail.get_secrets(key="OPENAI_API_KEY", should_set_env=True)

# All secrets tagged "llm-provider"
trail.get_secrets(tags=["llm-provider"], should_set_env=True)

# Read without touching the environment
secrets = trail.get_secrets(key="INTERNAL_API_TOKEN")

The should_set_env pattern

This is the reason the vault is convenient. Name a vault secret exactly as the environment variable it should become, and provider SDKs pick it up with no further wiring:

import trail
from openai import OpenAI

trail.init(application_name="support-bot", url=..., public_key=..., secret_key=...)

trail.get_secrets(tags=["llm-provider"], should_set_env=True)
# os.environ now has OPENAI_API_KEY, ANTHROPIC_API_KEY, …

client = OpenAI()   # reads OPENAI_API_KEY from the environment automatically

Order matters: fetch before constructing clients that read their key at construction time.


⚠️ It returns None on failure

Like get_prompt(), get_secrets() logs and returns None rather than raising. Check the result on any critical path:

result = trail.get_secrets(key="OPENAI_API_KEY", should_set_env=True)

if result is None:
    logger.error("Vault fetch failed; falling back to environment configuration")
    if "OPENAI_API_KEY" not in os.environ:
        raise RuntimeError("No OpenAI credentials available")

Without that check, a vault outage becomes an authentication error at your first model call — much harder to diagnose than a clear startup failure.

It does raise ValueError: make sure trail.init is defined when credentials weren't passed as trail.init() arguments — why.


Tags

Tags let one call fetch a group:

trail.get_secrets(tags=["production", "llm-provider"], should_set_env=True)

Useful schemes:

Scheme Example tags Fetch pattern
By purpose llm-provider, vector-db, internal-api Fetch what a subsystem needs
By environment production, staging Fetch per deployment
By service openai, pinecone Fetch a single provider's set

Tag consistently from day one; retrofitting across dozens of secrets is tedious.


Startup patterns

Simple

import trail

trail.init(application_name="support-bot", url=..., public_key=..., secret_key=...)

if trail.get_secrets(tags=["llm-provider"], should_set_env=True) is None:
    raise RuntimeError("Could not load provider credentials from vault")

from openai import OpenAI
client = OpenAI()

With a fallback to local environment

Convenient for development, where you may not want to depend on TrailAI being reachable:

import os, logging, trail

logger = logging.getLogger(__name__)

REQUIRED = ["OPENAI_API_KEY", "PINECONE_API_KEY"]


def load_secrets():
    if trail.get_secrets(tags=["llm-provider"], should_set_env=True) is None:
        logger.warning("Vault unavailable; relying on local environment")

    missing = [k for k in REQUIRED if not os.environ.get(k)]
    if missing:
        raise RuntimeError(f"Missing credentials: {', '.join(missing)}")


load_secrets()

Periodic refresh

Applications that run for weeks pick up rotations without a restart:

import schedule, threading, time

def refresh():
    trail.get_secrets(tags=["llm-provider"], should_set_env=True)

schedule.every(30).minutes.do(refresh)

def _run():
    while True:
        schedule.run_pending()
        time.sleep(1)

threading.Thread(target=_run, daemon=True).start()

Note that a client constructed with a key captured at construction won't see the new value. Reconstruct the client after refreshing, or use a client that reads the environment per call.


Bootstrap: the secrets you still need locally

The vault can't hold TrailAI's own credentials — you need them to reach the vault:

# Must come from your environment or platform secret manager
TRAIL_URL=...
TRAIL_PUBLIC_KEY=...
TRAIL_SECRET_KEY=...

Everything else can live in the vault. You go from a dozen secrets in every environment to three.


Security notes

  • Secrets live in memory once fetched. Standard process-memory considerations apply.
  • should_set_env=True makes them visible to anything that can read os.environ — including crash dumps and some error reporters. If that matters, fetch without it and pass values explicitly.
  • Never log a fetched value. Log the name if you must:
    logger.info("Loaded secret: %s", key)          # ✅
    logger.info("Loaded secret: %s", secret_value) # ❌
    
  • Vault access follows project access. Anyone who can see the project can read its vault.
  • Access is authenticated with the project's API key pair, so a leaked TrailAI secret key exposes the vault. Rotate promptly — API Keys.

Related