Key precedence
The gateway determines which provider API key to use based on these rules:1. Gateway-managed keys
Store provider API keys in ngrok Vaults & Secrets and reference them in your gateway configuration. These keys are used instead of any key sent by your SDK:apiKey: "sk-different-key", the gateway uses gateway-key. This keeps provider keys out of your application code—rotate them by updating the secret, no redeployment needed. You can also configure multiple keys for failover and use intelligent selection based on quota and error rates.
2. Passthrough mode
If no keys are configured for a provider, the key from your SDK is forwarded to the provider:3. Restricted provider mode
Whenonly_allow_configured_providers: true, only providers explicitly listed in the providers array are allowed. Requests to unlisted providers are rejected:
anthropic or other unlisted providers will fail. For providers on the list, normal key precedence applies—gateway keys are used if configured, otherwise client keys are passed through.
When to use each mode
| Mode | When to use |
|---|---|
| Passthrough | Development and testing. Applications already have their own provider keys. |
| Gateway-managed | Production deployments. You want centralized key rotation, failover across multiple keys, or to keep provider keys out of application code. Requires adding authorization. |
| Restricted | Compliance or cost control. You need to limit which providers can be used regardless of what keys clients send. |
Multiple keys for failover
Configure multiple provider API keys for automatic failover:Intelligent key selection
For more control over key selection, useapi_key_selection with CEL expressions to select keys based on runtime metrics:
- Quota-aware routing: Route to keys with remaining capacity
- Error-rate routing: Avoid keys hitting rate limits
- Load distribution: Spread traffic across multiple keys
Key sources
Inline keys (development only)
ngrok secrets (recommended)
${secrets.get('namespace', 'key')}.
Next steps
- Securing Your Gateway - Add authorization when using gateway-managed keys
- Traffic Policy Secrets - Store and manage provider keys securely
- Multi-Key Failover - Example configuration with multiple keys
- CEL Functions Reference - API key selection functions