Key storage options
Inline keys (development only)
Store keys directly in your Traffic Policy:ngrok Vaults & Secrets (recommended)
Store keys in ngrok Vaults & Secrets—they’re encrypted at rest, never visible in your Traffic Policy, and can be rotated without redeploying:Creating secrets
Using the CLI
Using the dashboard
- Navigate to Vaults & Secrets in the ngrok Dashboard
- Create a vault if you don’t have one (for example,
ai-keys) - Create a secret in the vault (for example,
openai) - Add your provider API keys as key-value pairs
Multiple keys for failover
Configure multiple provider API keys for automatic failover when keys hit rate limits or fail:- Gateway tries the first key
- If it fails (rate limit, quota exceeded, error), tries the next key
- Continues until a key succeeds or all keys are exhausted
Key rotation
To rotate provider API keys without downtime:1
Add the new key
2
Deploy and monitor
Deploy the updated Traffic Policy. Monitor traffic to ensure the new key works correctly.
3
Remove the old key
Once confirmed, remove the old key:
4
Revoke the old key
Revoke the old key with your provider (OpenAI, Anthropic, etc.) to complete the rotation.
Multi-provider example
- 3 failover keys for OpenAI
- 2 failover keys for Anthropic
- 1 key for Google
- Automatic key rotation on failures
Intelligent key selection
For advanced control over which API key is used, configureapi_key_selection with CEL expressions. This enables intelligent key rotation based on runtime metrics like quota usage and error rates.
Basic configuration
How it works
Strategies execute in order until one returns at least one key:- First strategy filters keys with >100 remaining requests
- If no keys match, falls back to all keys
- Selected keys are then tried in order for failover
Quota-based selection
Prioritize keys with remaining capacity:Error rate-based selection
Avoid keys experiencing issues:Load distribution
Randomize key selection to distribute load:Available key variables
| Variable | Description |
|---|---|
k.quota.remaining_requests | Requests remaining before rate limit |
k.quota.remaining_tokens | Tokens remaining before rate limit |
k.error_rate.total | Fraction of all errors (0.0 to 1.0) |
k.error_rate.rate_limit | Fraction of rate limit (429) errors |
k.error_rate.timeout | Fraction of timeout errors |
Security best practices
- Add authorization to your gateway when using server-side keys—see Securing Your Gateway
- Never commit provider API keys to version control
- Use ngrok secrets for all production keys
- Rotate keys regularly to minimize exposure risk
- Monitor key usage to detect anomalies
- Use different keys for different environments (dev, staging, prod)
- Set up alerts for rate limit errors to proactively add capacity
Passthrough mode
If you don’t configure any keys for a provider, the gateway forwards whatever key your SDK sends:- Development environments where each developer uses their own key
- Applications that already manage their own keys
- Quick testing without configuration changes
Next steps
- Securing Your Gateway - Add authorization when using server-side keys
- Provider API Keys Concept - Understanding key precedence
- Multi-Key Failover - Example configurations with intelligent selection
- Configuring Providers - Full provider setup
- CEL Functions Reference - Complete API key selection reference