Skip to main content
Traffic Policy enables you to manage traffic to your ngrok endpoints by dynamically implementing logic based on the request before it reaches your upstream services. This guide will teach you how to configure an Agent Endpoint with a custom Traffic Policy using the ngrok Agent CLI. There are two ways to define your Traffic Policy:
  • You can create a yaml or json file and manually apply it to specific endpoints when you start them. This approach is best when you want to apply a different Traffic Policy to each endpoint.
  • You can edit your Agent Configuration File. This file applies to all endpoints you start with the agent CLI by default. Choose this approach to set up traffic management logic for all endpoints running locally on your machine.
This guide will demonstrate both approaches. If you don’t know which to choose, follow the instructions for using a separate policy file.

What you’ll need

1. Set up your Traffic Policy

Create a custom Traffic Policy file with the following contents:
on_http_request:
  - actions:
      - type: custom-response
        config:
          status_code: 200
          body: "Hello, World!"
This policy will respond to each HTTP request with a simple “Hello, World!” message.

2. Start your endpoint

Run the agent, applying the Traffic Policy you saved in the previous step with the --traffic-policy-file flag:
$ ngrok http 80 --traffic-policy-file policy.yml
This command starts an HTTP tunnel for port 80, using the specified policy.yml Traffic Policy to manage traffic.

3. Test it out

After running the ngrok command in the previous step you should now see a URL in the forwarding section. Open the URL in your web browser. You should see the “Hello, World!” message displayed in your browser.

What’s next?

You’ve now successfully set up your first Agent Endpoint with a custom Traffic Policy using the ngrok agent. To learn more about ngrok’s Traffic Policy and its capabilities, check out the following resources: