Traffic Policy Examples
Explore a curated collection of examples and configuration examples spanning from common to unconventional use cases for the Traffic Policy module.
A number of these examples come from a longer article about how ngrok makes policy management accessible to developers, including a simple Go-based application for testing these and other configurations.
See the following categories for specific expressions and actions:
Authentication
Add JWT authentication and key-based rate limiting
Building from our Auth0 guide, these rules also add rate limiting based on your consumers' JWTs.
- YAML
- JSON
Loading…
Loading…
Conditional Access to a page using oauth variables
- YAML
- JSON
Loading…
Loading…
Capturing and sending identity token over a header
- YAML
- JSON
Loading…
Loading…
Rate limiting
Rate limit for specific endpoint
This rule applies rate limiting of 30
requests per second to the endpoint
/api/videos
.
- YAML
- JSON
Loading…
Loading…
Rate limit API consumers based on authentication status
Create a low rate limit for unauthenticated (likely free) users, while allowing authenticated users a higher level of capacity.
- YAML
- JSON
Loading…
Loading…
Rate limit API consumers based on pricing tiers
Using a naming scheme in your upstream servers, and API calls using a tier
header, you can quickly customize access to your API based on any number of pricing tiers.
- YAML
- JSON
Loading…
Loading…
Block unwanted requests
Disallow bots and crawlers with a robots.txt
This rule returns a custom response with a robots.txt
file to deny search engine or AI crawlers on all paths.
- YAML
- JSON
Loading…
Loading…
You can also extend the expression above to create specific rules for crawlers based on their user agent strings, like ChatGPT-User
and GPTBot
.
- YAML
- JSON
Loading…
Loading…
Block bots and crawlers by user agent
In addition to, or instead of, denying bots and crawlers with a robots.txt
file, you can also take action on only incoming requests that contain specific strings in the req.user_agent
request variable.
You can extend the expression to include additional user agents by extending (chatgpt-user|gptbot)
like so: (chatgpt-user|gptbot|anthropic|claude|any|other|user-agent|goes|here)
.
- YAML
- JSON
Loading…
Loading…
Deny non-GET requests
This rule denies all inbound traffic that is not a GET request.
- YAML
- JSON
Loading…
Loading…
Custom response for unauthorized requests
This rule sends a custom response with status code 401
and body Unauthorized
for requests without an Authorization header.
- YAML
- JSON
Loading…
Loading…
Block traffic from specific countries
Remain compliant with data regulations or sanctions by blocking requests originating from one or more countries using their respective ISO country codes.
- YAML
- JSON
Loading…
Loading…
Limit request sizes
Prevent excessively large user uploads, like text or images, that might cause performance or availability issues for your upstream service.
- YAML
- JSON
Loading…
Loading…
Other
User agent filtering
We deliver tailored content to Microsoft Edge users by examining the
User-Agent
header for the case-insensitive string (?i)edg/
succeeded by
digits \d
. To see how this works in practice, explore the following
regex101 demonstration.
To ensure correct decoding from YAML/JSON, it's necessary to properly escape the
\d
sequence. In YAML, if your string is not enclosed in quotes, use a single
escape: \\d
. However, when your string is wrapped in quotes, either in YAML or
JSON, you need to double-escape: \\\\d
for accurate decoding.
- YAML
- JSON
Loading…
Loading…
Deprecate an API version
By include a X-Api-Version
header in your API reference or developer documentation, you can quickly return a helpful error message, which encourages them to explore usage of the new version.
- YAML
- JSON
Loading…
Loading…
Manipulate request headers
Add new headers to requests to give your upstream service more context about the consumer, which in turn allows for richer functionality, such as localized languages and pricing.
- YAML
- JSON
Loading…
Loading…
Add compression
Quickly ensure all JSON responses are compressed en route to your API consumer. If your upstream service already handles compression, ngrok skips this step.
- YAML
- JSON
Loading…
Loading…
Enforce TLS version
Prevent obsolete and potentially vulnerable browsers, SDKs, or CLI tools like curl
from attempting to access your API.
- YAML
- JSON
Loading…
Loading…
Log unsuccessful events
Connect your API to ngrok's event logging system for smarter troubleshooting of your API gateway and upstream services.
- YAML
- JSON
Loading…
Loading…