Tokenomics never stores API keys in code, configuration files, or token definitions. Instead, it references secrets by environment variable name. This keeps sensitive credentials secure and makes it easy to rotate keys without changing policies or tokens.
When you create a token, you specify which environment variable contains the API key:
{
"base_key_env": "OPENAI_PAT"
}The policy stores the reference (OPENAI_PAT), not the actual key. When a request comes in, Tokenomics looks up the environment variable at runtime and uses the value.
# Set environment variable with actual API key
export OPENAI_PAT="sk-proj-abc123def456..."
# Create policy that references it by name
tokenomics token create --policy '{"base_key_env":"OPENAI_PAT"}'
# Output: tkn_xyz789...- Policy stores:
"base_key_env": "OPENAI_PAT"(never the actual key) - Token stores: encrypted policy (never sees the key)
- At request time: looks up
$OPENAI_PATfrom environment and uses it
Change the key without updating any policies:
export OPENAI_PAT="sk-proj-new-key-789xyz..."
# All existing tokens automatically use the new keyUse custom names to isolate keys by purpose and make them easier to manage:
# Provider PATs (Personal Access Tokens)
export OPENAI_PAT="sk-proj-..."
export ANTHROPIC_PAT="sk-ant-..."
export AZURE_OPENAI_PAT="..."
# Tokenomics-specific configuration
export TOKENOMICS_HASH_KEY="random-secret-for-token-encryption"
export TOKENOMICS_KEY="tkn_..."
# Optional: webhook secrets
export WEBHOOK_SECRET="shared-secret-for-hmac-validation"Avoid default SDK env var names like OPENAI_API_KEY or ANTHROPIC_API_KEY to keep API keys isolated from standard library lookups.
Tokenomics loads environment variables from multiple sources in this order:
- System environment variables (set via
exportor shell startup files) .tokenomics/.envin the data directory (if it exists).envin current working directory (if it exists)- Process defaults (built-in values)
For local development, create ~/.tokenomics/.env:
# ~/.tokenomics/.env
OPENAI_PAT=sk-proj-dev-key-here
ANTHROPIC_PAT=sk-ant-dev-key-here
TOKENOMICS_HASH_KEY=dev-hash-key-only-for-testingTokenomics will automatically load these when you start the server:
tokenomics serve
# Reads ~/.tokenomics/.env automaticallyAlternatively, create .env in your working directory:
# .env (in project root)
OPENAI_PAT=sk-proj-...
TOKENOMICS_HASH_KEY=secretThen run Tokenomics from that directory:
cd /my/project
tokenomics serve
# Reads .env from current directoryThe .env file uses simple KEY=VALUE format:
# Comments start with #
OPENAI_PAT=sk-proj-abc123
# Values with spaces need quotes
CUSTOM_PROMPT="Be helpful and kind"
# Empty lines are ignored
ANOTHER_KEY=value
# No space around =
NOT_KEY = value # This line is ignored (spaces around =)Important: .env files contain secrets. Always:
-
Add
.envto.gitignoreto prevent accidental commits:echo ".env" >> .gitignore echo ".tokenomics/.env" >> .gitignore
-
Restrict file permissions:
chmod 600 ~/.tokenomics/.env chmod 600 .env -
Never commit
.envto version control -
Use
.env.exampleto document required variables without exposing values:# .env.example OPENAI_PAT=sk-proj-your-key-here ANTHROPIC_PAT=sk-ant-your-key-here TOKENOMICS_HASH_KEY=random-secret-string
Tokenomics supports zero-downtime secret rotation:
- Generate new API key from your provider (OpenAI, Anthropic, etc.)
- Update environment variable:
export OPENAI_PAT="sk-proj-new-key"
- Restart Tokenomics (or send SIGHUP to reload):
kill -HUP $(pgrep tokenomics)
- Verify with a test request
- Revoke old key from provider once confirmed working
No policy or token changes needed.
When using multiple providers, each has its own environment variable:
{
"providers": {
"openai": {
"api_key_env": "OPENAI_PAT"
},
"anthropic": {
"api_key_env": "ANTHROPIC_PAT"
},
"azure": {
"api_key_env": "AZURE_OPENAI_PAT"
}
}
}Each variable is independent:
export OPENAI_PAT="sk-proj-..."
export ANTHROPIC_PAT="sk-ant-..."
export AZURE_OPENAI_PAT="..."If a provider's env var is not set when a request matches that provider, Tokenomics returns an error.
The main Tokenomics server also uses environment variables for configuration:
# Required
TOKENOMICS_HASH_KEY="random-secret-used-to-encrypt-tokens"
# Optional
TOKENOMICS_DIR="~/.tokenomics" # Data directory
TOKENOMICS_LISTEN="0.0.0.0:8080" # Listen address (default)
TOKENOMICS_TLS="true" # Enable TLSOverride config file values:
# config.yaml has:
# listen: "0.0.0.0:8000"
# Override at runtime:
export TOKENOMICS_LISTEN="127.0.0.1:9000"
tokenomics serve
# Binds to 127.0.0.1:9000 insteadIf using webhooks, Tokenomics can sign events with a shared secret:
export WEBHOOK_SECRET="shared-secret-between-tokenomics-and-receiver"Receivers can verify the signature by:
- Receiving the X-Signature header
- Computing HMAC-SHA256(body, WEBHOOK_SECRET)
- Comparing with X-Signature value
# Good - clear purpose
export OPENAI_PAT="..."
export ANTHROPIC_PAT="..."
# Bad - too generic or misleading
export API_KEY="..."
export SECRET="..."- Set a reminder to rotate API keys monthly or quarterly
- Stagger rotations across providers to avoid outages
- Test new keys before revoking old ones
Development:
# .env file (in .gitignore)
OPENAI_PAT=dev-keyProduction:
# Set via secrets management (AWS Secrets Manager, Vault, etc.)
export OPENAI_PAT=$(aws secretsmanager get-secret-value ...)- Log which tokens and policies use which secrets
- Monitor for unusual request patterns
- Set up alerts for rate limit or budget violations
Use different API keys for different environments:
# development
export OPENAI_PAT="sk-proj-dev-..."
# staging
export OPENAI_PAT="sk-proj-stage-..."
# production
export OPENAI_PAT="sk-proj-prod-..."Each has different rate limits, budgets, and quotas.
Error: base_key_env "OPENAI_PAT" is not set
Solution: Set the environment variable:
export OPENAI_PAT="sk-proj-..."
tokenomics serveOr add to .env:
# ~/.tokenomics/.env
OPENAI_PAT=sk-proj-...The environment variable is set, but the value is wrong.
# Verify the variable is set
echo $OPENAI_PAT
# Check if it's a valid key format (starts with sk-proj-)
# Verify the key is active in your provider console
# Make sure you're not using an old/revoked key- Check file location - should be
~/.tokenomics/.envor.envin working directory - Check file permissions - must be readable by Tokenomics process
- Check file format -
KEY=VALUEwith no spaces around= - Check startup logs - Tokenomics logs which files it reads
# Debug: see what Tokenomics loads
export TOKENOMICS_DEBUG=true
tokenomics serveIf both ~/.tokenomics/.env and .env exist, system environment variables take precedence:
System env ($OPENAI_PAT)
↓ (if not set)
~/.tokenomics/.env
↓ (if not found)
.env (in working directory)
↓ (if not found)
Fail with error
- CONFIGURATION.md - Config file reference
- TOKEN_MANAGEMENT.md - Token creation and management
- AGENT_INTEGRATION.md - Connecting agents with tokens