The problem
Without authorization, a gateway configured with server-side API keys is open to abuse:Solution: API key authorization
Require clients to provide an access token that you control. Generate your own tokens, store them in ngrok Vaults & Secrets, and validate them in your Traffic Policy before processing requests.1
Create a secret for your access token
Create a vault and secret to store your access token:
2
Add authorization to your Traffic Policy
Use an expression to compare the
Authorization header against your secret and reject unauthorized requests:3
Update your client configuration
Clients must now include the access token in their requests:Or with curl:
Multiple access tokens
Support multiple clients with different access tokens. Useset-vars to define the list of valid tokens, then check membership with in:
Using X-API-Key header
If you prefer using anX-API-Key header instead of Authorization: Bearer:
Adding rate limiting
Combine authorization with rate limiting for additional protection:Combining with IP restrictions
For additional security, restrict access to specific IP addresses using CIDR notation:Alternative: JWT validation
For more complex authentication scenarios, use thejwt-validation action to validate JWTs from your identity provider:
Rotating access tokens
To rotate your access token without downtime:- Add the new token to your secrets
- Update your Traffic Policy to accept both tokens temporarily
- Update clients to use the new token
- Remove the old token from your policy and secrets
Best practices
- Use strong tokens - Generate access tokens with at least 32 bytes of entropy
- Never share tokens - Issue different tokens to different clients
- Rotate regularly - Change access tokens periodically
- Monitor usage - Use observability to detect unusual patterns
- Layer defenses - Combine authorization with rate limiting and IP restrictions
- Use HTTPS - Always use HTTPS endpoints (ngrok handles this automatically)
Next steps
- Managing API Keys - Secure provider API key storage
- Restricting Model Access - Control providers and models
- Traffic Policy Secrets - Learn more about secrets
- Rate Limit Action - Add rate limiting